【问题标题】:Force the origin to start at 0 without margin between data and x axis in new ggplot theme强制原点从 0 开始,在新的 ggplot 主题中数据和 x 轴之间没有边距
【发布时间】:2020-09-10 03:55:43
【问题描述】:

基于这个问题Force the origin to start at 0 我想将其实现到一个新创建的主题中,而不仅仅是作为默认设置。

data(iris)
ggplot(iris, aes(x = Species)) + 
  geom_bar() +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05)))

如何在不定义每个情节的情况下做到这一点?

【问题讨论】:

标签: r ggplot2 plot axes


【解决方案1】:

据我所知,theme() 无法做到这一点。但是您可以像这样在scale_y_continuous 周围定义一个包装器

library(ggplot2)

data(iris)

scale_y_origin <- function(...) {
  scale_y_continuous(expand = expansion(mult = c(0, 0.05)), ...)
}

ggplot(iris, aes(x = Species)) + 
  geom_bar() +
  scale_y_origin()

【讨论】:

    猜你喜欢
    • 2012-11-21
    • 1970-01-01
    • 2023-03-06
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    相关资源
    最近更新 更多