I’m trying to plot an cumulative xg plot between two teams and ggplotly, its exactly how i want it to look except there is a small gap in the fill and im not sure if its to do with geom_ribbon ymin and ymax or if its the alpha. The below is the code for the plot;
Plotly cumulative xg plot
fig <- ggplot(wba_liv_shots, aes(x = round(minute), y = cumulative_xG, color = team, fill = team)) +
geom_line(size = 0.5) + # Add lines for each team with increased thickness
geom_text(data = subset(wba_liv_shots, result %in% c(“Goal”, “OwnGoal”)), aes(x = round(minute), y = cumulative_xG, label = result), vjust = -0.5) + # Add labels for goals and own goals
geom_point(data = subset(wba_liv_shots, result %in% c(“Goal”, “OwnGoal”)), aes(x = round(minute), y = cumulative_xG), shape = 1, size = 2) + # Change crosses to dots for goals and own goals
geom_ribbon(aes(ymin = 0, ymax = cumulative_xG), alpha = 0.2) + # Fill area below the lines
annotate(geom = “text”, x = Inf, y = Inf, label = paste(total_xG$team, “-“, round(total_xG$xG, 2), “xG”), hjust = -0.2, vjust = 1.3, size = 4, color = “white”) + # Add total xG for each team
labs(title = “Cumulative Expected Goals over Time”, x = “Minute”, y = “Cumulative xG”) +
scale_color_discrete(name = NULL, labels = c(“Arsenal”, “Tottenham”)) + # Remove color legend title and only include team names
scale_fill_discrete(name = NULL, labels = c(“Arsenal”, “Tottenham”)) + # Remove fill legend title and only include team names
theme_few() +
theme(legend.position = “right”)
ggplotly(fig)
I tried fixing the layering of the plot but that also didn’t seem to work.
Mohamed Ahmed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.