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))
Related
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)
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()
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 am polishing my graphs and have a problem with fitting direct labels in the plotting area. A want to remove most of the area between y1 and the y-axis to the left in the plot similar to that generated by the code below, but keep the extra area to the right to have room for the labels.
Adding +scale_x_discrete(expand=c(0,0.05)) removes extra area on both sides, but leaves no room for labels, and it can't seem to remove it on only one side.
Adding margins to the right of the plotting area with +theme(plot.margin = unit(c(0,4,0,0), "cm")) still doesn't allow the labels to appear there.
A solution that places the labels outside, to the right of the border would be even better.
Any help much appreciated.
library(ggplot2)
library(directlabels)
library(reshape2)
theme_set(theme_bw())
# some data
dfr<-data.frame(c("Longish Name A","Longish Name B","Longish Name C"),c(1,1,1),c(1,2,3),c(2,3,4))
colnames(dfr) <- c("subject","y1","y2","y3")
dfr<-melt(dfr, id.vars="subject")
# the graph
ggplot(data=dfr,aes(y=value, x=variable, group=subject)) +
geom_line(aes(color=subject))+
geom_dl(aes(label=subject), list(dl.trans(x=x+0.2), "last.qp", cex=0.5)) +
guides(color=FALSE)
Convert your x values to numeric inside the aes() and then use scale_x_continuous() to get back to original labels and set limits= that are wider on side.
ggplot(data=dfr,aes(y=value, x=as.numeric(variable), group=subject)) +
geom_line(aes(color=subject))+
geom_dl(aes(label=subject), list(dl.trans(x=x+0.2), "last.qp", cex=0.5)) +
guides(color=FALSE)+
scale_x_continuous(breaks=c(1,2,3),labels=c("y1","y2","y3"),expand=c(0,0.05),
limits=c(1,3.4))
How do I increase the grey plot area of a chart with one factor based axis and one numerical axis so that text labels in geom_text() plots are in view and do not extend outside the plot area?
In particular, I would like to extend the grey area to provide a margin area within the plot area that allows the text labels to appear in full.
Or is there a better way?
You can change the layout option of each ggplot using ggplot_gtable, then display all plots using grid.arrange.
library(ggplot2)
library(gridExtra)
## create a dummy ggplot
(g1 <- ggplot(mtcars, aes(wt, mpg)) +
geom_text(aes(label=rownames(mtcars)), size=6, angle=45) +
theme(plot.margin = unit(rep(1, 4), "cm")))
Obviously the text labels do not extend outside the plot area. But the following code allows just that:
gg_table <- ggplot_gtable(ggplot_build(g1))
gg_table$layout$clip[gg_table$layout$name=="panel"] <- "off"
grid.draw(gg_table)
Create a gg_table for each panel, then use grid.arrange to display all:
grid.arrange(gg_table, gg_table, gg_table, gg_table, ncol=2)
I know this is labor intensive, but you can write a function to create multiple ggplots and gg_tables to save time.