使用Ggplot制作堆叠条形图并添加百分比标签

3
我想要绘制一个基于数量的堆积条形图,但是标签上应该显示出该图表中所占比例。我已经生成了下面这张图表,但百分比是基于所有数据的。我想要的是按团队计算的百分比(例如,澳大利亚的百分比总和=100%,英格兰的百分比总和也是100%)。 Team plot 为了实现这个目标,以下是需要使用的代码。该函数会统计5场比赛中每个团队中不同角色的数量(由于每场比赛中每位球员的角色出现两次(5场比赛x2次出场),因此必须将结果除以10):
team_roles_Q51 <- function(){
        ashes_df <- tidy_data()
        
        graph <- ggplot(ashes_df %>%
                        count(team, role) %>%       #Groups by team and role
                        mutate(pct=n/sum(n)),       #Calculates % for each role
               aes(team, n, fill=role)) +
                geom_bar(stat="identity") +
                scale_y_continuous(labels=function(x)x/10) +      #Needs to be a better way than dividing by 10
                ylab("Number of Participants") +
                geom_text(aes(label=paste0(sprintf("%1.1f", pct*100),"%")),
                          position=position_stack(vjust=0.5)) +
                ggtitle("England & Australia Team Make Up") +
                theme_bw()

        print(graph)
}

导入的数据框如下所示:

Data frame imported

数据框前10行的结构如下:

structure(list(batter = c("Ali", "Anderson", "Bairstow", "Ball", 
"Bancroft", "Bird", "Broad", "Cook", "Crane", "Cummins"), team = structure(c(2L, 
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L), .Label = c("Australia", 
"England"), class = "factor"), role = structure(c(1L, 3L, 4L, 
3L, 2L, 3L, 3L, 2L, 3L, 3L), .Label = c("allrounder", "batsman", 
"bowler", "wicketkeeper"), class = "factor"), innings = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("test_1_innings_1", 
"test_1_innings_2", "test_2_innings_1", "test_2_innings_2", "test_3_innings_1", 
"test_3_innings_2", "test_4_innings_1", "test_4_innings_2", "test_5_innings_1", 
"test_5_innings_2"), class = "factor"), batting_num = c(6, 11, 
7, 10, 1, NA, 9, 1, NA, 9), score = c(38, 5, 9, 14, 5, NA, 20, 
2, NA, 42), balls_faced = c(102, 9, 24, 11, 19, NA, 32, 10, NA, 
120)), row.names = c(NA, 10L), class = "data.frame")

任何帮助都将不胜感激。谢谢。

听起来你需要在mutate()之前添加group_by(team)ashes_df %>% count(team, role) %>% group_by(team) %>% mutate(pct=n/sum(n))。在这种情况下,这与ggplot2没有什么关系。 - MrFlick
1个回答

9
你需要按照团队进行group_by,计算比例并在aes中使用pct
library(dplyr)
library(ggplot2)

ashes_df %>%
  count(team, role) %>%       
  group_by(team) %>%
  mutate(pct= prop.table(n) * 100) %>%
  ggplot() + aes(team, pct, fill=role) +
  geom_bar(stat="identity") +
  ylab("Number of Participants") +
  geom_text(aes(label=paste0(sprintf("%1.1f", pct),"%")),
            position=position_stack(vjust=0.5)) +
  ggtitle("England & Australia Team Make Up") +
  theme_bw()

enter image description here


再次感谢Ronak。我不想将图表归一化为100%。我错过的部分是你和MrFlick建议的group_by。这解决了问题。 - MikeM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接