将分布图的最后一个条形定义为大于之前的所有值

问题描述 投票:0回答:1

我想绘制玩家获胜总数的分布。我想将x轴的最后一个部分作为“比之前的值更多”类别。

示例数据:

game_data <- data.frame(player = c(1,2,3,4,5, 6), n_wins = c(1,8,2,3,6,4))

game_data
  player n_wins
1      1      1
2      2      8
3      3      2
4      4      3
5      5      6
6      6      4
6      6      4

下面的代码创建一个类别“ NA”,但我希望它大于5(=大于5胜)。

game_data %>% group_by(player) %>% summarise(allwins = sum(n_wins)) %>%
  ggplot(aes(x = cut(allwins, breaks = seq(1,6, by = 1)), include.lowest=TRUE)) + 
  geom_bar(aes(y = (..count..)/sum(..count..))) + 
  scale_y_continuous(labels=scales::percent) +
  labs(title="Distribution of Wins", subtitle="", y="Fraction of Players", x="Number of Wins")

Distribution Plot

我不仅要更改标签,还希望它自动创建最后一个类别。

r ggplot2 plot dplyr distribution
1个回答
1
投票

您可以通过添加+ Inf作为中断来执行以下操作:

set.seed(100)
game_data <- data.frame(player = c(1,2,3,4,5, 6), n_wins = c(1,8,2,3,6,4))
BR = c(0:5,+Inf)

game_data %>% group_by(player) %>% summarise(allwins = sum(n_wins)) %>%
  ggplot(aes(x = cut(allwins, breaks = BR,labels=c(1:5,"5+")))) + 
  geom_bar(aes(y = (..count..)/sum(..count..))) + 
  scale_y_continuous(labels=scales::percent) +
  labs(title="Distribution of Wins", subtitle="", y="Fraction of Players", x="Number of Wins")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.