虽然这可能是重复的,但我想知道我的问题的解决方案。我尝试使用 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
。我如何让这个脚本运行?
您可以使用ggplus包:
https://github.com/guiastrennec/ggplus
我特别发现它比使用 ArrageGrobs/gridExtra 更容易;它会自动为您将这些方面放在几页中。
由于您将初始绘图保存为"p",因此您的代码将如下所示:
# Plot on multiple pages
facet_multiple(plot = p,
facets = 'group', #****
ncol = 2,
nrow = 1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句