【问题标题】:ddply, ggplot, and multiple plots on same pageddply、ggplot 和同一页面上的多个绘图
【发布时间】:2013-11-22 22:08:45
【问题描述】:

我正在通过以下代码在多个页面上绘制图表:

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();

我想减少页数,并在同一页上有多页图表。比方说,同一页上有 4 个图表(2 行和 2 列)。我怎样才能做到这一点?

我尝试了各种通过 Internet 提供的方法,但无法使它们起作用。请提出解决方案。我认为由于 ddply 的存在,这有点棘手。

数据dacc如下:

> 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")

【问题讨论】:

  • gridExtra::marrangeGrob 有类似的例子
  • 参见例如herehere
  • @Henrik - 谢谢。你们能不能给我指出一个页面,在那里我可以学习如何在有多个使用 ggplot 的图表时为给定页面设置一个 x 轴、y 轴和图例。我仍在努力寻找解决方案。谢谢。
  • 试试arrangeGrobsubbottomlegend参数;对于图例,您需要从情节中提取它。这里有几个例子,使用ggplotGrobgtable_filter

标签: r ggplot2 plyr


【解决方案1】:

只是为了完整性,发布我的问题的解决方案。将来可能对某人有用。

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");

【讨论】:

  • 是的,我知道。几个小时前我发布了我的答案。当系统允许我接受时会接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 2020-01-27
  • 1970-01-01
相关资源
最近更新 更多