Last active
January 12, 2018 05:58
-
-
Save Ram-N/91b08a2cecb081ff45af3cf17de88aee to your computer and use it in GitHub Desktop.
Adding Annotations to Facet_Grid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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