如何在geom_text中自定义映射变量的颜色?

How to customize the color of mapped variable in a geom_text?

我想自定义在 geom_text 中映射 default_binary 变量的颜色。尝试使用适用于 geom_barscale_fill_manual,但没有任何反应。有任何想法吗?在最后添加了 link 来表示 rn 的样子。[1]

ggplot(DATA) +
  geom_text(aes(x = recession_binary, y = percents, label = percents, color = default_binary)) +
  scale_fill_manual("", values = c("Default" = rgb(232/255,74/255,39/255), "Paid in full" = rgb(19/255,41/255,75/255)))

简单来说,我只想把上面rgb表示的蓝色和橙色替换成粉色和绿色(顺序无关紧要): [1]: https://i.stack.imgur.com/Dxzsk.png

如上所示,通过将 scale_color_manual 更改为 scale_color_manual,您将获得所需的结果。 geom_text 没有 fill 美学。

library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.3
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'readr' was built under R version 3.6.3
#> Warning: package 'purrr' was built under R version 3.6.3
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'stringr' was built under R version 3.6.3
#> Warning: package 'forcats' was built under R version 3.6.3
mpg %>% 
  filter(class %in% c('compact', 'minivan'), cyl %in% c(4, 6)) %>% 
  group_by(class, cyl) %>% 
  summarise(cty= mean(cty)) %>% 
  ggplot(aes(class, cty, color=as_factor(cyl), label= cyl) ) +
  geom_text()+
  scale_color_manual(values = c(rgb(232/255,74/255,39/255), 
                                cty= rgb(19/255,41/255,75/255)))
#> `summarise()` regrouping output by 'class' (override with `.groups` argument)

reprex package (v0.3.0)

于 2020-10-12 创建