I'm trying to colour the xend
points in my dumbbell plot based on whether the x_diff
value is positive or negative. Essentially, if the x_diff
value is positive, I'd like the xend
point coloured green, and if its negative, coloured red. I've attempted to define this in my data, but I come unstuck when I try and run this through ggplot
the way I have the code written now. Does anyone have any suggestions that may help? Example code is below.
Thank you.
library(tidyverse)
library(ggalt)
data <- tibble(
id = c(paste0("player", 1:5)),
x1 = c(0.219, 0.169, 0.103, 0.193, 0.345),
x2 = c(0.258, -0.030, 0.071, 0.315, 0.223),
x_diff = x2 - x1,
point_colour = ifelse(x_diff > 0, "#046A38", "#C60C30")
)
plot <- data %>%
ggplot() +
geom_dumbbell(aes(x = x1, xend = x2, y = id), size = 2, colour = "#E3E2E1",
size_x = 4, size_xend = 4, colour_xend = data$x_diff) +
theme_classic()
plot
Probably not the most elegant solution but you can overlap geom_point()
, and color it as you prefere:
data %>%
ggplot() +
geom_dumbbell(aes(x = x1, xend = x2, y = id),
size = 2, colour = "#E3E2E1",size_x = 4, size_xend = 4) +
geom_point(aes(x = x2, y = id), color = data$point_colour, size = 4) +
theme_classic()