Coloring points in dumbbell plot based on condition

Hi. I'm trying to color 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 colored green, and if its negative, colored red. I've attempted to define this in my data, but I come unstuck and get the below error when I try and run this through ggplot the way I have the code written now.

Error in $<*tmp*, "colour", value = c(0.039, -0.199, :
replacement has 5 rows, data has 1

Does anyone have any suggestions that may help?

Thank you.


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) +


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.