Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key-value mapping of axis/variable labels in ggplot

Tags:

r

ggplot2

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.

like image 378
mathematical.coffee Avatar asked Jul 09 '18 03:07

mathematical.coffee


2 Answers

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)
like image 79
TC Zhang Avatar answered Sep 29 '22 13:09

TC Zhang


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).

like image 31
cderv Avatar answered Sep 29 '22 12:09

cderv