Search code examples
rggplot2annotationsgridscale

GGPlot annotation gets pushed off page scale when combining multiple plots within grid.draw


I have 5 plots for 5 different groups. I want to indicate a statistically significant difference a specific time points. I used annotate() to place asterisks in individual plots above the time points. However, when I combine all the plots together to make one figure, the asterisks get pushed off the plots. It looks like it is a problem with the y scales not being fixed. I'm providing as much data as I feel comfortable with. The first bit of code is for one of the groups. The plots all look relatively similar for the 5 groups. The second bit is the data frame I am using to combine the plots. Pictures attached of one plot by itself, then all plots combined. There should be multiple asterisks on multiple plots

ggplot(data,aes(X,Y,group=Group,color=Group))+
  theme_bw()+
  theme(panel.grid.major=element_line(color="white",size=.1))+
  theme(panel.grid.minor=element_line(color="white",size=.1))+
  geom_point(stat="summary")+
  geom_errorbar(stat="summary",fun.data=mean_se,width=0.25)+
  geom_line(stat="summary")+
  scale_color_manual(labels = c("C", "T"),values=c("black", "red"))+
  theme(axis.title.y = element_text(vjust=2.5))+
  annotate("text", x=5, y=3, label= "*",size=10)

grid.newpage()

grid.draw(rbind(ggplotGrob(plotanimal1), 
            ggplotGrob(plotanimal2), 
            ggplotGrob(plotanimal3), 
            ggplotGrob(plotanimal4), 
            ggplotGrob(plotanimal5)))

One Graph

All 5 graphs


Solution

  • You can make the asterisks by using geom_point with shape = 42. That way, ggplot will automatically fix the y axis values itself. You need to set the aesthetics at the same values you would have with annotate. So instead of

     annotate("text", x=5, y=3, label= "*",size=10)
    

    You can do

     geom_point(aes(x=5, y=3), shape = 42, size = 2)