p1 <- ggplot(mpg, aes(cty, hwy)) + geom_point()
p1 + facet_grid(cyl ~ class, labeller=label_both)
p1 + facet_grid(cyl ~ class, labeller=labeller(cyl=label_both))
ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point() +
facet_grid(vs + am ~ gear, margins=TRUE,
labeller=labeller(vs=label_both, am=label_both))
capitalize <- function(string) {
substr(string, 1, 1) <- toupper(substr(string, 1, 1))
string
}
conservation_status <- c('cd'='Conservation Dependent',
'en'='Endangered',
'lc'='Least concern',
'nt'='Near Threatened',
'vu'='Vulnerable',
'domesticated'='Domesticated')
## Source: http://en.wikipedia.org/wiki/Wikipedia:Conservation_status
p2 <- ggplot(msleep, aes(x=sleep_total, y=awake)) + geom_point()
p2 + facet_grid(vore ~ conservation, labeller = labeller(vore = capitalize))
p2 + facet_grid(vore ~ conservation,
labeller=labeller(vore = capitalize, conservation = conservation_status ))
# We could of course have renamed the levels;
# then we can apply another nifty function
msleep$conservation2 <- plyr::revalue(msleep$conservation, conservation_status)
p2 %+% msleep +
facet_grid(vore ~ conservation2, labeller = labeller(vore = capitalize))
p2 %+% msleep +
facet_grid(vore ~ conservation2, labeller = labeller(conservation2 =
label_wrap_gen(10)))
Run the code above in your browser using DataLab