绘制超过特定值的人员百分比

Plot the percentage of persons for whom a certain value is exceeded

我想绘制超过某个变量值的用户比例。

示例数据:

game_data <- data.frame(player = c(1,1,1,2,2,2,3,3,3), day= c(1,2,3,1,2,3,1,2,3), n_wins = c(0,2,1,1,0,0,2,0,1))

game_data
  player day n_wins
1      1   1      0
2      1   2      2
3      1   3      1
4      2   1      1
5      2   2      0
6      2   3      0
7      3   1      2
8      3   2      0
9      3   3      1

不幸的是,我不知道如何绘制它,这只是基本代码:

game_data %>% group_by(player) %>% summarize(allwins = sum(n_wins)) %>% ggplot(aes(x=?, y = ?)) + geom_bar()

我想绘制 1 对 2 对 3 获胜的用户比例(y 轴)(所以 1 的 33.3% 条,2 和 0% 的条) 3 的 66.7% 栏)。

有几种方法可以做到这一点,这里是一种预先计算值并与 identity 一起使用的方法 stat:

library(tidyverse)

game_data %>% 
  group_by(player) %>% 
  summarize(allwins = sum(n_wins)) %>% 
  add_count(allwins) %>%
  complete(allwins = 1:max(allwins), 
           fill = list(n = 0)) %>%
  distinct(allwins, Percentage = round(n / length(na.omit(player)) * 100, 2)) %>%
  ggplot(aes(x = allwins, y = Percentage)) +
  geom_bar(stat = 'identity')

输出: