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

时间:2021-03-15 02:17:59

标签: r ggplot2 dplyr bar-chart

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

实现这一点的代码是以下函数。此函数计算 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)
}

导入的数据框的示例是:

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")

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

1 个答案:

答案 0 :(得分:1)

您需要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()

enter image description here