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) +
geom_text(
data = subset(wba_liv_shots, result %in% c("Goal", "OwnGoal")),
aes(x = round(minute), y = cumulative_xG, label = result), vjust = -0.5
) +
geom_point(
data = subset(wba_liv_shots, result %in% c("Goal", "OwnGoal")),
aes(x = round(minute), y = cumulative_xG), shape = 1, size = 2
) +
geom_ribbon(aes(ymin = 0, ymax = cumulative_xG), alpha = 0.2) +
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"
) +
labs(title = "Cumulative Expected Goals over Time", x = "Minute", y = "Cumulative xG") +
scale_color_discrete(name = NULL, labels = c("Arsenal", "Tottenham")) +
scale_fill_discrete(name = NULL, labels = c("Arsenal", "Tottenham")) +
theme_few() +
theme(legend.position = "right")
ggplotly(fig)
I tried fixing the layering of the plot but that also didn’t seem to work.
New contributor
Mohamed Ahmed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1