简体   繁体   中英

for loop to create line plots for individual columns in r

I'm brand new to and can't seem to figure this out. I have a and need to each column as a its own unique line plot, and then output those plots to a csv. Here's what I have for the plotting, however when I simply run the code to create the plots nothing is outputted in .

for(i in 3:50){
  x <- data[,i]
  y <- data[,2]
  plot(x,type="l", main=names(data[i]), ylab="cases", xlab="month")
}

Does anyone have any suggestions on how to make this work? I just need to create individual plots for each column (each column corresponds to an individual country), and then output all of those plots onto one single .

Something like this should work (but you did not provide a sample of the data to test it):

pdf("myplots.pdf")
for(i in 3:50){
  x <- data[,i]
  y <- data[,2]
  plot(x, y, type="l", main=names(data)[i], ylab="cases", xlab="month")
}
dev.off()

The file will be in your current working directory ( getwd() ). Each page will be a separate plot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM