如何使用ggplot2获取几个pdf页面中的图表

Mon*_*ife 6 r ggplot2

我需要帮助将图表处理成多个pdf页面.这是我目前的代码:

file <- read.csv(file="file.csv") 
library(ggplot2)
library(gridExtra)
library(plyr)

gg1 <- ggplot() +
  geom_line(aes(x=TIME, y=var1, colour = "z1"), file) +
  geom_line(aes(x=TIME, y=var2, colour = "z2"), file) + 
  geom_point(aes(x=TIME, y=var3), file) + facet_wrap( ~ ID, ncol=5)+ 
  xlab("x") +
  ylab("Y") +
  ggtitle(" x ") + scale_colour_manual(name="Legend",
    values=c(z1="red", z2 ="blue")) + theme(legend.position="bottom")   
gg10 = do.call(marrangeGrob, c(gg1, list(nrow=4, ncol=4)))
ggsave("need10.pdf", gg10)
Run Code Online (Sandbox Code Playgroud)

这是创建的图像,不分割我的图像

在此输入图像描述

我希望有一个代码可以在多个页面中以4乘4的布局绘制我的绘图.我的代码的最后两行需要调整,我不知道如何自己修复它.

Wei*_*ong 12

ggplus包装似乎你想要做什么.我在下面的代码块中更改了原始内容中的一些内容:facet_wrap已注释掉,并被file移动到ggplot不必在每个内容中重新指定geom_*:

gg1 <- ggplot(file) +
  geom_line(aes(x=TIME, y=var1, colour = "z1")) +
  geom_line(aes(x=TIME, y=var2, colour = "z2")) + 
  geom_point(aes(x=TIME, y=var3)) +
  # facet_wrap( ~ ID, ncol=5) +
  xlab("x") +
  ylab("Y") +
  ggtitle(" x ") + 
  scale_colour_manual(name="Legend",
    values=c(z1="red", z2 ="blue"),
    labels=c("X","Y")) +
  theme(legend.position="bottom")   

devtools::install_github("guiastrennec/ggplus")
library(ggplus)
pdf("need10.pdf")
gg10 <- facet_multiple(plot=gg1, facets="ID", ncol = 4, nrow = 4)
dev.off()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 在此输入图像描述