1

Is there a way to use an apply function in R in order to create barplots with ggplot2?

Say, we have a dataframe containing only factor variables out of which one is boolean. In my case I have a dateframe with +40 variables. Can one plot all the variables against the boolean one with a single line of code?

  data("diamonds")
factors <- sapply(diamonds, function(x) is.factor(x))
factors_only <- diamonds[,factors]
factors_only$binary <- sample(c(1, 0), length(factors_only), replace=TRUE)
factors_only$binary <- as.factor(factors_only$binary)

But I want to create barplots like this one:

qplot(factors_only$color, data=factors_only, geom="bar", fill=factors_only$binary)

This does not work:

  sapply(factors_only,function(x) qplot(x, data=factors_only, geom="bar", fill=binary))

Please advise

lmo
  • 37,904
  • 9
  • 56
  • 69
  • you could reshape your data, and use `facet_` : `m = reshape2::melt(factors_only, id="binary") ; ggplot(m, aes(value, fill=binary)) + geom_bar() + facet_grid(. ~ variable, scale="free_x")` (but you will need to reorder your bars using `factor` on `m$value`: along these lines perhaps: `aes(factor(value, levels=unlist(lapply(factors_only, levels))), fill=binary)`) – user20650 Jan 27 '17 at 14:26

1 Answers1

5

You can use lapply to run through the variable names and then use get to pull up the current variable.

temp <- lapply(names(factors_only),
               function(x) qplot(get(x), data=factors_only, geom="bar", fill=binary, xlab=x))

To print a list item,

print(temp[[1]])

will produce

enter image description here

A nice feature of running through the variable names is that you can use these to dynamically name the labels in the figure.

lmo
  • 37,904
  • 9
  • 56
  • 69