【问题标题】:Plot proportion in bar chart grouped by another variable绘制由另一个变量分组的条形图中的比例
【发布时间】:2017-10-18 15:29:53
【问题描述】:

我目前正在阅读R for Data Science 并尝试创建一些图表。我知道要在条形图中获得比例,您需要使用group = 1。例如,下面的代码有效:

library(ggplot2) 

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = color))

但我没有得到相同的比例图。

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = color, y = ..prop.., group = 1))

我确实得到了比例,但不是color

【问题讨论】:

  • 你不需要tidyverse这个包
  • diamondsggplot2 的一部分,这是您唯一需要的包。

标签: r ggplot2 rstudio data-science geom-bar


【解决方案1】:

这是使用..count..的一种方法

require(ggplot2)

ggplot(diamonds,aes(cut,..count../sum(..count..),fill=color))+
  geom_bar()+
  scale_y_continuous(labels=scales::percent)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2022-12-03
    • 2018-03-15
    相关资源
    最近更新 更多