在同一图中缩放多个颜色参数

时间:2018-08-01 20:50:29

标签: r ggplot2

在我的情节中,我想同时更改点的颜色和这些点上标签的颜色。我可以独立控制点和标签的颜色,但是在同一图中会分解。我已经为所有三种情况提供了代码。

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

0 个答案:

没有答案