0

I am plotting graphs over multiple pages, through the following code:

pdf(file = "ADAMean_By_Group1.pdf");
  d_ply(dacc, .(SIC), .print = TRUE, function(x){
      ggplot(x, aes(x = PER)) + 
      geom_line(aes(y = MEAN, group = NLEAD, color = NLEAD)) +
  })
  dev.off();

I want to reduce the number of pages, and have multiple pages of graph on same page. Let say, 4 graphs on same page (2 rows, and 2 columns). How can I do that?

I tried various methods, provided over internet, but can't make those to work. Please suggest a solution to this. I think it is somewhat tricky because of the presence of ddply.

Data dacc is provided below:

> dput(head(dacc[,c("SIC","NLEAD","MEAN","PER")],50))
structure(list(SIC = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
12, 12, 12, 12), NLEAD = c("0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "1", "1", "1"), MEAN = c(0.0412326905353457, 0.0451549825189391, 
0.048346878393873, 0.0549350094483683, 0.0730158407197195, 0.069153817795241, 
0.0505335160500995, 0.061317755985916, 0.0684815764449659, 0.0940169129245316, 
0.0582861931922358, 0.144592677844448, 0.0967616801627094, 0.219418938031889, 
0.029572456024009, 0.0426391121531116, 0.0891335203743842, 0.0371790985677946, 
0.27466936080368, 0.316047288322806, 0.399779612196661, 0.241800058717666, 
0.298782209496961, 0.280784586232505, 0.321683292079049, 0.286186440113042, 
0.440826914583147, 0.559706666532758, 0.256427311906362, 0.324337469833082, 
0.61264951851092, 0.249524284265047, 0.503753962899658, 0.314965901129344, 
0.329647650268623, 0.289924074761747, 0.409961187505677, 0.044341055684329, 
0.0298866866711071, 0.0494858483864846, 0.0252166835930562, 0.0474083585004927, 
0.0600282487190496, 0.0393457798051726, 0.0628251930948487, 0.0872080403831907, 
0.0494707607175366, 0.0525395728691009, 0.0407661644145909, 0.0631339860500073
), PER = c(0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0, 0.9, 
0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 
0.3, 0.2, 0.1, 0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 
0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0, 0.9, 0.8, 0.7
)), .Names = c("SIC", "NLEAD", "MEAN", "PER"), row.names = c(27L, 
173L, 217L, 14L, 227L, 147L, 172L, 79L, 73L, 62L, 241L, 101L, 
112L, 124L, 137L, 165L, 174L, 164L, 1035L, 837L, 675L, 434L, 
924L, 355L, 1103L, 588L, 1117L, 275L, 1126L, 490L, 909L, 673L, 
532L, 908L, 496L, 1003L, 261L, 1269L, 1231L, 1191L, 1206L, 1183L, 
1213L, 1202L, 1211L, 1145L, 1189L, 1259L, 1210L, 1240L), class = "data.frame")
Sumit
  • 2,242
  • 4
  • 25
  • 43
  • 2
    gridExtra::marrangeGrob has examples similar to this – baptiste Nov 22 '13 at 22:13
  • See e.g. [here](http://stackoverflow.com/questions/19059826/multiple-graphs-over-multiple-pages-using-ggplot/19063291#19063291), and [here](http://stackoverflow.com/questions/16025168/several-or-multiple-timeseries-plot-outputs-from-a-single-data-frame/16025851#16025851) – Henrik Nov 22 '13 at 22:38
  • @Henrik - Thanks. Can you guys point me to a page where I can learn how to have just one x-axis, y-axis and legend for a given page when there are multiple graphs using ggplot. I am still trying to find a solution to that. Thanks. – Sumit Nov 23 '13 at 14:45
  • try the `sub`, `bottom` and `legend` parameters of `arrangeGrob`; for the legend you will need to extract it from a plot. There are several examples here, using `ggplotGrob` and `gtable_filter`. – baptiste Nov 23 '13 at 17:01

1 Answers1

0

Just for sack of completeness, posting solution to my question. Might be useful to someone in future.

plots <- dlply(dacc, .(SIC), function(x){
      ggplot(x, aes(x = PER)) + 
      geom_line(aes(y = MEAN, group = NLEAD, color = NLEADMEAN))
    })

  ml <- do.call(marrangeGrob, c(plots, list(nrow = 2, ncol = 2)));
  ggsave("my_plots.pdf", ml, height = 7, width = 13, units = "in");
Sumit
  • 2,242
  • 4
  • 25
  • 43
  • Yes, I know. I posted my answers few hours ago. Will accept when the system allows me to. – Sumit Nov 23 '13 at 16:12