Learn R Programming

teal.modules.general (version 0.4.1)

add_facet_labels: Add labels for facets to a ggplot2 object

Description

Enhances a ggplot2 plot by adding labels that describe the faceting variables along the x and y axes.

Usage

add_facet_labels(p, xfacet_label = NULL, yfacet_label = NULL)

Value

Returns grid or grob object (to be drawn with grid.draw)

Arguments

p

(ggplot2) object to which facet labels will be added.

xfacet_label

(character) Label for the facet along the x-axis. If NULL, no label is added. If a vector, labels are joined with " & ".

yfacet_label

(character) Label for the facet along the y-axis. Similar behavior to xfacet_label.

Examples

Run this code
library(ggplot2)
library(grid)

p <- ggplot(mtcars) +
  aes(x = mpg, y = disp) +
  geom_point() +
  facet_grid(gear ~ cyl)

xfacet_label <- "cylinders"
yfacet_label <- "gear"
res <- add_facet_labels(p, xfacet_label, yfacet_label)
grid.newpage()
grid.draw(res)

grid.newpage()
grid.draw(add_facet_labels(p, xfacet_label = NULL, yfacet_label))
grid.newpage()
grid.draw(add_facet_labels(p, xfacet_label, yfacet_label = NULL))
grid.newpage()
grid.draw(add_facet_labels(p, xfacet_label = NULL, yfacet_label = NULL))

Run the code above in your browser using DataLab