2017-04-19 166 views
4

我试图绘制显示一列内的每个组的相对百分比堆积条形图。显示百分比通过柱上的堆积条形图

这里是我的问题的说明,使用默认的MPG数据集:

mpg %>% 
    ggplot(aes(x=manufacturer, group=class)) + 
    geom_bar(aes(fill=class), stat="count") + 
    geom_text(aes(label=scales::percent(..prop..)), 
    stat="count", 
    position=position_stack(vjust=0.5)) 

这是输出: enter image description here

我的问题是,这个输出显示每个类的百分比对总计,而不是每个厂家内的相对百分比。

例如,我想的第一列(奥迪)以显示棕色(紧凑)83.3%(15/18)和绿色(中型)16.6%(3/18)。

我发现了一个类似的问题在这里: How to draw stacked bars in ggplot2 that show percentages based on group?

,但我想知道是否有到GGPLOT2内做到这一点,尤其是因为我的实际数据集使用一堆dplyr管道之前最终整理数据的简单方法管道到ggplot2。

回答

3

如果我将你的问题与你给出的链接进行比较,那么区别在于链接“自我计数”。这就是我所做的。我不确定这是否适合您的真实数据。

library(ggplot2) 
library(dplyr) 

mpg %>% 
    mutate(manufacturer = as.factor(manufacturer), 
     class = as.factor(class)) %>% 
    group_by(manufacturer, class) %>% 
    summarise(count_class = n()) %>% 
    group_by(manufacturer) %>% 
    mutate(count_man = sum(count_class)) %>% 
    mutate(percent = count_class/count_man * 100) %>% 
    ggplot() + 
    geom_bar(aes(x = manufacturer, 
       y = count_man, 
       group = class, 
       fill = class), 
      stat = "identity") + 
    geom_text(aes(x = manufacturer, 
       y = count_man, 
       label = sprintf("%0.1f%%", percent)), 
      position = position_stack(vjust = 0.5)) 

编辑,基于注释:

我犯了一个错误的选择了错误的列y

library(ggplot2) 
library(dplyr) 

mpg %>% 
    mutate(manufacturer = as.factor(manufacturer), 
     class = as.factor(class)) %>% 
    group_by(manufacturer, class) %>% 
    summarise(count_class = n()) %>% 
    group_by(manufacturer) %>% 
    mutate(count_man = sum(count_class)) %>% 
    mutate(percent = count_class/count_man * 100) %>% 
    ungroup() %>% 
    ggplot(aes(x = manufacturer, 
      y = count_class, 
      group = class)) + 
    geom_bar(aes(fill = class), 
      stat = "identity") + 
    geom_text(aes(label = sprintf("%0.1f%%", percent)), 
      position = position_stack(vjust = 0.5)) 
+0

你的方法,百分比是正确的,但块大小是错误的。但我认为这是正确的方向;让我和dplyr一起玩,看看我能否做对。 – kraussian

+0

我是多么愚蠢!我也会看看并编辑答案! – ricoderks

+0

哇,现在完美了!我也想过这样做,但没有意识到可以使用_ungroup_将汇总数据恢复为原始格式。这是我的缺失环节;谢谢! :) – kraussian

1

如果剧情需要的数目及百分比作为有色barplots的顶部的文本,以帮助我们看到的差异,也许是更好的呈现结果作为一个简单的表:

round(prop.table(table(mpg$class, mpg$manufacturer), margin = 2), 3) * 100 

#    audi chevrolet dodge ford honda hyundai jeep land rover lincoln mercury nissan pontiac subaru toyota volkswagen 
# 2seater  0.0  26.3 0.0 0.0 0.0  0.0 0.0  0.0  0.0  0.0 0.0  0.0 0.0 0.0  0.0 
# compact  83.3  0.0 0.0 0.0 0.0  0.0 0.0  0.0  0.0  0.0 15.4  0.0 28.6 35.3  51.9 
# midsize  16.7  26.3 0.0 0.0 0.0 50.0 0.0  0.0  0.0  0.0 53.8 100.0 0.0 20.6  25.9 
# minivan  0.0  0.0 29.7 0.0 0.0  0.0 0.0  0.0  0.0  0.0 0.0  0.0 0.0 0.0  0.0 
# pickup  0.0  0.0 51.4 28.0 0.0  0.0 0.0  0.0  0.0  0.0 0.0  0.0 0.0 20.6  0.0 
# subcompact 0.0  0.0 0.0 36.0 100.0 50.0 0.0  0.0  0.0  0.0 0.0  0.0 28.6 0.0  22.2 
# suv   0.0  47.4 18.9 36.0 0.0  0.0 100.0  100.0 100.0 100.0 30.8  0.0 42.9 23.5  0.0 
+0

谢谢您的回答。这是有益的,但不正是我一直在寻找,因为_mpg_数据集只是意图的例子。 但它是一个很好的点,你的矩阵显示可能是一个更好的方式来显示这个特定的数据集类,制造商摘要。 – kraussian