0

I have created a list of plots using the below code:

plots <- list()
for(i in 1:(k*k)){
  plots[[i]] <- ggplot(subset(mean_conf, Names == Names[i]), aes(x=nob)) + 
    geom_line (aes(y = Mean), color = "black") + 
    geom_line (aes(y = Left_Interval), color="black", linetype="twodash") +
    geom_line (aes(y = Right_Interval), color="black", linetype="twodash") + 
    theme(axis.title.x=element_blank(),axis.title.y=element_blank()) + expand_limits(y = 0)
}

using grid.arrange function (do.call(grid.arrange,plots)) brings below picture

enter image description here

I want to add text to specific areas on my plot. Something like this: enter image description here

I have checked different functions, but I could not find a solution to my problem. I would appreciate any advice, suggestions, and help.

ShazAl
  • 35
  • 3
  • 1
    Have you try to facet your plot ? So, can you provide a reproducible example of your data in order people can try some solution on their own r session ? (see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – dc37 Apr 23 '20 at 19:43
  • Have you looked the cowplots package? this link may provide some clues: https://www.r-bloggers.com/ggplot2-easy-way-to-mix-multiple-graphs-on-the-same-page/ – Peter Apr 23 '20 at 20:04
  • 3
    Like dc37 says, try to omit the `subset()` statement in your main ggplot call, and add `+ facet_wrap(~ Names, ncol = 2)` – teunbrand Apr 23 '20 at 20:25
  • 1
    @dc37 Thanks for the recommendation, it was great. I had to create another column to match the length of my observations, and after that, everything worked just fine. – ShazAl Apr 23 '20 at 23:17
  • @teunbrand thanks it worked just great. – ShazAl Apr 23 '20 at 23:17
  • @peter thanks, I tried to use cowplots but I could not get it to work for my problem. – ShazAl Apr 23 '20 at 23:19
  • also check for the `patchwork` package, very simple of use. https://github.com/thomasp85/patchwork – Jrm_FRL Apr 24 '20 at 08:27

1 Answers1

0

gridExtra and grid may be your friends for the layout you are after.

Here's a starter for 10.

library(ggplot2)
library(gridExtra)
library(grid)


p <- ggplot(mtcars, aes(disp, wt)) + geom_line()

ver <- textGrob("Plots verion 1", gp = gpar(fontsize = 15))
A_top <- textGrob("A", gp = gpar(fontsize = 15))
A_left <- textGrob("A", gp = gpar(fontsize = 15), x = unit(45, "mm"), y = unit(15, "mm") )

B_top <- textGrob("B", gp = gpar(fontsize = 15))
B_left <- textGrob("B", gp = gpar(fontsize = 15), x = unit(45, "mm"), y = unit(15, "mm"))

grid.arrange(ver, A_top, B_top, A_left, p, p, B_left, p, p, 
                  top = textGrob("Title", gp=gpar(fontsize = 20), x = unit(125, "mm")),
                  bottom = textGrob("Subtitle", gp=gpar(fontsize = 10), x = unit(65, "mm")), 
                  ncol = 3, 
                  widths = unit(c(50, 75, 75), "mm"), 
                  heights = unit(c(10, 75, 75), "mm"))


This gives you:

enter image description here

The following links can be explored for refining the layout

https://cran.r-project.org/web/packages/gridExtra/vignettes/arrangeGrob.html#title-andor-annotations

Peter
  • 11,500
  • 5
  • 21
  • 31