18.2 Base R palettes

Base R provides built-in palettes, that can be leveraged in ggplot. For example:

  • rainbow()
  • heat.colors()
  • terrain.colors()
  • topo.colors()
  • cm.colors()

All of the above are functions that take one parameter n that is, the number of colors needed. The output is a vector of hex color codes, for example:

rainbow(5)
## [1] "#FF0000" "#CCFF00" "#00FF66" "#0066FF" "#CC00FF"
rainbow(10)
##  [1] "#FF0000" "#FF9900" "#CCFF00" "#33FF00" "#00FF66" "#00FFFF" "#0066FF" "#3300FF" "#CC00FF"
## [10] "#FF0099"

We can set them as values parameter in the scale_fill_manual() or scale_color_manual() layers:

ggplot(data=gtf, mapping=aes(x=chr, fill=gene_type)) + 
  geom_bar(position="dodge") +
  scale_fill_manual(values=rainbow(5))

ggplot(data=gtf, mapping=aes(x=chr, fill=gene_type)) + 
  geom_bar(position="dodge") +
  scale_fill_manual(values=terrain.colors(5))