Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non standard evaluation, lapply, and ggplot

Tags:

r

ggplot2

nse

I'm trying to programmatically plot distributions using ggplot2.

I can't figure out how to work with non standard evaluation (NSE) here (even after having read Hadley's book chapter etc. on NSE).

Consider the following code:

library(ggplot2)


gg_dens <- function(x){
  eval(ggplot(data.frame(x), aes_string(x = substitute(x))) + geom_density() +
         ggtitle(substitute(x)), data.frame(x))
}


lapply(mtcars, function(x) gg_dens(x))

This code does produce a number of density plots, one for each column, ok. However, it does not print the name of the variable being plotted. Rather, the placeholder variable x is printed (cf. figure).

plot resulting from code above

My goal is to have the x quote substituted by the real variable name, eg., mpg.

like image 574
Sebastian Sauer Avatar asked Oct 30 '22 00:10

Sebastian Sauer


1 Answers

lapply is not going to work with the function you have right now to solve this. x is simply a vector when passed to that function, it is not the name of that variable, and lapply isn't passing anything that is the name. In other words, there is nothing in the scope of that function for it to figure out what should be the proper x-axis label.

One solution is similar to @Jimbou:

gg_dens <- function(name, dat) {
  ggplot(dat, aes_string(x = name)) + geom_density() + ggtitle(name)
}    
lapply(names(mtcars), gg_dens, mtcars)

Or just use facets instead:

mtcars2 <- tidyr::gather(mtcars)
ggplot(mtcars2, aes(value)) + geom_density() + facet_wrap(~key, scales = 'free')
like image 56
Axeman Avatar answered Nov 15 '22 05:11

Axeman