[英]Error when generating pdf using script in R
I'm using R to loop through the columns of a data frame and make a graph of the resulting analysis. 我正在使用R循环遍历数据框的列,并绘制结果分析图。 I don't get any errors when the script runs, but it generates a pdf that cannot be opened.
脚本运行时我没有收到任何错误,但它生成了一个无法打开的pdf。
If I run the content of the script, it works fine. 如果我运行脚本的内容,它可以正常工作。 I wondered if there is a problem with how quickly it is looping through, so I tried to force it to pause.
我想知道循环的速度是否有问题,所以我试图强迫它暂停。 This did not seem to make a difference.
这似乎没有什么区别。 I'm interested in any suggestions that people have, and I'm also quite new to R so suggestions as to how I can improve the approach are welcome too.
我对人们有任何建议感兴趣,而且我对R也很陌生,所以我也欢迎如何改进方法的建议。 Thanks.
谢谢。
for (i in 2:22) {
# Organise data
pop_den_z = subset(pop_den, pop_den[i] != "0") # Remove zeros
y = pop_den_z[,i] # Get y col
x = pop_den_z[,1] # get x col
y = log(y) # Log transform
# Regression
lm.0 = lm(formula = y ~ x) # make linear model
inter = summary(lm.0)$coefficients[1,1] # Get intercept
slop = summary(lm.0)$coefficients[2,1] # Get slope
# Write to File
a = c(i, inter, slop)
write(a, file = "C:/pop_den_coef.txt", ncolumns = 3, append = TRUE, sep = ",")
## Setup pdf
string = paste("C:/LEED/results/Images/R_graphs/Pop_den", paste(i-2), "City.pdf")
pdf(string, height = 6, width = 9)
p <- qplot(
x, y,
xlab = "Radius [km]",
ylab = "Population Density [log(people/km)]",
xlim = x_range,
main = "Analysis of Cities"
)
# geom_abline(intercept,slope)
p + geom_abline(intercept = inter, slope = slop, colour = "red", size = 1)
Sys.sleep(5)
### close the PDF file
dev.off()
}
The line should be 这条线应该是
print(p + geom_abline(intercept = inter, slope = slop, colour = "red", size = 1))
In pdf devices, ggplot (and lattice) only writes to file when explicitly printed. 在pdf设备中,ggplot(和lattice)仅在显式打印时写入文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.