7
votes

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.

2
Is this something you're looking for stackoverflow.com/a/49943976/786542Tung
@Tung that's a similar but not the same issue - that's to do with saving the variable name like x.var <- 'sep.len' and having ggplot interpret the variable name correctly as sep.len not x.var (aes_string can solve that problem for me).mathematical.coffee

2 Answers

5
votes

I digged into the ggplot object and came up with this: the benefit is that you do not need to know the mapping in advance

library(ggplot2)

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
)

p <- ggplot(ir, aes(x=sep.len, y=sep.wid, col=sp)) +
     geom_point() +
     scale_color_discrete("species")


## for lazy labels

lazy.labels <- c(
  sp     ='species',
  sep.len='sepal length',
  sep.wid='sepal width',
  pet.len='petal length',
  pet.wid='petal width'
)

p$labels <-lapply(p$labels,function(x){as.character(lazy.labels[x])})

Or, using a function:

plot_with_labels <- function(p, l) {
  p$labels <- lapply(p$labels, function(x) { as.character(l[x]) } )
  return(p)
}

plot_with_labels(p, lazy.labels)
1
votes

One solution if your graph are always the same is to create labels in advance using a list mapping aesthetics to your new names to replace the default one. Then you can use labs(lazy.labels).

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
)
library(ggplot2)
# mapping aesthetics names to labels
lazy.labels <- list(
  col = 'species',
  x = 'sepal length',
  y ='sepal width'
)
p <- ggplot(ir, aes(x = sep.len, y = sep.wid, col = sp)) + 
  geom_point() +
  labs(lazy.labels)

Created on 2018-07-09 by the reprex package (v0.2.0).