Asymmetric expansion of ggplot axis limits

前端 未结 3 464
情深已故
情深已故 2020-12-03 01:16

How do you adjust the expansion of limits asymmetrically in ggplot? For example,

library(ggplot2)

ggplot(mtcars) + 
  geom_bar(aes(x = cyl), width = 1)
         


        
3条回答
  •  庸人自扰
    2020-12-03 01:32

    I have now tried to add code for this to ggplot2; see issue #1669 and the corresponding pull request. If it is accepted, the syntax for the expand argument will been changed from c(m, a) to c(m_lower, a_lower, m_uppper, a_upper), for specifying separate expansion values for the lower and upper range limits. (The old syntax will still continue to work, though, as the first two elements will be reused if elements three and/or four are missing.)

    With this new syntax, you can use

    ggplot(mtcars) +
      geom_bar(aes(x = cyl), width = 1) +
      scale_y_continuous(expand = c(0, 0, 0.05, 0))
    

    The result looks like this:

    It also works with 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))
    

提交回复
热议问题