I would like to create a dotplot with labels like shown in the graphic.
However I cant get geom_text_repel to draw lines to the correct positions if points are close together.
df=data.frame(name=c("A1","A2","B1","B2","B3"),value=c(1,2,2,2,2),group=c("A","A","B","B","B"))
ggplot(df,aes(x=group,y=value,label=name))+
geom_dotplot(df,mapping=aes(x=group,y=value),binaxis='y', stackdir='center', dotsize=1)+
geom_text_repel(df,mapping=aes(segment.color="red"),box.padding = 1,max.overlaps=10)
Related
I am trying to mimic two types of plots (two images provided) where the negative values are to the right and positive to the left. I have sample-code below:
tdf<-data.frame(prcnt=c(-50,25,-80,5,10,-40),nm=c('AB','BC','CD','DE','EF','FG'),catg=c(rep('catA',2),rep('catB',2),rep('catC',2)))
ggplot(tdf,aes(nm,prcnt,fill=catg))+geom_col()+scale_y_continuous(limits=c(-100,100))+coord_flip()+scale_y_reverse()
How can I also have it mean-centered (at 0) like this? Thanks.
You could get the top plot with something like
ggplot(tdf,aes(ymin=as.numeric(nm)-.45,ymax=as.numeric(nm)+.45,
xmin=100, xmax=prcnt,fill=catg))+
geom_rect() +
scale_y_continuous(breaks=as.numeric(tdf$nm),
labels=levels(tdf$nm))+
scale_x_reverse(limits=c(100, -100))
and you can get the bottom plot with
ggplot(tdf,aes(nm,prcnt,fill=catg))+
geom_col()+
scale_y_reverse(limits=c(100,-100))+
coord_flip()
I have created a basic scatterplot with ggplot2 and added a rug with geom_rug on the y-axis (left side) to it, however the rug is hiding a few points. I have tried position_dodge, position_jitterdodge and hjust in order to move the rug and make those points visible, but it did not give the desired result. I can't move the rug to the right side of the plot, since it will be hiding points here as well.
Is there a way to move the rug on the other side of the y-axis, outside of the plot?
Here is some example code:
data("midwest", package = "ggplot2")
ggplot(midwest, aes(x=area, y=poptotal)) + geom_point(alpha=0.8)+
geom_rug(aes(x=NULL, y=poptotal), alpha=0.8)
I solved the problem by simply expanding the x-axis limits, using
scale_x_discrete(expand = c(0.01,0.01))
Is there a reason why ggplot might mess with the geom_point shape in the legend?
In the actual plot everything looks with the shapes correctly plotted as circles, but in the legend it shows them as weird boxes / squares, i.e. it is showing this:
But it should show this:
Could it be because I have an ifelse in my geom_point ? This is what I have here for this part:
geom_point(aes( y=y, colour=ifelse( (ty>308)&(Time < chron(times=c('08:30:30.0'))), ifelse(side=='left', 'red', 'blue'),'gray')), na.rm = T)
This issue is actually because geom_point and geom_line are both plotted and the points are varying according to the size parameter. ggplot is trying to show a point on a line which looks good when it is small and clear but becomes strange and box-like as the size varies.
To make it clearer, turning off the legend for either the line or the points will keep just one.
For example:
geom_line(aes(y=foo , colour='green'), show.legend = F)
I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.
I am trying to make a scatterplot in ggplot2 with a size mapping to a third variable and density2d contours. It seems as if the legend is being confused by the inclusion of density2d contours.
For example, the following code works:
library('ggplot2')
set.seed(1)
x=rnorm(100); y=rnorm(100,sd=10); z=seq(1,10,length.out=100)
dd=data.frame(x=x,y=y,z=z)
ggplot(dd,aes(x,y,size=z))+geom_point()
But now, note the legend behaves unusually when I add in a call to stat_density2d(). In particular, the plot legend shows blue blocks instead of black circles:
ggplot(dd,aes(x,y,size=z))+geom_point()+stat_density2d()
As size= is one of the aesthetics you can set for the stat_density2d() and in this case it is set in ggplot() call, legend is made for both - lines and points (points are hided under lines in legend as geom_point() is called before stat_density2d()). To remove blue lines from legend, you can set manually size=0.5 (or some other value) inside the stat_density2d() and then legend will be correct.
ggplot(dd,aes(x,y,size=z))+geom_point()+stat_density2d(size=0.5)