我已经搜索了这个网站和其他人的答案,似乎无法让代码的PDF部分工作,非常感谢帮助。
此代码工作正常,它循环并为RStudio输出中的每个行业创建绘图:
gg <- list()
#make the plots, facet by client on each page - works well
for (p in 1:length(df)){
gg[[p]] <- ggplot(data = df[[p]], aes(x = MonthsActive, y = Participation, color = CommClient)) +
ylim(0,1) + geom_line(size = 0.8) +
scale_x_continuous(limits = c(1,13)) +
facet_wrap(~ClientName, scales="fixed") +
scale_color_hue(l = 45) +
ggtitle(sprintf("Participation Rate for %s for First Year",params[p]))
plot(gg[[p]])
}
现在,当我将PDF函数包装起来时,我无法输出它。我已经测试了目标路径(Windows系统),打印时看起来没问题。有一次,我得到了空白的不可读的PDF,所以路径似乎有效。此代码不会创建单独的PDF:
gg <- list()
#make the plots, facet by client on each page
for (p in 1:length(df)){
#set the file path by name - when using print looks fine
myPath <- file.path("Q:","DataScience", "ParticipationPlots", paste(params[p], ".pdf", sep=""))
#set pdf as device and make individual PDFs
pdf(file = myPath, onefile = F, paper = "USr", width = 11, height = 8.5)
#this code is the same as above that works except for dev.off() at end
gg[[p]] <- ggplot(data = df[[p]], aes(x = MonthsActive, y = Participation, color = CommClient)) +
ylim(0,1) + geom_line(size = 0.8) +
scale_x_continuous(limits = c(1,13)) +
facet_wrap(~ClientName, scales="fixed") +
scale_color_hue(l = 45) +
ggtitle(sprintf("Participation Rate for %s for First Year",params[p]))
plot(gg[[p]])
}
dev.off()
答案 0 :(得分:3)
dev.off()需要在循环内部