0

I wrote a for loop to make a matrix of charts, however I wrote the code as following

par(mfrow = c(4, 4))

for (i in c(6:17)) {
  print(ggpubr::ggboxplot(logdat, 
                          x = "Diagnostic", 
                          y = names(logdat)[i] , 
                          color = "Diagnostic", 
                          add = "jitter") + 
    stat_compare_means(comparisons = my_comparisons,
                       method = "t.test")) 
}

Only individual graphs are shown and not the matrix I want

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Omar113
  • 210
  • 1
  • 7
  • 1
    Can you supply some data ? Try to make posts reproducible – Carles Oct 02 '18 at 10:14
  • Interesting question. Would like to help but cannot. Please set up a [MCVE] including all `library` lines and UDF assignments. Your code should run in empty R environments free from local disk files for us to help. See [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Parfait Oct 02 '18 at 14:26

1 Answers1

0

You are using the ggboxplot from ggpubr which is based on ggplot2, which is based on grid graphics system. Unfortunately, grid graphics does not work with base R graphics, so setting par doesn't work either. To arrange plots created by the library build on top of grid please use the grid.arrange from gridExtra package. See the example below:

library(ggpubr)
data("ToothGrowth")
df <- ToothGrowth
grobsList <- list()
for (i in c(1:4)) {

  p <- ggboxplot(df, x = "dose", y = "len", width = 0.8)
  grobsList <- c(grobsList, list(p))
}


gridExtra::grid.arrange(
  grobs = grobsList, ncol = 2, nrow = 2)

You can also try to use marrangeGrob but it might create the plot outside the current graphical window.

You can also render the plots inside the loop when the desired number of plots were created:

library(ggpubr)
data("ToothGrowth")
df <- ToothGrowth
grobsList <- list()

for (i in c(1:7)) {

  p <- ggboxplot(df, x = "dose", y = "len", width = 0.8)
  grobsList <- c(grobsList, list(p))

  if(length(grobsList) == 4) {
    # print the result
    gridExtra::grid.arrange(
      grobs = grobsList, ncol = 2, nrow = 2)
    grobsList <- list() # reset the list containing the plots
  }
}

# print the remaining plots
if(length(grobsList) > 0) {
  gridExtra::grid.arrange(
    grobs = grobsList, ncol = 2, nrow = 2)
}
  • Thank you for your insight , that was great. However, on my data it gives me error when I put the nrow argument " Error: nrow * ncol >= n is not TRUE". also all the graphs are collapsed and not saving the position. any ideas – Omar113 Oct 02 '18 at 13:41
  • Oh, when there's no space on the page in `base` graphics, it automatically creates a new drawing area. However, in `grid` graphics, this is not a case, so the number of plots should be lower than the ncol*nrow. I'll edit the answer to take this into account. – Zygmunt Zawadzki Oct 02 '18 at 13:53
  • I don't understand any of your code but this solved my problem. You are a genius thank you – Omar113 Oct 03 '18 at 07:12