1
votes

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,

1
A rather manual way is to add scale_y_continuous(expand = c(1,1)) and play with expand limitsHydro

1 Answers

2
votes

The limits are automatically set based on the x,y values... but ignore the ymax and ymin values used for the error bar geoms, which is why the errorbars are outside the viewable area on the plot. One option could be to reset your own limits manually, but much better would be to use the expand= argument with scale_y_continuous() to expand the viewable area a bit past the automatically-set limits.

To use the argument, you assign expand=expansion(mult=c(lower,upper)) or expand=expansion(add=c(lower,upper)). The numbers for upper and lower set the expansion on the high or low end of the axis, respectively. Using add expands the axis by adding a discrete amount, whereas mult expands the axis by adding a multiplier. mult is more useful in this case, since the facets have different limits by default. Here's the some numbers that worked okay for me and 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)+
  scale_y_continuous(expand=expansion(mult=c(0.8,0.8))) +
  facet_wrap(~df$var,  scales="free_y", ncol = 1, strip.position = "left")+
  theme_bw()+
  theme(
    axis.line=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=element_blank())

enter image description here