【问题标题】:R - Name of each bar in ggplot2R - ggplot2 中每个条的名称
【发布时间】:2016-02-28 02:21:05
【问题描述】:

我想在每个栏下都有每个栏的名称(在我的例子中,名称是“Round”,它们恰好是 1、2、... 12)

这是我当前的代码:

ggplot(data=draft1, aes(x = Round, y = mean.age)) + 
geom_bar(stat = "identity", fill = "steelblue", color = "black", width = 0.7) +
ylab("Average age of retirement") +   ylim(c(0,40)) + 
ggtitle("Average age of retirement by rounds of all players") +
geom_text(aes(label = mean.age), position=position_dodge(width=0.9), vjust = -0.5)

这是当前的输出:

【问题讨论】:

    标签: r ggplot2 geom-bar


    【解决方案1】:

    将您的Round 设置为一个因素

    ggplot(data=draft1, aes(x = factor(Round), y = mean.age)) + 
    

    或使用scale_x_continuous()

    ggplot(data=draft1, aes(x = Round, y = mean.age)) + 
      ... +
      scale_x_continuous(breaks=(seq(1:12)))
    

    【讨论】:

      【解决方案2】:

      只需为 x 添加一个离散比例:

      library(ggplot2)
      
      draft1 = data.frame(Round = seq(1, 12), mean.age = sample(29:32))
      
      ggplot(data=draft1, aes(x = Round, y = mean.age)) + 
        geom_bar(stat = "identity", fill = "steelblue", color = "black", width = 0.7) +
        ylab("Average age of retirement") +   ylim(c(0,40)) + 
        ggtitle("Average age of retirement by rounds of all players") +
        geom_text(aes(label = mean.age), position=position_dodge(width=0.9), vjust = -0.5) +
        scale_x_discrete()
      

      【讨论】:

      • 您可能想要使用limitsbreaks 参数来整理x 轴
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 2014-11-17
      • 2016-11-07
      相关资源
      最近更新 更多