0

I use this loop to plot 96 different columns in a dataset, to visualise them in the same figure.

This plot was supposed to help with that, and it does create 96 different plots called "plot_1", "plot_2", "plot_3" etc.

However, all the plots are just a copy of the last column, and i simply cant seem to figure out why, i must be missing something..

Code is below:

for (i in 3:ncol(od)){
  print(i)
  p1<-ggplot(od, aes(x=time,y=od[,i]))+
    geom_point(color="tomato")+
    ggtitle(colnames(od)[i])+
    theme(plot.title=element_text(margin=margin(t=40,b=-40)),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank(),
          axis.text.y=element_blank(),
          axis.ticks.y=element_blank(),
          axis.title.y=element_blank(),
          axis.title.x=element_blank())
  assign(paste("plot_",i,sep=""), p1,envir = .GlobalEnv) 
}

Thanks so much in advance!

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    The `aes()` values are lazily evaluated on demand. Try `aes(x=time,y=od[, !!i])`. In the future it's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Leave off unimportant code like `theme()` to make the problem easier to see. – MrFlick Nov 10 '22 at 20:51

0 Answers0