0

Im trying to make a combine a graph to three boxplots in R using ggarange. But then it gives me the error: Warning: Cannot convert object of class list into a grob.Warning: Cannot convert object of class list into a grob.Warning: Cannot convert object of class list into a grob. I tried several things from Stackoverflow but nothing seems to work for my code.

I use the following code to make the boxplots:

clr <- hcl.colors(length(unique(df6$Q8)), alpha=.5)
blr <- hcl.colors(length(unique(df6$Q8)), alpha=1)

A <- boxplot(AI ~ Q8,data=df6,
        xlab="walking ability", ylab="% active", col=clr, border=blr)

corr <- with(df6, cor.test(AI, Q8))[c('estimate', 'conf.int')]

legend('topleft', 
       legend=bquote(rho ==.(signif(corr[[1]], 2))~'['*.(signif(corr[[2]][1], 2))*','~.(signif(corr[[2]][2], 2))*']'),
       bty='n')

clr <- hcl.colors(length(unique(df6$Q8)), alpha=.5)
blr <- hcl.colors(length(unique(df6$Q8)), alpha=1)

B <- boxplot(activity ~ Q8,data=df6,
        xlab="Walking ability", ylab="% active", col=clr, border=blr)

corr <- with(df6, cor.test(activity, Q8))[c('estimate', 'conf.int')]

legend('topleft', 
       legend=bquote(rho ==.(signif(corr[[1]], 2))~'['*.(signif(corr[[2]][1], 2))*','~.(signif(corr[[2]][2], 2))*']'),
       bty='n')

clr <- hcl.colors(length(unique(df6$Q8)), alpha=.5)
blr <- hcl.colors(length(unique(df6$Q8)), alpha=1)

C <- boxplot(ZIM_SD ~ Q8,data=df6,
        xlab="Walking ability", ylab="Zero inflated model", col=clr, border=blr)

corr <- with(df6, cor.test(ZIM_SD, Q8))[c('estimate', 'conf.int')]

legend('topleft', 
       legend=bquote(rho ==.(signif(corr[[1]], 2))~'['*.(signif(corr[[2]][1], 2))*','~.(signif(corr[[2]][2], 2))*']'),
       bty='n')

Then I use this code to combine the graphs (this leads to the error).

ggarrange(A,                # First row with line plot
  # Second row with box and dot plots
  ggarrange(B, C, ncol = 2, labels = c("", "")), 
  nrow = 2, 
  labels = ""       # Label of the line plot
  ) 
Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
  • We can't reproduce your error message since you haven't provided your data, have you checked this? https://stackoverflow.com/questions/58124284/lay-out-multiple-ggplot-graphs-on-a-page – jrcalabrese Oct 25 '22 at 14:25
  • You are using `ggplot2` commands to combine base graphics plots (`boxplot()`). That will not work. Use one system or the other. For base graphics the function you want is `layout()` and you will use it do define the positions of the three plots before creating the plots. – dcarlson Oct 25 '22 at 18:09
  • Could you please share some reproducible data using `dput`? – Quinten Oct 25 '22 at 18:14

0 Answers0