I often work with dataframes with "R-friendly"/"programmer-friendly" column names, typically with no spaces, and/or abbreviated (lazy to type the full names when doing the analysis). For example:
ir <- data.frame(
sp=iris$Species,
sep.len=iris$Sepal.Length,
sep.wid=iris$Sepal.Width,
pet.len=iris$Petal.Length,
pet.wid=iris$Petal.Width
)
When I plot these with ggplot I often want to replace the labels with "user-friendly" column names, e.g.
p <- ggplot(ir, aes(x=sep.len, y=sep.wid, col=sp)) + geom_point() +
xlab("sepal length") + ylab("sepal width") +
scale_color_discrete("species")
Question: Is there some way to specify label mappings to pass into ggplot ?
lazy.labels <- c(
sp ='species',
sep.len='sepal length',
sep.wid='sepal width',
pet.len='petal length',
pet.wid='petal width'
)
And do something like
p + labs(lazy.labels)
or even
p + xlab(lazy.labels[..x..]) + ylab(lazy.labels[..y..])
where ..x..
, ..y..
is some automagic ggplot variable holding the name of the current X/Y variable? (then I can put these annotations into a convenience function without having to change them for each graph)
This is particularly useful when I make many plots in reports. I can always rename ir
with the "user-friendly" columns, but then I have to do a lot of
ggplot(ir, aes(x=`sepal length`, y=`sepal width`, ...
which is a bit cumbersome because of all the spaces.
x.var <- 'sep.len'
and having ggplot interpret the variable name correctly assep.len
notx.var
(aes_string
can solve that problem for me). – mathematical.coffee