How to plot using facet_wrap, over multiple pages as a .pdf files in r cran

I am using ggplot, to compare 114 unique studies for a particular variable I'm interested in.

This is what I have used.

ggplot(steps, aes(x=factor(edu))) + geom_bar(aes(y = (..count..), group = id_study,)) + facet_wrap(~id_study,)

Whilst this works, all 114 studies are plotted on one page and the formatting is all squashed. How do I split this over 4x4 pages ?

Many thanks

S

edit ****

As there are 114 unique studies, I have 5 pages in total 
1)
ggplot(steps, aes(x=factor(edu))) + geom_bar(aes(y = (..count..), group = id_study,))+ facet_wrap_paginate(~id_study, nrow = 5, ncol = 5, scales
 = fixed, shrink = TRUE, labeller = label_value, as.table = TRUE, switch = NULL, drop = TRUE, dir = h, strip.position = top, page = 1)  

2)
ggplot(steps, aes(x=factor(edu))) + geom_bar(aes(y = (..count..), group = id_study,))+ facet_wrap_paginate(~id_study, nrow = 5, ncol = 5, scales
 = fixed, shrink = TRUE, labeller = label_value, as.table = TRUE, switch = NULL, drop = TRUE, dir = h, strip.position = top, page = 2)  

3)
ggplot(steps, aes(x=factor(edu))) + geom_bar(aes(y = (..count..), group = id_study,))+ facet_wrap_paginate(~id_study, nrow = 5, ncol = 5, scales
 = fixed, shrink = TRUE, labeller = label_value, as.table = TRUE, switch = NULL, drop = TRUE, dir = h, strip.position = top, page = 3) 

4)
 ggplot(steps, aes(x=factor(edu))) + geom_bar(aes(y = (..count..), group = id_study,))+ facet_wrap_paginate(~id_study, nrow = 5, ncol = 5, scales
 = fixed, shrink = TRUE, labeller = label_value, as.table = TRUE, switch = NULL, drop = TRUE, dir = h, strip.position = top, page = 4) 

5)

ggplot(steps, aes(x=factor(edu))) + geom_bar(aes(y = (..count..), group = id_study,))+ facet_wrap_paginate(~id_study, nrow = 5, ncol = 5, scales
 = fixed, shrink = TRUE, labeller = label_value, as.table = TRUE, switch = NULL, drop = TRUE, dir = h, strip.position = top, page = 5)

all of these into 1 page?

thank you

Topic plotting ggplot2 data r data-mining

Category Data Science


Use ggforce. It has paginate wrappers to the facet function. You specify how many plots per page, then calculate and call the function once for each page (you could do this programmatically as it also has a function to determine how many pages you need, then do a loop).


Depending on the shape of your plots, it might make sense to create four separate plots. I suggest this just because it makes sense to show the axes and labels on each individual page, otherwise it could be that the reader would have to always flips back to e.g. page 1 to see what the scales are.

I would probably do something like a 6 (rows) by 5 (columns) on each page, assuming portrait page orientation. Then you need to just adjust your code to do something like this (as pseudo-code):

page1 <- ggplot(steps[0:30]), aes(x=factor(edu))) + geom_bar(aes(y = (..count..), group = id_study,)) + facet_wrap(~id_study,)

page2 <- ggplot(steps[30:60]), aes(x=factor(edu))) + geom_bar(aes(y = (..count..), group = id_study,)) + facet_wrap(~id_study,)

page3 <- ggplot(steps[60:90]), aes(x=factor(edu))) + geom_bar(aes(y = (..count..), group = id_study,)) + facet_wrap(~id_study,)

page4 <- ggplot(steps[90:114]), aes(x=factor(edu))) + geom_bar(aes(y = (..count..), group = id_study,)) + facet_wrap(~id_study,)

Then continue to export the images as you require, for example:

ggsave('./first_page_plots.pdf', page1, dpi=600)

check out more save options here.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.