我到处寻找解决方案,但似乎大多数都是将单独生成的绘图合并为PDF格式,而不是将使用分面生成的绘图分离到PDF的单独页面上。
在上述数据中,使用以下代码,选择生成列表中的所有项目:
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))
...should生成所有8个(A-H)案例的小平面包裹。但是,生成这种方法往往会使绘图变得不直观,并降低其可读性,实际上,我打算在500+情况下使用这种方法(它只返回标有列名的条形图,而没有可读性图表)。
在转换为PDF时,是否可以指定分面中的图表数量显示在单个页面上,而不是将所有绘图压缩到一个页面中?例如,使用上面的数据,在单独的页面上生成两个2x2绘图,分别包含所有8个案例(即pg 1上的案例A-D,pg 2上的案例E-H )。
我可以通过突出显示4个案例+ "Item","Total“和"Admin”,重复下面4个案例,并组合生成的PDF来做到这一点。然而,在实践中超过500例的情况下,这将意味着超过100次迭代,有很大的潜在的人为错误。如果能帮助你自动化这个过程,那就更好了。
发布于 2015-04-28 04:20:23
因为现在还没有答案。这里有一个:
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()
https://stackoverflow.com/questions/22996911
复制相似问题