【发布时间】:2020-01-07 19:31:49
【问题描述】:
假设我有一个地块列表plotlist,我想打电话给patchwork::wrap_plots(plotlist)。
我还想在列表的开头再添加一个情节。
让我们从一个包含 2 个图的列表开始:
library(ggplot2)
library(patchwork)
cols <- c("mpg", "hp")
plot_col <- function(this_col) {
ggplot(mtcars) +
aes_string("wt", this_col) +
geom_point()
}
plotlist <- lapply(cols, plot_col)
这行得通:
res <- wrap_plots(plotlist)
但是等等,我还有一个情节。
p <- plot_col("qsec")
我可以将新情节 p 前置到 plotlist 之前吗?
这些方法都不起作用:c()、list()、purrr::prepend()
newlist <- c(p, plotlist)
res <- wrap_plots(newlist)
#> Error: Only know how to add ggplots and/or grobs
newlist <- list(p, plotlist)
res <- wrap_plots(newlist)
#> Error: Only know how to add ggplots and/or grobs
newlist <- purrr::prepend(plotlist, p)
res <- wrap_plots(newlist)
#> Error: Only know how to add ggplots and/or grobs
【问题讨论】:
-
您需要
newlist <- c(list(p), plotlist)在正确的级别添加p。 -
在这种情况下你可能会这样做
p + plotlist