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')