并排数据的 Ggplot 条形图未显示为堆叠

Ggplot bar plotting of side by side data not showing as stacked

我正在尝试准备一个条形图来比较两支球队对比赛(或局)的得分。我越来越接近我想要的了。但是条形图似乎显示的是每场比赛的最高分,而不是总分。感谢有关如何纠正此问题的任何建议。我的数据框是

原始数据部分:

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

我目前制作的图表(如上所述是不正确的)显示的是个人最高分而不是团队总分,如下所示:

执行此操作的代码如下:

plot_graphs <- function(){
        ashes_df <- tidy_data() #imports data frame
        
        canvas2 <- ggplot(ashes_df,aes(x = innings, y = score, fill = team))
        graph2 <- canvas2 +
                geom_bar(stat = "identity", position = "dodge", na.rm = TRUE) +
                ggtitle("England & Australia Innings Scores") +
                theme_bw()
                
        print(graph2)
}

我们将不胜感激。

在绘图之前先总结数据。

library(dplyr)
library(ggplot2)

ashes_df %>%
  group_by(innings, team) %>%
  summarise(batting_num = sum(batting_num, na.rm = TRUE)) %>%
  ggplot(aes(x = innings, y = batting_num, fill = team)) + 
  geom_bar(stat = "identity", position = "dodge", na.rm = TRUE) +
  ggtitle("England & Australia Innings Scores") +
  theme_bw()