Multiple Graphs Over Multiple Pages Using Ggplot

Plot over multiple pages

One option is to just plot, say, six levels of individual at a time using the same code you're using now. You'll just need to iterate it several times, once for each subset of your data. You haven't provided sample data, so here's an example using the Baseball data frame:

library(ggplot2)
library(vcd) # For the Baseball data
data(Baseball)

pdf("baseball.pdf", 7, 5)
for (i in seq(1, length(unique(Baseball$team87)), 6)) {
print(ggplot(Baseball[Baseball$team87 %in% levels(Baseball$team87)[i:(i+5)], ],
aes(hits86, sal87)) +
geom_point() +
facet_wrap(~ team87) +
scale_y_continuous(limits=c(0, max(Baseball$sal87, na.rm=TRUE))) +
scale_x_continuous(limits=c(0, max(Baseball$hits86))) +
theme_bw())
}
dev.off()

The code above will produce a PDF file with four pages of plots, each with six facets to a page. You can also create four separate PDF files, one for each group of six facets:

for (i in seq(1, length(unique(Baseball$team87)), 6)) {
pdf(paste0("baseball_",i,".pdf"), 7, 5)
...ggplot code...
dev.off()
}

Another option, if you need more flexibility, is to create a separate plot for each level (that is, each unique value) of the facetting variable and save all of the individual plots in a list. Then you can lay out any number of the plots on each page. That's probably overkill here, but here's an example where the flexibility comes in handy.

First, let's create all of the plots. We'll use team87 as our facetting column. So we want to make one plot for each level of team87. We'll do this by splitting the data by team87 and making a separate plot for each subset of the data.

In the code below, split splits the data into separate data frames for each level of team87. The lapply wrapper sequentially feeds each data subset into ggplot to create a plot for each team. We save the output in plist, a list of (in this case) 24 plots.

plist = lapply(split(Baseball, Baseball$team87), function(d) {
ggplot(d, aes(hits86, sal87)) +
geom_point() +
facet_wrap(~ team87) +
scale_y_continuous(limits=c(0, max(Baseball$sal87, na.rm=TRUE))) +
scale_x_continuous(limits=c(0, max(Baseball$hits86))) +
theme_bw() +
theme(plot.margin=unit(rep(0.4,4),"lines"),
axis.title=element_blank())
})

Now we'll lay out six plots at time in a PDF file. Below are two options, one with four separate PDF files, each with six plots, the other with a single four-page PDF file. I've also pasted in one of the plots at the bottom. We use grid.arrange to lay out the plots, including using the left and bottom arguments to add axis titles.

library(gridExtra)

# Four separate single-page PDF files, each with six plots
for (i in seq(1, length(plist), 6)) {
pdf(paste0("baseball_",i,".pdf"), 7, 5)
grid.arrange(grobs=plist[i:(i+5)],
ncol=3, left="Salary 1987", bottom="Hits 1986")
dev.off()
}

# Four pages of plots in one PDF file
pdf("baseball.pdf", 7, 5)
for (i in seq(1, length(plist), 6)) {
grid.arrange(grobs=plist[i:(i+5)],
ncol=3, left="Salary 1987", bottom="Hits 1986")
}
dev.off()

Multiple graphs over multiple pages using ggplot and marrangeGrob

You can use the ggplus package:

https://github.com/guiastrennec/ggplus

I particularly find it easier than messing with the ArrageGrobs/gridExtra; it automagically puts the facets in several pages for you.

Since you saved your initial plot as "p", your code would then look like:

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

ggplot2: Plotting graphs on multiple pages from a list and common legend

UPDATE:
Adding the following code to save the outputs of the graph works perfectly:

plots <- marrangeGrob(plist, nrow = 2, ncol = 2)

ggsave("multipage.pdf", plots, width = 11, height = 8.5, units = "in")

Save graphs (facet) on multiple pages

With the page argument you only specify

The page to draw (see ?facet_wrap_paginate)

That's why you get only the last or second page with page = i.

If you want all pages you have to loop over the pages:

library(ggplot2
library(ggforce)

i <- ceiling(
length(levels(df$Transect)) / 4) # set the number of pages

pdf("multi_page.pdf", width = 16 / 2.54, height = 12 / 2.54)
lapply(seq(i), function(page) {
SdesGG <- df %>% #launch each time or does not work
group_by(Transect) %>% #mandatory or need to fortify
ggplot(aes(x = oxygen, y = depth, color = Station)) +
geom_line() +
scale_color_brewer(palette = "Paired") +
scale_y_reverse() +
facet_wrap_paginate(~Transect, ncol = 2, nrow = 2, page = page) #ggforce
})
dev.off()

#> [[1]]

#> 
#> [[2]]

drawing multiple plots, 2 per page using ggplot

If you just need to output plots with two per page, then I would use gridExtra as was suggested above. You could do something like this if you were to put your ggplot objects into a list.

library(ggplot2)
library(shinipsum) # Just used to create random ggplot objects.
library(purrr)
library(gridExtra)

# Create some random ggplot objects.
ggplot_objects <- list(random_ggplot("line"), random_ggplot("line"))

# Create a list of names for the plots.
ggplot_objects_names <- c("This is Graph 1", "This is Graph 2")

# Use map2 to pass the ggplot objects and the list of names to the the plot titles, so that you can change them.
ggplot_objects_new <-
purrr::map2(
.x = ggplot_objects,
.y = ggplot_objects_names,
.f = function(x, y) {
x + ggtitle(y)
}
)

# Arrange each ggplot object to be 2 per page. Use marrangeGrob so that you can save two ggplot objects per page.
ggplot_arranged <-
gridExtra::marrangeGrob(ggplot_objects_new, nrow = 2, ncol = 1)

# Save as one pdf. Use scale here in order for the multi-plots to fit on each page.
ggsave("ggplot_arranged.pdf",
ggplot_arranged, scale = 1.5)

If you have a list of dataframes that you are wanting to create ggplots for, then you can use purrr::map to do that. You could do something like this:

purrr::map(df_list, function(x) {
ggplot(data = x, aes(x = aData, y = bData)) +
geom_point(color = "steelblue", shape = 19)
})

plotting multiple ggplots in a several page pdf (one or several plots per page)

The solution was actually pretty simple in the end...

### create a layout matrix (nrow and ncol will do the trick too, but you have less options)

layout_mat<-rbind(c(1,1,2),
c(1,1,3))

plots<-marrangeGrob(plot.list, layout_matrix=layout_mat)

ggsave( filename="mypdf.pdf", plots, width=29.7, height=21, units="cm")

This version actually gives you full control over plot sizes and uses the entire page!

multiple graph on different page and save as pdf

Making use of ggforce::facet_wrap_paginate you could do:

library(ggforce)
library(tibble)

co1<- tibble(age= c(10:14 ), pop=c(10,12,14,16,18), cn= c(10.1,12.1,14.25,16.09,18.3), country ="a")
co2<- tibble(age= c(10:14 ), pop=c(10.5,12.6,14.5,16.5,18.5), cn= c(10.6,12.5,14.3,16.7,18.6), country ="b")
co3<- tibble(age= c(10:14 ), pop=c(10.9,12.9,14.9,16.9,18.9), cn= c(11.9,13.9,15.9,17.9,19.9), country ="c")
df<- rbind(co1,co2,co3)

pdf("multi_page.pdf", width = 16 / 2.54, height = 12 / 2.54)
lapply(seq_along(unique(df$country)), function(page) {
ggplot(data=df, aes(x=age,group = country))+
# If you want a legend: Map on aesthetics!
geom_line(aes(y=pop, colour="pop"))+
geom_line(aes(y=cn,colour="cn"))+
# Set the colors via scale_xxx_manual
scale_color_manual(values = c(pop = "blue", cn = "red")) +
facet_wrap_paginate(~country, ncol = 1, nrow = 1, page = page) +
scale_y_continuous(labels = scales::comma)+
xlab("age") + ylab("population")
})
dev.off()

Plotting over multiple pages

I think the problem is that you need print() around your last line (p+ ...) to get it to actually print to the device inside the for loop . . .



Related Topics



Leave a reply



Submit