在我的情节中,我想同时更改点的颜色和这些点上标签的颜色。我可以独立控制点和标签的颜色,但是在同一图中会分解。我已经为所有三种情况提供了代码。
library(dplyr)
library(ggplot2)
ds <-
mtcars %>%
count(cyl, vs) %>%
mutate(
label_color = case_when(
n > median(n) ~ "black",
TRUE ~ "white")
)
# Works
ds %>%
ggplot(aes(x = as.character(vs), y = cyl, size = n, color = n, label = n)) +
geom_point() +
geom_text(aes(color = label_color)) +
scale_color_identity() #+
#scale_color_gradient(low = "red", high = "blue")
# Works
ds %>%
ggplot(aes(x = as.character(vs), y = cyl, size = n, color = n, label = n)) +
geom_point() +
geom_text(aes(color = label_color)) +
#scale_color_identity() +
scale_color_gradient(low = "red", high = "blue")
# Fails
ds %>%
ggplot(aes(x = as.character(vs), y = cyl, size = n, color = n, label = n)) +
geom_point() +
geom_text(aes(color = label_color)) +
scale_color_identity() +
scale_color_gradient(low = "red", high = "blue")