Skip to content

Instantly share code, notes, and snippets.

@briatte
Forked from padpadpadpad/label_facets_ggplot2.R
Created January 9, 2019 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briatte/fb6e272bed090a1f7df60378481964a4 to your computer and use it in GitHub Desktop.
Save briatte/fb6e272bed090a1f7df60378481964a4 to your computer and use it in GitHub Desktop.
function to label facets with letters in ggplot2
# load package
library(ggplot2)
# write function
label_facets <- function(string){
len <- length(string)
string = paste('(', letters[1:len], ') ', string, sep = '')
return(string)
}
# draw example plots
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~manufacturer, labeller = labeller(manufacturer = label_facets))
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class, labeller = labeller(class = label_facets))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment