r - How to expand axis asymmetrically with ggplot2 without setting limits manually? -
i want bottom axis barplot zero, top expanded small amount (according scale). however, plotting output of stat_summary not have maximum value available me, , using facets scale="free_y" cannot manually set them. otherwise use baptiste's trick of adding geom_blank. know read off values , create data frame upper bounds manually, can set without having hard-code limits?
the other option compute means outside of function call , plot without calling stat_summary (this way have upper bounds directly each facet), there way around this?
example:
ggplot(mtcars)+ stat_summary(aes(cyl,mpg), fun.y="mean", geom="bar")+ scale_y_continuous(expand=c(0,0))+ facet_grid(carb~.,"free_y")
you can "extend" ggplot creating scale custom class , implementing internal s3 method scale_dimension so:
library("scales") scale_dimension.custom_expand <- function(scale, expand = ggplot2:::scale_expand(scale)) { expand_range(ggplot2:::scale_limits(scale), expand[[1]], expand[[2]]) } scale_y_continuous <- function(...) { s <- ggplot2::scale_y_continuous(...) class(s) <- c('custom_expand', class(s)) s } this example of overriding default scale. now, desired result specify expand so
qplot(1:10, 1:10) + scale_y_continuous(expand=list(c(0,0.1), c(0,0))) where first vector in list multiplicative factor, second additive part , first (second) element of each vector corresponds lower (upper) boundary.
Comments
Post a Comment