ggplot用geom_bar中的百分比替换计数

Don*_*beo 16 r ggplot2 geom-bar

我有一个数据帧d:

> head(d,20)
   groupchange Symscore3
1            4         1
2            4         2
3            4         1
4            4         2
5            5         0
6            5         0
7            5         0
8            4         0
9            2         2
10           5         0
11           5         0
12           5         1
13           5         0
14           4         1
15           5         1
16           1         0
17           4         0
18           1         1
19           5         0
20           4         0
Run Code Online (Sandbox Code Playgroud)

我正在密谋:

ggplot(d, aes(groupchange, y=..count../sum(..count..),  fill=Symscore3)) +
  geom_bar(position = "dodge") 
Run Code Online (Sandbox Code Playgroud)

通过这种方式,每个条形表示其在整个数据上的百分比.

相反,我希望每个条形代表相对百分比; 即获得的柱的总和groupchange = k应该是1.

Jaa*_*aap 29

首先总结并转换您的数据:

library(dplyr)
d2 <- d %>% 
  group_by(groupchange,Symscore3) %>% 
  summarise(count=n()) %>% 
  mutate(perc=count/sum(count))
Run Code Online (Sandbox Code Playgroud)

然后你可以绘制它:

ggplot(d2, aes(x = factor(groupchange), y = perc*100, fill = factor(Symscore3))) +
  geom_bar(stat="identity", width = 0.7) +
  labs(x = "Groupchange", y = "percent", fill = "Symscore") +
  theme_minimal(base_size = 14)
Run Code Online (Sandbox Code Playgroud)

这给了:

在此输入图像描述


或者,您可以使用包中的percent功能scales:

brks <- c(0, 0.25, 0.5, 0.75, 1)

ggplot(d2, aes(x = factor(groupchange), y = perc, fill = factor(Symscore3))) +
  geom_bar(stat="identity", width = 0.7) +
  scale_y_continuous(breaks = brks, labels = scales::percent(brks)) +
  labs(x = "Groupchange", y = NULL, fill = "Symscore") +
  theme_minimal(base_size = 14)
Run Code Online (Sandbox Code Playgroud)

这使:

在此输入图像描述

  • @HoneyBuddha我怀疑大多数人是否只看待接受的答案:我已经发布了一些答案,至少收到了几个赞成票(其中一些甚至超过了接受的答案).更进一步,在`position ="fill"选项中进行编辑会让我感觉很喜欢.它也被大多数人视为不公平行为. (3认同)

Ric*_*loo 12

如果您的目标是使用最少的代码进行可视化,请将其position = "fill"用作参数geom_bar().

如果你想在小组百分比内,@ Jaap的dplyr回答答案是要走的路.

以下是使用上述数据集进行复制/粘贴的可重现示例:

library(tidyverse)

d <- data_frame(groupchange = c(4,4,4,4,5,5,5,4,2,5,5,5,5,4,5,1,4,1,5,4),
                Symscore3 = c(1,2,1,2,0,0,0,0,2,0,0,1,0,1,1,0,0,1,1,0))

ggplot(d, aes(x = factor(groupchange), fill = factor(Symscore3))) +
  geom_bar(position="fill")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 对于使用小型数据集的人来说,就代码的清晰度/方法的效率而言,此选项可能优于公认的答案。 (2认同)

Meg*_*ron 8

我们还可以向比例添加标签,而无需在源数据框中显式计算它们。

library(tidyverse)

d <- data_frame(groupchange = c(4,4,4,4,5,5,5,4,2,5,5,5,5,4,5,1,4,1,5,4),
                Symscore3 = c(1,2,1,2,0,0,0,0,2,0,0,1,0,1,1,0,0,1,1,0)) %>%
  mutate_all(as.character)  # treat the numbers as categories

ggplot(d, aes(x=groupchange, fill=Symscore3)) +
  geom_bar(position="fill") +
  geom_text(
    aes(label=signif(..count.. / tapply(..count.., ..x.., sum)[as.character(..x..)], digits=3)),
    stat="count",
    position=position_fill(vjust=0.5)) +
  labs(y="Proportion")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

此解决方案中的标签geom_text改编自此处