Skip to content

Instantly share code, notes, and snippets.

@padpadpadpad
Last active March 28, 2019 07:49
Show Gist options
  • Save padpadpadpad/dc1f4520e4f9530b2c70fed0e4a425e9 to your computer and use it in GitHub Desktop.
Save padpadpadpad/dc1f4520e4f9530b2c70fed0e4a425e9 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