3
我使用R循環遍歷數據框的列並生成分析結果圖。腳本運行時,我不會收到任何錯誤,但會生成無法打開的pdf。使用R中的腳本生成pdf時出錯使用R中的腳本
如果我運行腳本的內容,它工作正常。我想知道是否有一個問題,它循環的速度有多快,所以我試圖強迫它暫停。這似乎沒有什麼區別。我對任何人的建議都很感興趣,而且我對R也很陌生,所以我建議如何改進這種方法也值得歡迎。謝謝。
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()
}
這只是格和ggplot2,並沒有真正與網格有關。 – hadley 2009-11-25 17:44:38
謝謝哈德利。修復。 – 2009-11-25 19:04:46
謝謝你,工作。我沒有意識到它需要明確打印。 – womble 2009-11-25 19:19:29