Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Last active January 12, 2018 05:58
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 Ram-N/91b08a2cecb081ff45af3cf17de88aee to your computer and use it in GitHub Desktop.
Save Ram-N/91b08a2cecb081ff45af3cf17de88aee to your computer and use it in GitHub Desktop.
Adding Annotations to Facet_Grid
mtcars[, c("cyl", "am", "gear")] <- lapply(mtcars[, c("cyl", "am", "gear")], as.factor)
p <- ggplot(mtcars, aes(mpg, wt, group = cyl)) +
geom_line(aes(color=cyl)) +
geom_point(aes(shape=cyl)) +
facet_grid(gear ~ am) +
theme_bw()
p
len <- length(levels(mtcars$gear)) * length(levels(mtcars$am))
vars <- data.frame(expand.grid(levels(mtcars$gear), levels(mtcars$am)))
colnames(vars) <- c("gear", "am")
dat <- data.frame(x = rep(15, len), y = rep(5, len), vars, labs=LETTERS[1:len])
p + geom_text(aes(x, y, label=labs, group=NULL),data=dat)
dat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment