-2

How do I flip the default ordering of colors which ggplot picks for factor variable passed onto 'fill' aesthetic. (Here, I have a factor variable with two levels Won and Lost).

Ref image below, I'd like this 'red-family' shade to go with Lost; As it is, 'red' gets used for Won category instead of Lost, which in my view doesn't gel with one's common intuition of colors.

  ggplot(data, mapping = aes(x = Party,  y = Votes, fill = Result)) +  
     geom_bar(stat = "identity", width = .6)

Votes scored by Political Parties in Indian State of TamilNadu: Votes scored by Political Parties in Indian State of TamilNadu

Community
  • 1
  • 1
cosmos
  • 85
  • 1
  • 7
  • 1
    Possible duplicate of [Change bar plot colour in geom\_bar with ggplot2 in r](https://stackoverflow.com/questions/38788357/change-bar-plot-colour-in-geom-bar-with-ggplot2-in-r) – Hardik Gupta Oct 26 '17 at 08:18
  • 1
    This previous answers shows how to emulate `ggplot2` color scheme :) https://stackoverflow.com/questions/8197559/emulate-ggplot2-default-color-palette – Djork Oct 26 '17 at 09:45

2 Answers2

3

Thanks @Djork for sharing the link which answers 'Emulate ggplot default color-palette'

I used this code-chunk to preview and select color-codes of interest, from default-palette,
library(scales) show_col(hue_pal()(4)) hue_pal()(4)

and later,
+ scale_fill_manual( values = c("#7CAE00", "#F8766D"))
to use the 'green' and 'red' chosen from default hue-palette.

To clarify, I found manual-colors resulting from 'green', 'red' custom-labels (as suggested in comments above) to be of very high contrast. With the hue-palette, I get nuanced color-tones.

cosmos
  • 85
  • 1
  • 7
0

We can give the colors custom values.

ggplot(data, mapping = aes(x = Party,  y = Votes, fill = Result))
+ geom_bar(stat = "identity", width = .6)
+ scale_fill_manual(values = c("green", "yellow"))
jay.sf
  • 60,139
  • 8
  • 53
  • 110