0

I have composed a function that creates histograms using ggplot2 of numeric columns of a dataframe and stores them to a list and returns the list.

However, although the plots are created correctly inside the function --as evidenced by print statements that can be placed inside the function for debugging purposes-- the list that is returned contains replicates of the same plot, which I find entirely bizarre and incomprehensible.

Here is my code:

hist_of_columns = function(data, class){
    library(ggplot2)
    library(ggthemes)

    data = as.data.frame(data)

    variables_numeric = names(data)[unlist(lapply(data, function(x){is.numeric(x) | is.integer(x)}))]

    indices = match(variables_numeric, names(data))
    index_of_class = match(class, names(data))

    list_of_plots = list()
    for (i in (1 : length(variables_numeric))){
         data_temp = data[, c(indices[i], index_of_class)]

          p  = ggplot(data_temp, aes(x= data_temp[, 1], color= data_temp[, 2], fill=data_temp[, 2])) +
           geom_histogram(aes(y=..density..), alpha=0.3,
           position="identity", bins = 100)+ theme_economist() +
           geom_density(alpha=.2) + xlab(names(data)[indices[i]]) + labs(fill = class) + guides(color = FALSE)

          # print(p)
          name = names(data)[indices[i]]

          list_of_plots[[name]] = p
          # print(list_of_plots[[name]])
    }

    return(list_of_plots)     
}

data(mtcars)
hist_of_columns(mtcars, 'am')
halfer
  • 19,824
  • 17
  • 99
  • 186
user8270077
  • 4,621
  • 17
  • 75
  • 140
  • It will be easier to answer this question if you simplify it a bit. Drop the function; just call everything directly on `mtcars`. By and large, though, you shouldn't need to write code like this at all. If you specify what sort of plots you're looking for, there's an easier way to get that result. – alistaire Jun 17 '18 at 15:50
  • If I'm reading this right, you can do more or less what you want with `library(tidyverse); mtcars %>% gather(variable, value, -am) %>% ggplot(aes(value, y = ..density.., fill = factor(am))) + geom_histogram(bins = 10) + facet_wrap(~variable, scales = 'free')` – alistaire Jun 17 '18 at 15:59
  • Evaluation issue, it would seem. Check this link https://stackoverflow.com/questions/31993704/storing-ggplot-objects-in-a-list-from-within-loop-in-r Possible duplicate. – Radim Jun 17 '18 at 16:07

0 Answers0