Search code examples
rggplot2tidyversegeom-text

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


I want to customize the colors which map the default_binary variable in a geom_text. Tried using scale_fill_manual which works for a geom_bar, but nothing happens. Any ideas? Added the link for how it looks like rn in the end.[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)))

Put simply, I just want to substitute the pink and green with the blue and orange denoted by the rgb above (doesn't matter the order): [1]: https://i.sstatic.net/Dxzsk.png


Solution

  • As suggested above by changing the scale_color_manual to scale_color_manual you will get the desired result. geom_text does not have a fill aesthetic.

    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)
    

    Created on 2020-10-12 by the reprex package (v0.3.0)