Learn R Programming

Kmisc (version 0.5.0)

labeller: ggplot2 labeller

Description

This function works as a labelling mapper for ggplot2, typically used in facet_grid. All arguments must be named. Items are mapped as name => value, where name represents the original levels of the factor used for facetting.

Usage

labeller(..., .parse = TRUE)

Arguments

...
A set of named arguments.
.parse
boolean; if TRUE we parse the text as though it were an expression.

Examples

Run this code
if (require(ggplot2)) {

  df <- data.frame(
    x=1:100,
    y=rnorm(100),
    grp=rep( c("tau+", "tau-"), each=50 ) ## levels are "tau+", "tau-"
  )

  f <- labeller(
    `tau-` = 'tau["-"]',
    `tau+` = 'tau["+"]'
  )

  ggplot(df, aes(x=x, y=y)) +
    geom_point() +
    facet_grid(". ~ grp", labeller=f)

  df$grp2 <- factor(rep( c("beta+", "beta-"), each=50 ))

  f <- labeller(
    `tau-` = 'tau["-"]',
    `tau+` = 'tau["+"]',
    `beta+` = 'beta["+"]',
    `beta-` = 'beta["-"]'
  )

  ggplot(df, aes(x=x, y=y)) +
    geom_point() +
    facet_grid("grp ~ grp2", labeller=f)
}

Run the code above in your browser using DataLab