Reputation: 167
I am trying to prepare a bar plot comparing the score of two teams against matches (or innings). I am getting close to what I want. But the bar chart appears to showing the maximum score in each match and not the total. Appreciate any advice on how to correct for this. The data frame I have is
Section of original data:
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")
The graph that I have developed so far (which as noted above is incorrect) and shows maximum individual score and not the total team score is as follows:
The code for doing this is as follows:
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)
}
Any help would certainly be appreciated.
Upvotes: 1
Views: 71
Reputation: 388862
summarise the data first before plotting.
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()
Upvotes: 0