0

I have this example and was trying to diagnose why it doesn't.

df = data.table(one = rnorm(50),
                two = rnorm(50),
                thr = rnorm(50))

So for this data table, I want to plot histograms of two columns within a loop. This is done with the following.

these_vars = c("one","two")
for(var in these_vars){
   df[,..var] %>% ggplot(., aes_string(var)) + 
      geom_histogram() + ggtitle(var)
}

However, this doesn't seem to work as the plots aren't rendered. Can anyone help diagnose the issue with this little process.

ATMA
  • 1,450
  • 4
  • 23
  • 33

1 Answers1

1

You will have to use plot explicitly in a for loop:

for(var in these_vars){
    p <- df[,..var] %>% ggplot(., aes_string(var)) + 
        geom_histogram() + ggtitle(var)
    plot(p)
}
GL_Li
  • 1,758
  • 1
  • 11
  • 25