Add a legend in ggplot dumbbell plot

414 Views Asked by At

I have created a dumbbell plot below and I would like to add a legend based on the color of the points which display x and xend. Is that possible for dumbbell?

library(ggalt)

health <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area))
ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area)) + 
        #create a thick line between x and xend instead of using defaut 
        #provided by geom_dubbell
        geom_segment(aes(x=pct_2013, 
                         xend=pct_2014, 
                         y=Area, 
                         yend=Area), 
                     color="#b2b2b2", size=1.5)+
        geom_dumbbell(color="light blue", 
                      size_x=3.5, 
                      size_xend = 3.5,
                      #Note: there is no US:'color' for UK:'colour' 
                      # in geom_dumbbel unlike standard geoms in ggplot()
                      colour_x="#edae52", 
                      colour_xend = "#9fb059")+
        labs(x=NULL, y=NULL, 
             title="Dumbbell Chart", 
             subtitle="Pct Change: 2013 vs 2014")+
        geom_text(color="black", size=2, hjust=-0.5,
                  aes(x=pct_2013, label=pct_2013))+
        geom_text(aes(x=pct_2014, label=pct_2014), 
                  color="black", size=2, hjust=1.5)
0

There are 0 best solutions below