I'm trying to change the y axis of multiple graphs plotted in the same plot area using facet_wrap. When I set the scale = "free_y" all the dots are plotted within the range used for the y axis. The issue is that some of the error bars exceed the y axis limits and therefore are being cut from the graph. I need to expand each y axis limits individually, given different scales, and do not even know if that is possible in ggplot2.
Here's a subset:
This is the data set:
df <- data.frame("time" = c(1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10),
"mean" =c(1.06, 5.35,3.67, 7.11, 6.03 , 7.55, 6.27, 12.06, 9.74, 0.71,115.7,103.1,104.0,65.3,103.0,117.2,97.4,75.4,82.4,85.8,69.21, 80.43, 66.38, 65.40, 73.32, 76.40, 77.55, 53.25, 51.00, 53.55),
"sd" =c(0.56, 3.56, 2.43, 2.82, 1.72, 2.64, 3.69, 7.51, 3.70, 0.00, 26.2,32.7,17.7,23.4,23.5,15.9,25.4,23.4,7.90, 19.25, 8.66, 14.68, 10.97, 9.71, 11.55, 13.68,16.54, 11.83, 13.4, 10.3),
"var" =c("abd","abd","abd","abd","abd","abd","abd","abd","abd","abd","g.size","g.size","g.size","g.size", "g.size","g.size","g.size","g.size","g.size","g.size","size","size","size","size","size","size","size","size","size","size"))
This is the code:
ggplot(df, aes(x =time,y=mean)) + geom_point(size = 3)+ geom_errorbar(ymax=df$mean+df$sd,ymin=df$mea-df$sd,width=0.1)+ facet_wrap(~df$var, scales="free_y", ncol = 1, strip.position = "left")+ theme_bw()+theme(axis.line.x=element_line(colour="black"),axis.line.y=element_line(colour="black"), axis.text.y= element_text(size=18),axis.text.x=element_text(size=18,angle = 90,vjust=0.1),axis.title.x=element_blank(),axis.title.y=element_blank())
This is the resulting plot:
How do I expand each individual y axis so that the error bars fit in?
Thanks,
scale_y_continuous(expand = c(1,1))
and play withexpand
limits
– Hydro