简体   繁体   English

在多个页面上绘图

[英]Plot over multiple pages

I have the facet_wrap function to make multiple graphs (n=~51) but they all appear on one page.我有facet_wrap函数来制作多个图形(n=~51),但它们都出现在一页上。 Now after searching, I found out that ggplot2 can't place graphs on multiple pages.现在搜索后,我发现ggplot2无法在多个页面上放置图形。

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.我查看了这个问题( 使用 ggplot 在多个页面上显示多个图形)并尝试了代码,但收效甚微。

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:这是我的图形代码,它在一页上生成约 51 个图形,使它们非常小且难以看到,如果我可以将其打印为 pdf 中每页 1 个图形,那就太好了:

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.如果有人能写出一小段代码并向我解释,那就太好了。

There are multiple ways to do the pagination: ggforce or gridExtra::marrangeGrob .有多种方法可以进行分页: ggforcegridExtra::marrangeGrob See also this answer for another example.另请参阅此答案以获取另一个示例。

ggforce:力量:

library(ggplot2)
# install.packages("ggforce")
library(ggforce)

# Standard facetting: too many small plots
ggplot(diamonds) +
  geom_point(aes(carat, price), alpha = 0.1) +
  facet_wrap(~cut:clarity, ncol = 3)

# Pagination: page 1
ggplot(diamonds) +
  geom_point(aes(carat, price), alpha = 0.1) +
  facet_wrap_paginate(~cut:clarity, ncol = 3, nrow = 3, page = 1)

# Pagination: page 2
ggplot(diamonds) +
  geom_point(aes(carat, price), alpha = 0.1) +
  facet_wrap_paginate(~cut:clarity, ncol = 3, nrow = 3, page = 2)

# Works with grid as well
ggplot(diamonds) +
  geom_point(aes(carat, price), alpha = 0.1) +
  facet_grid_paginate(color~cut:clarity, ncol = 3, nrow = 3, page = 4)

gridExtra:网格额外:

# install.packages("gridExtra")
library(gridExtra)

set.seed(123)
pl <- lapply(1:11, function(.x) 
  qplot(1:10, rnorm(10), main=paste("plot", .x)))

ml <- marrangeGrob(pl, nrow=2, ncol=2)

## non-interactive use, multipage pdf
## ggsave("multipage.pdf", ml)
## interactive use; calling `dev.new` multiple times
ml

Created on 2018-08-09 by the reprex package (v0.2.0.9000).reprex 包(v0.2.0.9000) 于 2018 年 8 月 9 日创建。

One option is to just plot, say, six levels of individual at a time using the same code you're using now.一种选择是使用您现在使用的相同代码一次绘制六个级别的individual 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:您尚未提供示例数据,因此以下是使用Baseball数据框的示例:

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.上面的代码将生成一个包含四页图的 PDF 文件,每页有六个面。 You can also create four separate PDF files, one for each group of six facets:您还可以创建四个单独的 PDF 文件,每组六个面一个:

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.我们将使用team87作为我们的分面列。 So we want to make one plot for each level of team87 .所以我们想为team87每个级别制作一个情节。 We'll do this by splitting the data by team87 and making a separate plot for each subset of the data.为此,我们team87拆分数据并为每个数据子集绘制单独的图。

In the code below, split splits the data into separate data frames for each level of team87 .在下面的代码中, split将数据拆分为team87每个级别的单独数据帧。 The lapply wrapper sequentially feeds each data subset into ggplot to create a plot for each team. lapply包装器按顺序将每个数据子集输入 ggplot 以创建每个团队的绘图。 We save the output in plist , a list of (in this case) 24 plots.我们将输出保存在plist ,这是一个(在本例中)24 个图的列表。

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.现在我们将在一个 PDF 文件中一次布置六个图。 Below are two options, one with four separate PDF files, each with six plots, the other with a single four-page PDF file.下面是两个选项,一个有四个单独的 PDF 文件,每个有六个绘图,另一个有一个四页的 PDF 文件。 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.我们使用grid.arrange来布置图,包括使用leftbottom参数来添加轴标题。

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

something like :类似的东西:

by(indbill, indbill$individual, function (x){
    ggplot(x, aes(x = prey, y = weight), tab) +
    geom_polygon(aes(group = load, color = capture), fill = NA, size = 0.75) +
    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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM