3

I have 10 ggplot graphs that were created using for-loops. I would like to annotate the graphs, but the Y position changes for each graph (x position remains the same).

Y axis on graph 1 runs from 2-8

Y axis on graph 2 runs from 800-1400 etc.

My code looks like this:

 Col_loop <- c("V", "Cr", "Co", "Ni", "Cu", "Zn", "Sr", "Cd", "Ba", "Pb")
 for (i in col_loop){ 
   print(i)
   plot <- ggplot(dat, aes_string(x = "Time", y= i)) + 
     geom_point(size = 1)+
     geom_line(size = 0.5)+
     geom_text(x=5, y=8, label="year", color = "red") 
   print(plot)
 }

This works for graph 1 but doesn’t show up on graph 2 because the y axis is on a different scale. How do I get the annotation (geom_text) to consistently be at the top of the graph no matter what the Y axis is?

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • 4
    Use `y = Inf` to stick it to the top. Use `vjust` and `hjust` to micromanage the position. E.g.: `ggplot(mtcars, aes(qsec, hp)) + geom_point() + annotate('text', x = Inf, y = Inf, label = 'test', hjust = 3, vjust = 3)`. – Axeman Feb 26 '20 at 22:23
  • Perfect. Thanks for your help! – Kimberly Peter Feb 27 '20 at 18:28

0 Answers0