ggplot2:绘制在多个页面上 [英] ggplot2: Plots over Multiple pages

查看:96
本文介绍了ggplot2:绘制在多个页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggplot2来绘制图表,并且我有 facet_wrap 函数来制作多个图表(〜51),但它们都出现在一个页面上。现在搜索后,我发现ggplot2不能在多个页面上放置图形。



有没有办法做到这一点?我看了这个问题(使用ggplot的多个页面上的多个图)并尝试了代码,但很少成功。



这是我的图形代码,它在一页上生成大约51个图形,使得它们非常小且难以看清,如果我可以按照每个图表这是很棒的:

  ggplot(indbill,aes(x =猎物,y =体重),tab )+ 
geom_polygon(aes(group = load,color = capture),fill = NA,size = 0.75)+
facet_wrap(〜individual)+
theme(axis.ticks.x = element_blank(),
axis.text.x = element_text(size = rel(0.5)),
axis.ticks.y = element_blank(),
axis.text.y = element_blank ))+
xlab()+ ylab()+
guides(color = guide_legend(ncol = 2))+
coord_radar()

$ b

如果有人可以写一些代码并向我解释,那就太好了!



谢谢!

解决方案

其中一种方法是只绘制六个级别的个人一次使用您现在使用的相同代码。你只需要迭代几次,每次数据的一个子集。你还没有提供样本数据,所以这里是一个使用 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()

以上代码将生成一个包含四页的情节,每个页面都有六个方面。您还可以创建四个独立的PDF文件,每个包含六个方面的一个文件:

  for(i in seq(1,length (独特(Baseball $ team87)),6)){
pdf(paste0(棒球_,我,pdf),7,5)
... ggplot code ...
dev.off()
}

另一种选择是,如果您需要更多的灵活性,是为每个级别(即每个唯一值)创建一个单独的绘图变量并将所有单个绘图保存在列表中。然后,您可以在每个页面上布置任意数量的图。这可能是矫枉过正的,但这里是一个例子,灵活性派上用场。



首先,我们来创建所有的图。我们将使用 team87 作为我们的构面栏。所以我们希望为每个 team87 级别制作一个情节。我们将通过将数据分成 team87 并为数据的每个子集制作一个单独的图来实现。



在下面的代码中, split 将数据拆分为 team87 的每个级别的单独数据框。 lapply wrapper按顺序将每个数据子集提供给ggplot,为每个团队创建一个图。我们将输出结果保存在 plist 中,(本例中)为24个图表。

  plist = lapply(split(Baseball,Baseball $ team87),function(d){
ggplot(d,aes(hits86,sal87))+
geom_point()+ $ b $ (限制= c(0,max(棒球$ sal87,na.rm = TRUE)))+
scale_x_continuous(限制= c(0,max(Baseball $ hits86)))+
theme_bw()+
theme(plot.margin = unit(rep(0.4,4),lines),
axis.title = element_blank())
$)

现在我们将在PDF文件中绘制六张图。以下是两个选项,其中一个包含四个单独的PDF文件,每个包含六个图表,另一个包含一个四页PDF文件。我也粘贴了底部的一块地块。我们使用> grid.arrange 来绘制图,包括使用 left bottom 参数添加轴标题。

  library(gridExtra)

#Four (1,长度(plist),6)){
pdf(paste0(棒球_,我,pdf)的单独单页PDF文件,每个文件有6个图
。 ),7,5)
grid.arrange(grobs = plist [i:(i + 5)],
ncol = 3,left =薪水1987,bottom =Hits 1986)
dev.off()
}

#在一个PDF文件中有四页图表
pdf(baseball.pdf,7,5)
对于(i in seq(1,length(plist),6)){
grid.arrange(grobs = plist [i:(i + 5)],
ncol = 3,left =Salary 1987),bottom =Hits 1986)
}
dev.off()


I'm plotting graphs using ggplot2 and I have the facet_wrap function to make multiple graphs (~51) but they all appear on one page. Now after searching, I found out that ggplot2 can't place graphs on multiple pages.

Is there a way to do this? I looked at this question (Multiple graphs over multiple pages using ggplot) and tried out the code, with little success.

Here is my code for my graphs, it produces ~51 graphs on one page, making them very small and hard to see, if I could print this to 1 graph per page in a pdf, that would be great:

ggplot(indbill, aes(x = prey, y = weight), tab) +
geom_polygon(aes(group = load, color = capture), fill = NA, size = 0.75) +
facet_wrap(~ individual) +
theme(axis.ticks.x = element_blank(),
    axis.text.x = element_text(size=rel(0.5)),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank()) +
xlab("") + ylab("") +
guides(color = guide_legend(ncol=2)) +
coord_radar()

If someone could write up a little code and explain it to me, that would be great!

Thanks!

解决方案

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

这篇关于ggplot2:绘制在多个页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆