11 Working with colors in R
You can find built-in named colors in R by using colors() function. There are 657 named colors.
You can see the color code here http://research.stowers-institute.org/efg/R/Color/Chart/ColorChart.pdf
Examples:
> x = c(4, 2, 6, 7, 8, 10)
> barplot(x, col = "steelblue") #color by name
> barplot(x, col = colors()[130]) #color by code
> barplot(x, col = "#EFEFEF") #color by hex code
> barplot(x, col = rgb(0, 0, 0.6)) #color by rgb value. Each shade value in range [0, 1]
> barplot(x, col = rgb(0, 0, 123, max = 256)) #color by rgb value
> barplot(x, col = c("darkblue", "darkred", "darkgreen")) #alternate coloring
Palette
> palette("default") #Set to default palette
> palette() #View palette
> barplot(x, col = 1:4) #display using palette colors 1 to 4.
> barplot(x, col = heat.colors(6))
More color palette:
https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/colorPaletteCheatsheet.pdf
Colorbrewer:
There are 3 types of palette
Sequential: represents range of values, for example age groups, temperature et
Qualitative: represents different groups of values for example, one color shade for each political parties, color of each county in the map
Diverging: Show high and low values, for example, positive and negative approval rating on a map where each extreme values get dark shades and middle lighter color representing neutral values
There is an R package for color brewer - "RColorBrewer". Install the package if not present in your R.
> require(RColorBrewer)
Shows all named palettes from RColorBrewer package
> display.brewer.all()
Display color from a named palette
> display.brewer.pal(9, "Blues")
Before you use the color brewer palette, save the palette in a vector
> x = c(4, 5, 2, 3, 10)
> blues = brewer.pal(5, "Blues")
> barplot(x, col = blues)