Skip to contents

Add axis labels that show facetting variable

Usage

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

Arguments

p

ggplot2 object to add facet labels to

xfacet_label

label of facet along x axis (nothing created if NULL), if vector, will be concatenated with " & "

yfacet_label

label of facet along y axis (nothing created if NULL), if vector, will be concatenated with " & "

Value

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

Examples

# we put donttest to avoid strictr error with seq along.with argument
# \donttest{
library(ggplot2)
library(grid)

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

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

# }