【问题标题】:Normalized bar graph标准化条形图
【发布时间】:2017-07-20 04:11:09
【问题描述】:

我有一个数据框:

df<-data.frame(Pet=rep(c("Dog", "Cat", "Bird"), c(5,10,15)), Gen=rep(c("M", "F", "M", "F", "M", "F"), c(3,5,12,5,3,2)))

当我将每只动物的雄性/雌性频率可视化时,我得到了这个图表:

ggplot(df, aes(Pet, group=Gen, fill=Gen)) + geom_bar(position="dodge", width=.5)

如何制作一个图表,其中包含具有相同身高的女性的条形图和具有与相应女性条形图相对高度的男性的条形图?

类似这样的:

【问题讨论】:

    标签: r ggplot2 normalization geom-bar


    【解决方案1】:

    一个简单的解决方法是首先标准化数据,然后绘制:

    t = table(df)
    as.data.frame.table(t/t[,'F']) %>% 
        ggplot(aes(x=Pet, y=Freq, group=Gen, fill=Gen)) + 
        geom_bar(position="dodge", width=.5, stat="identity")
    

    【讨论】:

      猜你喜欢
      • 2020-09-08
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      相关资源
      最近更新 更多