[英]Plotting two graphs getting the error: Cannot convert object of class list into a grob
I am trying to place two boxplots (box1, box2) beside each other and to label them "A" and "B" in the upper left corner.我正在尝试将两个箱线图(box1、box2)并排放置,并在左上角放置“A”和“B”label。 I am trying to use the
cowplot
-package for this.我正在尝试为此使用
cowplot
。
This is the function:这是 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.*
在 as_grob.default(plot) 中:无法将 class 列表中的 object 转换为 grob。*
And the only printout I get is the A and B letters.我得到的唯一打印输出是 A 和 B 字母。 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 在页面上布置多个 ggplot 图
In that question someone also suggested using dev.off()
but that did not work for me either.在那个问题中,有人还建议使用
dev.off()
但这对我也不起作用。
So grateful for replies!非常感谢回复!
The solution that I wrote based on mhh's reply:我根据mhh的回复写的解决方案:
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)
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)
.这是因为它需要一个列表,但你提供了一个向量,为了克服这个问题,你可以简单地使用
boxC <- list(box1,box2)
。
Here is a working example这是一个工作示例
To do this, you need ggplot2
and ggpubr
Install the latter with install.packages("ggpubr")
.为此,您需要
ggplot2
和ggpubr
使用install.packages("ggpubr")
安装后者。 Then get them both into your workspace with library(ggpubr)
然后使用
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)由reprex package (v0.3.0) 创建于 2020-08-19
Thanks to mhh's answer I made this code which works: :)感谢 mhh 的回答,我制作了这段有效的代码::)
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.