将facet_wrap分割成多页PDF

2024-01-30

我已经四处寻找解决方案,但似乎大多数处理将单独生成的图合并为 PDF 格式,而不是将使用分面生成的图分离到 PDF 的单独页面上。

示例数据 https://drive.google.com/file/d/0B8KPGzjWWaw7V0s3UG0waVJIc0E/edit?usp=sharing

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

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 多个案例中使用它(它只返回标有列名称的条形图,没有可读的图表)。

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

我可以通过突出显示 4 个案例 +“项目”、“总计”和“管理”并重复接下来的 4 个案例并合并生成的 PDF 来完成此操作。然而,在实践中,如果案例超过 500 个,这将意味着超过 100 次迭代,并且很可能出现人为错误。如果能帮助流程自动化,那就太好了。


因为还没有答案。这是一个:

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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将facet_wrap分割成多页PDF 的相关文章

随机推荐