hrbrmstr / ggalt

:earth_americas: Extra Coordinate Systems, Geoms, Statistical Transformations & Scales for 'ggplot2'
https://cran.r-project.org/web/packages/ggalt/vignettes/ggalt_examples.html
Other
654 stars 99 forks source link

geom_dumbbell with scales for line and dots #63

Open fzesch opened 4 years ago

fzesch commented 4 years ago

I'm trying to make a dumbbell chart with additional information about change (green/red) and significance of the change (vol) and add an additional legend for the dots at the end of the dumbbells.

Example plot:

library(ggalt)

# build data set
set.seed(1)

df <- data.frame(country=paste("Region", LETTERS[1:10]))
df$last_year <- runif(nrow(df))
df$this_year <- runif(nrow(df))
df$ydiff <- df$this_year - df$last_year
df$vol <- runif(nrow(df))

# create dumbbell plot
ggplot(df, aes(y=country, group=country)) + 
  geom_dumbbell(aes(x=last_year, xend=this_year, colour = ydiff, size=vol),
                colour_x = "blue",
                colour_xend = "yellow") +
  scale_color_gradient2(low="green", high="red") 

Now, I'd like to add a legend about what the yellow and blue dots are, but I cannot add two color scales, since I already use the gradient2 scale for the bar between the dots. Ideally, I would have a manual fill scale as an additional option to customize the plot (but then I might need long and wide data at the same time). Can you help me?