0

I am trying to place two boxplots (box1, box2) beside each other and to label them "A" and "B" in the upper left corner. I am trying to use the cowplot-package for this.

This is the function:

plot_grid(box1, box2, labels = c('A', 'B'), label_size = 12)

Then I get the warning message:

In as_grob.default(plot) : Cannot convert object of class list into a grob.*

And the only printout I get is the A and B letters. I have also tried to use:

boxC <- c(box1, bow2)
plot_grid(plotlist = boxC, labels = c('A', 'B'), label_size = 12, nrow=2)

My code mostly came from the answers to this similar question, but it does not seem to work for me.

Lay out multiple ggplot graphs on a page

In that question someone also suggested using dev.off() but that did not work for me either.

So grateful for replies!

The solution that I wrote based on mhh's reply:

BOX1_data <- read.table(file = "clipboard", 
                  sep = "\t", header=TRUE)
BOX1_data$Histology <- as.factor(BOX1_data$Histology)
BOX1plot <- ggplot(BOX1_data, aes(x=Histology, y=No.Variants)) + geom_boxplot()
BOX1plot

BOX2_data <- read.table(file = "clipboard", 
                  sep = "\t", header=TRUE)
BOX2_data$Stage <- as.factor(BOX2_data$Stage)
BOX2plot <- ggplot(BOX2_data, aes(x=Stage, y=No.Variants)) + geom_boxplot()
BOX2plot

BOX_list <- list(BOX1plot, BOX2plot)
> ggarrange(plotlist = BOX_list, labels = c('A', 'B'), ncol = 2)
Alvida
  • 15
  • 1
  • 4
  • Your main issue is that your plot list is not a list! Try with `boxC <- list(box1,box2)` and you will see that it works perfectly. – mhovd Aug 19 '20 at 07:26
  • I tried this by writing: > boxC <- list(box1,bow2) > plot_grid(plotlist = boxC, labels = c('A', 'B'), label_size = 12, nrow=2) But the same error occurs. – Alvida Aug 19 '20 at 09:13
  • Try to not include `label_size = 12` – mhovd Aug 19 '20 at 09:44

2 Answers2

3

This is because it is expecting a list, but you are providing a vector! To overcome this, you can simply use boxC <- list(box1,box2).

Here is a working example

To do this, you need ggplot2 and ggpubr Install the latter with install.packages("ggpubr"). Then get them both into your workspace with library(ggpubr)


library(ggplot2)
library(ggpubr)
df1 <- data.frame(group = "Stuff",  var = runif(10))
df2 <- data.frame(group = "Stash",  var = runif(10))

box1 <- ggplot(data = df1, aes(y = var)) +
  geom_boxplot()

box2 <- ggplot(data = df2, aes(y = var)) +
  geom_boxplot()

my_plot_list <- list(box1,box2)

ggarrange(plotlist = my_plot_list, labels = c('A', 'B'), nrow = 2)

Created on 2020-08-19 by the reprex package (v0.3.0)

mhovd
  • 3,724
  • 2
  • 21
  • 47
  • I tried applying your code mhh like this: dfbox1 <- data.frame(group = box_data) > dfbox2 <- data.frame(group = box2_data) > box1df <- ggplot(data = dfbox1, aes(y = var)) + + geom_boxplot() > box2df <- ggplot(data = dfbox2, aes(y = var)) + + geom_boxplot() > box_list <- list(box1df,box2df) > ggarrange(plotlist = box_list, labels = c('A', 'B'), nrow = 2) But then I get this error: Error: Aesthetics must be valid data columns. Problematic aesthetic(s): y = var. Did you mistype the name of a data column or forget to add after_stat()? – Alvida Aug 19 '20 at 09:10
  • Is this because I did not specify the "var="? I am very new to this so what should I put instead of your random 10 numbers? – Alvida Aug 19 '20 at 09:24
  • Can you include the code for your plots in your original question, @Alvida? – mhovd Aug 19 '20 at 09:45
1

Thanks to mhh's answer I made this code which works! :)

BOX1_data <- read.table(file = "clipboard", 
                  sep = "\t", header=TRUE)
BOX1_data$Histology <- as.factor(BOX1_data$Histology)
BOX1plot <- ggplot(BOX1_data, aes(x=Histology, y=No.Variants)) + geom_boxplot()
BOX1plot

BOX2_data <- read.table(file = "clipboard", 
                  sep = "\t", header=TRUE)
BOX2_data$Stage <- as.factor(BOX2_data$Stage)
BOX2plot <- ggplot(BOX2_data, aes(x=Stage, y=No.Variants)) + geom_boxplot()
BOX2plot

BOX_list <- list(BOX1plot, BOX2plot)
> ggarrange(plotlist = BOX_list, labels = c('A', 'B'), ncol = 2)
Alvida
  • 15
  • 1
  • 4
  • If my answer (or yours) has the appropriate answer for the question, please mark it as the accepted answer so that others may more easily find the solution. In this case, my perhaps biased opinion would lean towards my answer, as it provides example data and visual output. :) – mhovd Aug 20 '20 at 09:41
  • 1
    Absolutely! This was my first question on the forum so I am not so familiar with it yet. Thank you for the help! – Alvida Aug 21 '20 at 10:50