带百分比标签的 Ggplot 堆积条形图

Mik*_*keM 4 r bar-chart ggplot2 dplyr

我正在尝试根据计数绘制堆积条形图,但标签显示图上的百分比。我制作了下面的情节。然而,该百分比是基于所有数据的。我追求的是球队的百分比(例如澳大利亚的百分比之和= 100%,英格兰的百分比= 100%)。 团队情节

实现这一点的代码是以下函数。此函数计算 5 场比赛中每支球队中不同角色的数量(我必须将结果除以 10,因为球员角色在每场比赛中出现两次(5 场比赛 x 2 次出场):

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)
}
Run Code Online (Sandbox Code Playgroud)

导入的数据框的示例是:

导入数据框

数据帧前 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")
Run Code Online (Sandbox Code Playgroud)

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

Ron*_*hah 11

您需要group_by team计算比例并pct用于aes

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述