簡體   English   中英

使用ggplot和marrangeGrob在多個頁面上顯示多個圖形

[英]Multiple graphs over multiple pages using ggplot and marrangeGrob

雖然這可能是重復的,但我想知道我的問題的解決方案。 我嘗試使用ggplot運行多個頁面上的多個圖表的代碼,但我不知道數據是如何構造的 ,因此無法運行它。 我想繪制多個圖形並以pdf格式在多個頁面上打印。 我用mtcars試了一下:

library(ggplot2)
library(reshape2)
library(plyr)
library(gridExtra)

mtcars2 = melt(mtcars, id.vars='mpg')
mtcars2$group[mtcars2$variable  %in% c("cyl", "disp", "hp", "drat")] <- "A"
mtcars2$group[mtcars2$variable  %in% c("wt", "qsec", "vs", "am")] <- "B"
mtcars2$group[mtcars2$variable  %in% c("gear", "cyl")] <- "C"


p = ggplot(mtcars2) +
  geom_jitter(aes(value,mpg, colour=variable)) + 
  geom_smooth(aes(value,mpg, colour=variable), method="lm", se=FALSE) +
  scale_y_continuous(limits = c(0, 60))+
  labs(x = "Percentage cover (%)", y = "Number of individuals (N)")

plots = dlply(mtcars2, "group", `%+%`, e1 = p)
ml = do.call(marrangeGrob, c(plots, list(nrow=2, ncol=2)))
ggsave("multipage.pdf", ml)

產生錯誤Error in (function (grobs, ncol, nrow, ..., top = quote(paste("page", : argument "grobs" is missing, with no default 。如何運行此腳本?

而不是在之前版本的gridExtra中需要的do.call() ,請嘗試marrangeGrob(grobs = plots, nrow=2, ncol=2)

你可以使用ggplus包:

https://github.com/guiastrennec/ggplus

我特別發現它比使用ArrageGrobs / gridExtra更容易; 它會自動將方面放在幾頁中。

由於您將初始圖保存為“p” ,因此您的代碼將如下所示:

# Plot on multiple pages
facet_multiple(plot = p, 
           facets = 'group', #**** 
           ncol = 2, 
           nrow = 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM