由于在同一資料上運行兩個不同的模型,我創建了兩個資料框。每個資料框都包含一個串列列,每行一個圖。我最終希望將所有繪圖列印到 PDF,但將繪圖與多頁 PDF 的單頁上的相同分組變數配對。例如,在下面的簡化示例中,我希望繪圖從model_figs1哪里位于cyl == "6"`cyl == "6"的繪圖左側的同一頁面上。model_figs2' where
以下是一些示例資料:
library(tidyverse)
# I am sure you could combine model_figs1 & model_figs2 into one process/code chunk,
# but this is an overly simplified example and should be kept separate:
model_figs1 <-
mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(plot = map(data, ~ggplot(data = .x,
aes(x = hp, y = mpg))
geom_point()
geom_smooth(method = "lm")
labs(subtitle = cyl)))
model_figs2 <-
mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(plot = map(data, ~ggplot(data = .x,
aes(x = hp, y = mpg))
geom_point()
geom_smooth(method = "loess")
labs(subtitle = cyl)))
# Bind the dfs together
model_figs <-
bind_rows(model_figs1, model_figs2, .id = "id")
如何將具有相同分組變數值的圖配對到單個 PDF 頁面,同時將所有圖一次列印到多頁 PDF?
uj5u.com熱心網友回復:
如果每組只有兩個圖,那么我們可以在 group 列之后使用ggsavewithmarrangeGrobarrange
library(gridExtra)
library(dplyr)
model_figs <- model_figs %>%
arrange(cyl)
ggsave(filename = file.path(getwd(), "Downloads/newplots.pdf"),
plot = marrangeGrob(model_figs$plot, ncol = 2, nrow = 1))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427638.html
