I’m a big fan of ggplot2. Recently, I ran into a situation which called for a useful feature that I had not used previously: aes_string.
Imagine that you have data consisting of observations for several variables – let’s say A, B, C – where each observation is from one of two groups – call them X and Y:
df1 <- data.frame(A = rnorm(50), B = rnorm(50),
C = rnorm(50), group = rep(LETTERS[24:25], 25))
head(df1)
# A B C group
# 1 0.2748922 -0.4805635 -1.80242191 X
# 2 0.0060852 -1.2972077 0.64262069 Y
# 3 0.1994655 -0.4628783 0.07670911 X
# 4 0.5416900 0.3853958 0.50193895 Y
# 5 0.3118773 0.9488503 -0.55855749 X
# 6 2.0924626 0.3027878 -0.03000122 Y
If you were interested in the distribution of variable A by group, you might generate a boxplot like so:
png("A.png", width = 800, height = 600)
print(ggplot(df1) + geom_boxplot(aes(group, A, fill = group)) + theme_bw())
dev.off()
|
Here, the arguments to aes() are expressions (group, A) which ggplot interprets as column names from the data frame.
What if you wanted to generate plots for each of variable A, B and C using a loop? You might start like this:
for(i in names(df1)[1:3]) # oh wait, these are characters not expressions # [1] "A" "B" "C"
You see the problem. How do we pass the column names which are characters, not expressions, to aes()?
The answer: use aes_string() instead.
Description:
Aesthetic mappings describe how variables in the data are mapped
to visual properties (aesthetics) of geoms. Compared to aes this
function operates on strings rather than expressions.
And so:
for(i in names(df1)[1:3]) {
png(paste(i, "png", sep = "."), width = 800, height = 600)
df2 <- df1[, c(i, "group")]
print(ggplot(df2) + geom_boxplot(aes_string(x = "group", y = i, fill = "group")) + theme_bw())
dev.off()
}
It’s a little ugly as it stands (better to write a function using one of the apply family). However, the key point is: you can pass data frame column names as expressions to aes() or as characters to aes_string().
With thanks to Hadley’s contribution to this mailing list thread.
Filed under: programming, R, research diary, statistics Tagged: aes, aes_string, ggplot2
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series,ecdf, trading) and more...


Zero Inflated Models and Generalized Linear Mixed Models with R.
Zuur, Saveliev, Ieno (2012).