r - Asymmetric expansion of limits [ggplot2 2.0] -
how adjust expansion of limits asymmetrically in ggplot? example,
library(ggplot2) ggplot(mtcars) + geom_bar(aes(x = cyl), width = 1) i bottom of bars flush bottom of panel background, still space @ top. can achieve blank annotation:
ggplot(mtcars) + geom_bar(aes(x = cyl), width = 1) + annotate("blank", x = 4, y = 16) + scale_y_continuous(expand = c(0.0,0)) in previous versions of ggplot, however, use the solution provided rosen matev:
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 } and use scale_y_continuous(expand = list(c(0,0.1), c(0,0))) add consistently addition top of chart. in current version, however, error
ggplot(mtcars) + geom_bar(aes(x = cyl), width = 1) + scale_y_continuous(expand = list(c(0,0.1), c(0,0))) # error in diff(range) * mul : non-numeric argument binary operator is there effective solution ggplot2 2.0?
a solution should include ability work flexibly facets, , free_xy scale options. example,
ggplot(mtcars) + geom_bar(aes(x = cyl, fill = factor(vs)), width = 1) + facet_grid(vs ~ ., scales = "free_y") a solution should provide like:
ggplot(mtcars) + geom_bar(aes(x = cyl, fill = factor(vs)), width = 1) + facet_grid(vs ~ ., scales = "free_y") + scale_y_continuous(expand = c(0,0)) + geom_blank(data = data.frame(cyl = c(5,5), y = c(12, 16), vs = c(1,0)), aes(x = cyl, y = y))
i have tried add code ggplot2; see issue #1669 , corresponding pull request. if accepted, syntax expand argument been changed c(m, a) c(m_lower, a_lower, m_uppper, a_upper), specifying separate expansion values lower , upper range limits. (the old syntax still continue work, though, first 2 elements reused if elements 3 and/or 4 missing.)
with new syntax, can use
ggplot(mtcars) + geom_bar(aes(x = cyl), width = 1) + scale_y_continuous(expand = c(0, 0, 0.05, 0)) the result looks this:
it works facetting:
ggplot(mtcars) + geom_bar(aes(x = cyl, fill = factor(vs)), width = 1) + facet_grid(vs ~ ., scales = "free_y") + scale_y_continuous(expand = c(0, 0, 0.05, 0)) 





Comments
Post a Comment