'Adding a legend to ggplot

I have the below dataset;

Player Goals Shots
Regan Charles-Cook 10 32
Tony Watt 9 36
Bruce Anderson 8 26
Liam Boyce 8 44
Kyogo Furuhashi 8 31
Alfredo Morelos 8 80
Christian Ramirez 8 41
Liel Abada 7 57
Martin Boyle 7 43
Kevin van Veen 7 45

I am attempting a dumbbell chart and so far have the following code;

library(tidyverse)
library(ggplot2)
library(ggalt)

theme_set(theme_bw())

read_excel("SPL_Goals.xlsx")

data <- read_excel("SPL_Goals.xlsx")

data %>%
  #the below code sets out the initial plot template without the data
  ggplot(aes(x= Goals, xend = Shots, y= Player)) +
  #below code inputs the data viz on to the plot
  geom_dumbbell(
    size = 1.5, color = "black", size_x = 10,   #size=1.5 dictates black line size
    size_xend = 3, colour_x = "green",
    colour_xend = "red") +
  labs(
    title = "Scotland; Goals v Shots",           #add title
    subtitle = "Top 10; Matchday 22",                         #add subtitle
    x = "Total", y = "Player"
  )+
  geom_text(aes(label = Goals))

This produces the below chart; enter image description here

My query is how do I order the chart so Goals (the green circle) is ascending and also add a legend to show Goals (green) and Shots (red)? I have tried mutate, reorder and fct_reorder but I am doing something wrong as none of these are working.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source