【问题标题】:How make y axis corresponding to the values?如何使y轴对应于值?
【发布时间】:2017-11-11 21:40:11
【问题描述】:

我只想在 y 轴上有 3 个数字:1、1.7、2.4

  dt <- data.frame(a = c(1, 1.7, 2.4), b = letters[1:3])
  ggplot(dt, aes(b,a, fill = a)) + geom_bar(stat = "identity")

我知道我可以手动调整,但是有没有简单的方法让 ggplot 自动调整?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以使用scale_y_continuous()并根据您的数据设置刹车:

    dt <- data.frame(a = c(1, 1.7, 2.4), b = letters[1:3])
    ggplot(dt, aes(b,a, fill = a)) + geom_bar(stat = "identity") + 
      scale_y_continuous(breaks = dt$a)
    

    如果这个自动够了。

    【讨论】:

    • 然后我在另一个数据集中得到百分比:O
    • @WW 你到底是什么意思?
    • 在我的真实数据集中,我得到 y 轴的值:25% 和 75%,而不是像:1400, 2874.5, 6788 这样的值。这些值计算为 quantile25、median 和 quantile75
    • 好的,这是因为值有名称。 names(values) &lt;- NULL 已修复 :)
    猜你喜欢
    • 2023-01-30
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多