新手小白,为了完成毕业论文,从网上找的循环作图代码,改了一下之后运行就报错了,不知道问题出在哪里,还请各位大佬指点!
报错:错误: Aesthetics must be either length 1 or the same as the data (4): x
代码如下:
setwd("C:\\Users\\DELL\\Desktop")
library(ggplot2)
bohaibay <- read.table("C:/Users/DELL/Desktop/test217.csv", header = TRUE, sep = ",")
# Make plots.
plot_list = list()
for (i in 1:10) {
new <- bohaibay[which(bohaibay$id == i), ]
year = new[,3]
avg = new[,4]
p = ggplot(new, aes(x=year,y=avg)) +
geom_line()
plot_list[] = p
}
# Save plots to tiff. Makes a separate file for each plot.
for (i in 1:10) {
file_name = paste("bohaibay_plot_", i, ".tiff", sep="")
tiff(file_name)
print(plot_list[])
dev.off()
}
*在此处报错
# Another option: create pdf where each page is a separate plot.
pdf("plots9.pdf")
for (i in 1:10) {
print(plot_list[])
}
*在此处报错
dev.off()