我正在尝试使用grDevices::x11()让patchwork在函数中工作,这样我的绘图就可以在新窗口中打印。我可以用grid来做,但前者不能正常工作,它会打印最后一个窗口中的所有绘图,如下所示:
# devtools::install_github("thomasp85/patchwork")
library(tidyverse)
library(patchwork)
library(grid)
x <- mtcars %>% rownames_to_column()
p1 <- x %>% ggplot(aes(x = rowname, y = mpg)) + geom_col()
p2 <- x %>% ggplot(aes(x = rowname, y = cyl)) + geom_col()
p3 <- x %>% ggplot(aes(x = rowname, y = wt)) + geom_col()
xx2 <- xx1 <- xx <- list(p1 = p1, p2 = p2, p3 = p3)
# Function using grid - works
plot_list1 <- function(name, list1, list2, list3) {
x11()
grid.draw(rbind(ggplotGrob(list1[[name]]),
ggplotGrob(list2[[name]]),
ggplotGrob(list3[[name]]), size = "last"))
}
plot_list1("p1", xx, xx1, xx2) # works
map(names(xx), ~plot_list1(name = ., xx, xx1, xx2)) # and works
# Function using patchwork works for one plot but misbehaves when used with map
plot_list <- function(name, list1, list2, list3) {
x11()
(list1[[name]] + list2[[name]] + list3[[name]] + plot_layout(ncol = 1))
}
# this works
plot_list("p1", xx, xx1, xx2)
# this does not :(
map(names(xx)[1:2], ~plot_list(name = ., list1 = xx, list2 = xx1, list3 = xx2)) 发布于 2020-02-03 11:34:55
答案由托马斯提供:https://github.com/thomasp85/patchwork/issues/68
plot_list <- function(name, list1, list2, list3) {
p <- list1[[name]] + list2[[name]] + list3[[name]] + plot_layout(ncol = 1)
x11()
plot(p)
}
# now works
walk(names(xx)[1:2], ~plot_list(name = ., list1 = xx, list2 = xx1, list3 = xx2)) https://stackoverflow.com/questions/51549095
复制相似问题