如何与牛图的 plot_grid 一起使用

iat*_*wks 1 r ggplot2 cowplot purrr

我想将几个 ggplot2 图表组合成一个使用cowplot::plot_grid(). 从它的文档:

?plot
Arguments

... 
List of plots to be arranged into the grid. The plots can be objects of one of the following classes: ggplot, recordedplot, gtable, or alternative can be a function creating a plot when called (see examples).
Run Code Online (Sandbox Code Playgroud)

所以,如果我输入一个 ggplot2 对象列表 plot_grid(),它应该将这些图合并为一个,对吗?

那么为什么这行不通呢?

p1 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 
  geom_point(size=2.5) 
p2 <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar() +
  theme(axis.text.x = element_text(angle=70, vjust=0.5))

list(p1, p2) %>% 
  map(plot_grid)
Run Code Online (Sandbox Code Playgroud)

www*_*www 5

参见map( ?map)的文档,它指出:

.x     A list or atomic vector. 
.f     A function, formula, or atomic vector.
Run Code Online (Sandbox Code Playgroud)

这意味着您提供的功能.f将应用于.x. 所以下面的代码

list(p1, p2) %>% map(plot_grid)
Run Code Online (Sandbox Code Playgroud)

和下面的代码一样

plot_grid(p1)
plot_grid(p2)
Run Code Online (Sandbox Code Playgroud)

,这可能不是您想要的。

你想要的大概是这个

plot_grid(p1, p2)
Run Code Online (Sandbox Code Playgroud)

或这个

plot_grid(plotlist = list(p1, p2))
Run Code Online (Sandbox Code Playgroud)