R: saving ggplot2 plots in a list

前端 未结 2 527
不思量自难忘°
不思量自难忘° 2020-12-10 02:44

I am writing a R code that allows users to select columns from a data and plots histograms for each of them. Hence, I am using a \'for\' loop to generate the required number

2条回答
  •  天命终不由人
    2020-12-10 03:35

    Instead of mapping aesthetics using aes, you might be better off using aes_string:

     for(i in 1:length(u))
     {
       probabilityHistogram <- ggplot(plotData, aes_string(x=names(plotData)[i]))
       histogramList[[i]] <-  probabilityHistogram + geom_histogram(aes(y=..density..),     binwidth=bw, colour='black', fill='skyblue') + geom_density() + scale_x_continuous(names(plotData)[i]) + opts(legend.position='none')
     }
    

    That worked for me, at least. This avoids having to subset your data and allows you to reference the column you want to plot by quoted name.

提交回复
热议问题