I use R to loop through the columns of a data frame and create a graph of the resulting analysis. I do not get any errors when running the script, but it generates a PDF file that cannot be opened.
If I run the contents of the script, it works fine. I wondered if there was a problem with how quickly this happens, so I tried to get it to stop. It didn't seem to matter. I am interested in any suggestions that people have, and I am also completely new to R, so suggestions on how I can improve the approach are also welcome. Thanks.
for (i in 2:22) {
pop_den_z = subset(pop_den, pop_den[i] != "0")
y = pop_den_z[,i]
x = pop_den_z[,1]
y = log(y)
lm.0 = lm(formula = y ~ x)
inter = summary(lm.0)$coefficients[1,1]
slop = summary(lm.0)$coefficients[2,1]
a = c(i, inter, slop)
write(a, file = "C:/pop_den_coef.txt", ncolumns = 3, append = TRUE, sep = ",")
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"
)
p + geom_abline(intercept = inter, slope = slop, colour = "red", size = 1)
Sys.sleep(5)
dev.off()
}
source
share