11

I want to write a function that takes a dataframe, and graphs all the columns in that dataframe as histograms.

For a dataframe whose column names I know beforehand, I can write

qplot(colname1, data=df, geom='histogram')
qplot(colname2, data=df, geom='histogram')
...

but I want to do this generically, so that I can use the name of the column as a string "colname1".

In other words, how to write

plot_histogram_of_column <- function(df, colname) {
    # qplot(colname, data=df, geom='histogram') won't work
}

1 Answer 1

19

Use ggplot and aes_string. Something like this:

ggplot(data = df, aes_string(x = colname)) + geom_histogram()

aes_string was written precisely for this purpose.

Sign up to request clarification or add additional context in comments.

4 Comments

As documented here: had.co.nz/ggplot2/aes.html Note, though, that, as far as I can tell, NO page of the ggplot2 documentation site links to this, so the only way to find it is by knowing to search for it.
@Maxy-B The built-in docs (?aes) are much improved in version 0.9.0, and contain a link to the help page for aes_string. I can't recall if this was the case in 8.9.0.
Yes. I just independently solved my own problem related to this. This function is incredibly obscure and hard to discover, yet important. Surely one of the first things you would want to parameterize when plotting a large dataset is the identity of the y-series, yet the examples don't emphasisize this. (I edited your title for clarity)
The idiomatic way is now : ggplot(data = df, aes(x = !!ensym(colname))) + geom_histogram()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.