x <- c(12,4,21,17,13,9)

All gray and basic

barplot(x)

List of colors

head(colors())
## [1] "white"         "aliceblue"     "antiquewhite"  "antiquewhite1"
## [5] "antiquewhite2" "antiquewhite3"
barplot(x, col="moccasin")

Index number of tomato3 from colors()

barplot(x, col= colors()[633])

RGB Hexcodes

Can also use shortcut hexcodes (base 16), which are equivalent to RGB on the 0-255 scale, as FF in hex equals 255 in decimal.

blanchedalmond color

barplot(x, col="#FFEBCD") 

Same color

barplot(x,col= "blanchedalmond") 

Multiple Colors

If you have less colors than categories, the colors will cycle through

barplot(x, col = c("red", "blue")) 

barplot(x, col= c("magenta", "blue", "aquamarine3", "darkorchid3", "khaki2"))

Using Color Palettes

Can be more attractive and informative

help(package=colorspace)

Alternatively, you can use:

?palette
## starting httpd help server ... done

To see the current palette being used

palette()
## [1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow" 
## [8] "gray"

Default

barplot(x, col= 1:6) 

Indicate the numbers of colors needed

In this case, we need 6

barplot(x, col = rainbow (6)) 

A few examples

barplot(x, col = heat.colors(6))

barplot(x, col = terrain.colors(6))

barplot(x, col = topo.colors(6))

barplot(x, col = cm.colors(6))

RColorbrewer

Exploring Color with Colorbrewer to work with various palettes

x <- c(12,  4 , 21, 17, 13, 9)
barplot(x)

browseURL("http://colorbrewer2.org")
library(RColorBrewer)

See All Colors

display.brewer.all()

Display x Number of Colors in a Palette

display.brewer.pal(8, "Spectral")

barplot(x, col=brewer.pal(6, "Spectral"), border=FALSE)

Create a Vector for Shortcut

Spectral <- brewer.pal(6, "Spectral")
barplot(x, col=Spectral, border=FALSE)