【问题标题】:Create proportion bar plot with dodge position创建带有闪避位置的比例条图
【发布时间】:2019-02-20 11:52:00
【问题描述】:

我正在尝试创建以下图,但 比例 在 y 轴上。

library(ggplot2)
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")

但是当我添加y=..prop.. 时,它不会按clarity 对其进行分组。我尝试了以下方法:

ggplot(data = diamonds) + 
      geom_bar(mapping = aes(x = cut, y = ..prop.., fill = clarity), position = "dodge")

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    要计算比例(或频率),您可以使用..count..(比例是特定的count 除以所有count):

    library(ggplot2)
    ggplot(diamonds, aes(cut, (..count..) / sum(..count..), fill = clarity)) +
        geom_bar(position = "dodge") 
    

    【讨论】:

    • 我试过这个,但我添加了group = 1。谢谢!
    • @chintans 我知道。出于某种原因,..prob..dodgegroup = 1 不起作用。
    猜你喜欢
    • 2016-01-09
    • 2021-12-28
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    相关资源
    最近更新 更多