Dynamically Changing Variables to Plot with ggplot

I recently had a large dataset where I wanted to make a series of plots with different variables on the y-axis.  I remembered that a few years ago I great frustrated that I could not do this with ggplot, so I moved on.  This time, I was successful.

The key, as this Stack Overflow answer shows, is to pass the string for the column wrapped in UQ(as.name()). Below is the line from the function I made that does this:

results <- data.frame(data %>% group_by(graph_type, UQ(as.name(outfile))) %>% summarize(successDouble=mean(successDouble), successGreater35=mean(successGreater35), active_percent=mean(active_percent, na.rm=TRUE)))

outfile is the variable name.  Easy!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.