r - qplot call overwrites list elements -


running r script

list1<-list() list2<-list()  for(i in 1:3){   list1[[i]]<-i }  for(i in 1:3){   list2[[i]]<-qplot(i) } 

i recognize list1 contains elements 1,2,3. list2 contains 3 times element qplot(3).

is qplot not compatible looping? how can save plots in list using loop?

in ggplot aesthetics stored expressions , evaluated when plot rendered. qplot(i) not generate plot, rather plot definition, using reference variable i. 3 plots same in sense reference i.

if type

list2[[1]] 

after second loop has run, cause ggplot object stored in list2[[1]] rendered, using whatever value i set @ moment (which 3 after loop).

try this:

i <- 4 list2[[1]] 

now plot rendered equivalent qplot(4).

the workaround depends on trying achieve. basic idea not use external variables in aesthetics. in trivial case,

for(i in 1:3){   list2[[i]]<-ggplot(data.frame(x=i), aes(x))+geom_histogram() } 

will work. because reference external variable i not in aesthetics (e.g., call aes(...).


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -