我正在尝试根据值是正值还是负值来xend
为我的哑铃图中的点着色。x_diff
本质上,如果x_diff
值为正,我希望xend
点为绿色,如果为负,则为红色。我试图在我的数据中定义它,但是当我尝试通过ggplot
我现在编写的代码的方式运行它时,我遇到了困难。有没有人有任何可能有帮助的建议?示例代码如下。
谢谢你。
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