6
votes

I need help to process graphs into multiple pdf pages. Here is my current code:

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)

Here is the image created, without splitting my images

enter image description here

I wish to have a code to get my plots in a 4 by 4 layout in multiple pages. The last two lines of my code need adjustment and I do not know how to fix it myself.

2
This seems useful: A set of additional functions for ggplot2. I googled facet_wrap multiple pages. - Weihuang Wong
i substituted multi.plot <- marrangeGrob(grobs = gg1, nrow = 2, ncol = 2, top = quote(paste(gg1$labels$title,'\nPage', g, 'of', pages))) pdf('Example_marrangeGrob.pdf', w = 12, h = 8) print(multi.plot) dev.off() but received the error Error in gList(data = list(wrapvp = list(x = 0.5, y = 0.5, width = 1, : only 'grobs' allowed in "gList". Is there a way to fix this? - Monklife

2 Answers

13
votes

The ggplus wrapper appears to do what you want. I changed a couple of things in the code block below from your original: facet_wrap is commented out, and file is moved to ggplot so that it doesn't have to be re-specified in each 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()

enter image description here enter image description here

0
votes

4 years later...

since ggplus is deprecated, you might consider using ggforce. You could use any of the relevant facet_* options as described in the documentation. E.g., facet_matrix:

# Standard use:
ggplot(mpg) +
  geom_point(aes(x = .panel_x, y = .panel_y)) +
  facet_matrix(vars(displ, cty, hwy))

enter image description here