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) 

enter image description here

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))  

enter image description here

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") 

enter image description here

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)) 

enter image description here

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:

simple bar chart

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)) 

bar chart facetting


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -