How to add one common y and x label for an arrange of plots and also a label for several columns of this arrange in R?

前端 未结 2 798
北荒
北荒 2021-01-28 06:14

I have an arrange of 16 plots (4x4). The last column of this arrange is the legend, that is common for each row. I add below a fake code to create something similar to what I ha

2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-28 06:55

    I found a solution using add_sub from cowplot package. However, I had to play with different parameters for a while to get what I wanted and I don't understand well why some parameters do what they do. So this solution is tricky and you need some time playing to get what you want unless you know well the function.

    Here the code I used:

    P <-plot_grid(
      p1, p1, p1, leg1, 
      p2, p2, p2, leg2,
      p3, p3, p3, leg3,
      p4, p4, p4, leg4,
      ncol = 4, rel_widths=c(2,2,2,0.5),align = "hv"
    )  + theme(plot.margin = margin(55, 10, 30, 30)) # I added margins to the arrange
    
    
    P1 <- add_sub(P, "x axis text", x=0.5, y=-0.5, vpadding = grid::unit(0, "lines"))
    ggdraw(P1)
    P2 <- add_sub(P1, "y axis text", x=-0.02, y=-23, vpadding = grid::unit(-5, "lines"), angle = 90)
    ggdraw(P2)
    P3 <- add_sub(P2, "row 1", x=0.17, y=45, vpadding = grid::unit(0, "lines"),vjust = 1)
    ggdraw(P3)
    P4 <- add_sub(P3, "row 2", x=0.47, y=46, vpadding = grid::unit(0, "lines"),vjust = 1)
    ggdraw(P4)
    P5 <- add_sub(P4, "row 3", x=0.81, y=-25.4, vpadding = grid::unit(-2, "lines"),vjust = 1)
    ggdraw(P5)
    

    Something strange and that took me sometime until I realized is that the plot in the environment doesn't look like the plot you save. Look at what I see in Rstudio when I run the above code:

    However, when I save the plot (without changing anything), I see this plot:

提交回复
热议问题