【问题标题】:ggplot exact axis rangeggplot 精确轴范围
【发布时间】:2019-11-23 13:30:36
【问题描述】:

如何将轴范围设置为与指定的完全一致?

对于以下示例,我希望 x 轴在 0% 处交叉(而不是如图所示的 -0.5%),并且我希望最大值为 12%(而不是 12.5%)

我都试过了:

scale_x_continuous(limits = c(0, 0.12))

coord_cartesian(ylim = c(0, 0.12)) 

根据:

How to set limits for axes in ggplot2 R plots?

Can I limit ggplot axis range EXACTLY?

示例代码:

myData = data.frame(x = c(0, 1, 2, 3, 4, 5),
                  y = c(0.05,0.06, 0.07, 0.08, 0.09, 0.09))


ggplot() +
  geom_step(data=myData, aes(x=x, y=y), color='orange', size=1) +
  xlab('') +
  ylab('') +
  scale_y_continuous(labels = scales::percent, limits=c(0,0.12))


ggplot() +
  geom_step(data=myData, aes(x=x, y=y), color='blue', size=1) +
  xlab('') +
  ylab('') +
  scale_y_continuous(labels = scales::percent) + 
  coord_cartesian(ylim = c(0,0.12))

结果:

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您在寻找expand 吗?

    coord_cartesian(ylim = c(0,0.12), expand=0)
    

    或更好(见 cmets)

    scale_y_continuous(labels = scales::percent,
     expand = expand_scale(),
     limits = c(0,0.12))
    

    【讨论】:

    • scale_y_continuous(labels = scales::percent, expand = expand_scale(), limits = c(0,0.12)
    • 完美。谢谢。
    猜你喜欢
    • 2021-10-29
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多