ggalt icon indicating copy to clipboard operation
ggalt copied to clipboard

geom_dumbbell with scales for line and dots

Open fzesch opened this issue 4 years ago • 0 comments

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?

fzesch avatar Oct 31 '19 08:10 fzesch