This series discusses how we can use ggplot2 to produce plots for each column
of a data frame that depend on characteristics of this column
(e.g. the class of a column) in a scalable manner.
To this end, we integrate the following concepts / functions:
- anonymous functions
- non-standard evaluation
- lexical scoping
The reader should be familiar with these concepts, otherwise, Hadley Wickham’s
Advanced R might be a good starting point to read
up on all but the first topic.
The approach discussed here generalizes to other situations in which one wants
to customize plots based on the characteristics of input data.
For this blog post, we are going to use a subset of the diamonds data set.
Now, imagine you want a visual summary for each variable. Unfortunately,
the variables are not all of the same class. Otherwise, you might transform the
data into long format and use facets.
For the factors, you could do a bar chart, for the numerical variables, you might want to
use a density plot. Let’s have a look at a first approach. You could do the
following for cut and color.
Similarly, you can do for price and carat
Now, you can note two issues:
- There is a lot of code duplication. For each plot, you need another line of
code that is almost identical to the ones you have already. This is not
scalable to data sets with many columns. This problem will be addressed in this
- You might want to further customize your plots. For
example, changing the x-axis of the density plots from linear to logarithmic
might be desirable to make better use of space. This problem will be addressed
in the second part of the series.
To address the first problem, we can create a function that
behaves differently depending on whether the input is factorial or numeric.
current_class is a function that magically gets the class of the variable
that you used in
ggplot. It will be explained at a later stage.
Having defined that function, you could rewrite the above as follows:
This is a slight improvement on the first solution because you always call the
same functions for all plots. Hence, we can kind of use an
apply approach to
reduce the redundancy of this problem. You might think of the following:
Unfortunately, this does not quite work because for each iteration in
g will be the actual
values from each column, but in
aes, you need the name of the column, not the
actual value. Since there is no way to get from the values to the names, but
if we have the names, we can get the values, the trick is to loop over the names
of the data frame.
However, we are not quite there yet. Due to non-standard evaluation, we need to
further change two things:
gis not actually g, but points to something
gis just the name of an object (i.e.
“cut” for the first iteration), not the object itself.
The only explanation I still owe you is how the function
It only works because it is called from within
Hence, the parent frame of
current_class (the function that calls
lapply as its parent. For a given iteration,
the value of g is available in the environment of
simply needs to go up the tree until it reaches the environment of
and get the value of g and figure out it’s class. That is done as follows.
Now, we are done. This is all code we need to get our solution.
Finally, we can plot the result.
In this blog post, a few advanced concepts from the R toolbox were integrated
in order to create column-wise visual data summaries. To this end, we
created a set of functions which can be used to generate plots for different
data types (numerical and factorial).
This set of functions can be used in conjunction with
create summary plots, which would not be possible if different functions had to
be called for the different data types. The solution presented above is
scalable to data sets with an arbitrary number of columns without altering the
We will expand on this by customizing the appearance of the plots further.
Namely, the second part of the series centers on using a log-transformed
x-scale for continuous data and how to generate appropriate breaks, but the
principles we will develop there can be generalized well to other customization