将facet_wrap分割成多页PDF [英] Segment facet_wrap into multi-page PDF

查看:122
本文介绍了将facet_wrap分割成多页PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决方案,但是似乎大多数处理将单独生成的图合并为PDF格式,而不是将通过分面生成的图分离到PDF的不同页面上.

I have searched around for a solution to this, but it appears that most deal with individually generated plots being combined into PDF format, rather than separating out plots generated using faceting onto separate pages of a PDF.

示例数据

在上面的数据中使用以下代码,选择生成的列表中的所有项目:

In the above data using the following code, selecting all items in the generated list:

Ex<-read.csv("StackOverflowEx (3).csv")
library(ggplot2)
library(reshape2)
vars <- select.list(names(Ex),multiple=TRUE,graphics=TRUE)
Cases<-subset(Ex,select=vars)

gg<-melt(Cases,id=c("Item","Total","Admin"))
print(ggplot(gg, aes(x=Total,y=Admin))+
  geom_point(colour="dark green",size=1.5)+
  geom_point(aes(y=value,color=variable))+
  geom_smooth(aes(y=value,fill=variable),
              method=loess,size=1,linetype=1,se=T)+
  facet_wrap(~variable,ncol=2,nrow=1000)+
  ylim(0,1)+
  labs(x="Expected",y="Admin",title=vars))

...应生成所有8个(A-H)案例的分面换行.但是,生成该图趋于使图变得局促,并使图的可读性降低,并且在实践中,我打算在500多种情况下使用此图(这种情况只会返回标有列名且没有可读图表的条形图).

...should generate a facet wrap of all 8 (A-H) Cases. However, generating this tends to cramp the plots and make them less readable, and in practice, I intend to use this on 500+ cases (which just returns bars labeled with the column name with no readable charts).

在转换为PDF时,是否可以指定刻面中要显示在单个页面上的图表数量,而不是将所有图都压缩到单个页面中?例如,使用上述数据,在分别包含全部8个案例的独立页面上生成两个2x2绘图(即第1页的案例A-D,第2页的案例E-H).

Is it possible to specify the number of charts in the faceting to appear on an individual page when converting to PDF, rather than having all plots compress into a single page? For example, using the above data, generating two 2x2 plots on separate pages that contain all 8 cases individually (i.e. Cases A-D on pg 1, Cases E-H on pg 2).

我可以通过突出显示4个案例+项目",总计"和"管理员"并在接下来的4种情况下重复上述操作,并合并生成的PDF.但是,在实践中,如果有500多个案例,这意味着将进行100多次迭代,并且有很多潜在的人为错误.自动化过程的一些帮助将非常有用.

I could do this by highlighting 4 cases + "Item", "Total" & "Admin" and repeating for the next 4 cases and combining the resultant PDFs. However with upwards of 500 cases in practice this would mean more than 100 iterations with lots of potential for human error. Some help with automating the process would be great.

推荐答案

由于尚无答案.这是一个:

As there is no answer yet. Here is one:

library(ggplot2)
library(reshape2)

Ex <- read.csv("C:/Users/Thomas/Desktop/StackOverflowEx (3).csv")
gg <- melt(Ex,  id = c("Item", "Total", "Admin"))

# put in number of plots here
noPlots <- 4
# save all variables in a seperate vector to select in for-loop
allVars <- unique(gg$variable)
noVars <- length(allVars)

# indices for plotting variables
plotSequence <- c(seq(0, noVars-1, by = noPlots), noVars)

# pdf("plotpath.pdf") # uncomment to save the resulting plots in a pdf file
# loop over the variables to plot
for(ii in 2:length(plotSequence)){
  # select start and end of variables to plot
  start <- plotSequence[ii-1] + 1
  end <- plotSequence[ii]

  # subset the variables and save new temporary data.frame
  tmp <- subset(gg, variable %in% allVars[start:end])
  cat(unique(tmp$variable), "\n")

  # generate plot
  p <- ggplot(tmp,  aes(x = Total, y = Admin))+
    geom_point(colour = "dark green", size = 1.5)+
    geom_point(aes(y = value, color = variable))+
    geom_smooth(aes(y = value, fill = variable), 
                method = loess, size = 1, linetype = 1, se = T)+
    facet_wrap(~variable, ncol = 2, nrow = 1000)+
    ylim(0, 1)+
    labs(x = "Expected",  y = "Admin")
  print(p)
}
# dev.off()

这篇关于将facet_wrap分割成多页PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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