I have a big table of data with ~150 columns. I need to make a series of histograms out of about 1/3rd of them. Rather than putting 50 lines of the same plot command in my script, I want to loop over a list telling me which columns to use. Here is a test dataset to illustrate:
d <- data.frame(c(rep("A",5), rep("B",5)),
sample(c(1:10), 10, replace=TRUE),
sample(c(1:10), 10, replace=TRUE),
sample(c(1:10), 10, replace=TRUE),
sample(c(1:10), 10, replace=TRUE),
sample(c(1:10), 10, replace=TRUE))
colnames(d) <- c("col1","col2","col3","col4","col5","col6" )
ggplot(data=d, aes(col2, fill= col1)) + geom_density(alpha = 0.5)
So, rather than writing this a 50 times and replacing the aes() values, I really want to do something more like this...
cols_to_plot <- c("col2","col4","col6")
for (i in length(cols_to_plot)) {
ggplot(data=d, aes(cols_to_plot[i], fill= col1)) + geom_density(alpha = 0.5)
}
But of course, this doesn't work... Is there a way to do this kind of thing?
Thanks!