3

using the following code, I created a dumbbell chart.

f <- ggplot(Brewers_PA, aes(x=PA.2015, xend=PA.2016, y=Name))
f + geom_dumbbell(colour = "darkblue", point.colour.l = "darkred", point.colour.r = "darkBlue", point.size.l = 2.5, point.size.r = 2.5) + 
  theme(plot.background=element_rect(fill = "grey93", colour = "grey93")) +
  theme(plot.title=element_text(size = 11, face = "bold", hjust = 0)) + 
  theme(axis.text.x=element_text(size = 8)) +
  theme(axis.text.y=element_text(size = 8)) +
  theme(axis.title.x=element_text(size = 9)) + 
  theme(axis.title.y=element_text(size=9)) + ylab("") + xlab("Plate Appearance") + 
  ggtitle("Brewers Change in Plate Appearance 2015-2016") 

I was able to do this thanks to this tutorial. https://www.r-bloggers.com/beating-lollipops-into-dumbbells/

The only problem is that I would like to add a legend to this but I am not sure how. Does anybody know? All help is appreciated.

I would basically like the legend to show the years to the colours. So, "darkblue" = 2016 (PA.2016), "darkred" = 2015 (PA.2015). I wanted to add a picture but for some reason it won't work.

Here is the data frame I created:

               Name     PA.2015 PA.2016
1   Jonathan Villar     128     679
2      Chris Carter     460     644
3        Ryan Braun     568     564
4   Scooter Gennett     391     542
5      Hernan Perez     272     430
6  Kirk Nieuwenhuis     141     392
7   Jonathan Lucroy     415     544
8        Aaron Hill     353     429
9      Ramon Flores     289     289
10  Domingo Santana     187     281
11 Martin Maldonado     256     253
12     Keon Broxton       2     244
13    Orlando Arcia       0     216

Julien
  • 992
  • 1
  • 10
  • 26

2 Answers2

10

Pkg author, here.

I'd suggest not adding extra interpretation cycles for the reader and make it an in-chart "legend", plus removing some "chart junk" aesthetics:

library(ggplot2)
library(ggalt)

read.table(text='Name     PA.2015 PA.2016
"Jonathan Villar"    128     679
"Chris Carter"     460     644
"Ryan Braun"     568     564
"Scooter Gennett"     391     542
"Hernan Perez"     272     430
"Kirk Nieuwenhuis"     141     392
"Jonathan Lucroy"     415     544
"Aaron Hill"     353     429
"Ramon Flores"     289     289
"Domingo Santana"     187     281
"Martin Maldonado"     256     253
"Keon Broxton"       2     244
"Orlando Arcia"       0     216', header=TRUE, quote='"') -> Brewers_PA

f <- ggplot()
f <- f + geom_dumbbell(data=Brewers_PA, 
                       aes(x=PA.2015, xend=PA.2016, y=Name),
                       colour = "darkblue", 
                       point.colour.l = "darkred", point.colour.r = "darkBlue",
                       point.size.l = 2.5, point.size.r = 2.5)
f <- f + geom_text(data=data.frame(), 
                    aes(x=391, y= "Scooter Gennett", label="2015"),
                    color="darkred", hjust=1, size=3, nudge_x=-10)
f <- f + geom_text(data=data.frame(), 
                    aes(x=542, y= "Scooter Gennett", label="2016"),
                    color="darkBlue", hjust=0, size=3, nudge_x=10)
f <- f + scale_x_continuous(expand=c(0,10))
f <- f + labs(x=NULL, y=NULL,
              title="Brewers Change in Plate Appearance 2015-2016")
f <- f + theme(plot.background=element_rect(fill = "grey93", colour = "grey93"))
f <- f + theme(plot.title=element_text(size = 11, face = "bold", hjust = 0))
f <- f + theme(axis.text.x=element_text(size = 8))
f <- f + theme(axis.text.y=element_text(size = 8))
f <- f + theme(axis.title.x=element_text(size = 9)) 
f <- f + theme(axis.title.y=element_text(size=9))
f <- f + theme(axis.ticks=element_blank())
f <- f + theme(panel.grid.major.y=element_blank())
f <- f + theme(plot.margin=margin(30,30,30,30))
f

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Awesome! Thanks so much, and great package, btw. Made my life a lot easier. – Julien Oct 13 '16 at 02:28
  • I had issues with the colors until I followed the [ggalt vignette](https://github.com/hrbrmstr/ggalt/blob/master/vignettes/ggalt_examples.Rmd) and used `colour_x = "darkred", colour_xend = "darkBlue"` rather than `point.colour.l = "darkred", point.colour.r = "darkBlue"` – David Ebert Dec 04 '19 at 20:59
1

Here is another way of doing this that might be helpful (or not). enter image description here

##Reformat the data
xdf<-Brewers_PA %>% mutate("key"="Brewers Change in Plate Appearance 2015-2016")
temp<-Brewers_PA %>% mutate(diff=PA.2015-PA.2016) %>% arrange(desc(diff))
xdf$Name<-factor(xdf$Name,temp$Name)

##Plot using the dumbbell R package
p2<-dumbbell::dumbbell(xdf,id="Name", key="key", column2 = "PA.2015", column1 = "PA.2016", lab2 = "PA.2015", lab1="PA.2016", p_col2 = "darkblue", p_col1 ="darkred", delt=1, pointsize = 2 , pt_val = 1, textsize = 3, title = "Brewers Change in Plate Appearance 2015-2016")

Foo Cheung
  • 41
  • 2