Color Groups
RColorBrewer is an R packages that uses the work from http://colorbrewer2.org/ to help you choose sensible colour schemes for figures in R. The colors are split into three group, sequential, diverging, and qualitative.
- Sequential – Light colours for low data, dark for high data
- Diverging – Light colours for mid-range data, low and high contrasting dark colours
- Qualitative – Colours designed to give maximum visual difference between classes
The Palettes
Palettes from RColorBrewer can be displayed with the display.brewer.all() function.
R Commands
Palette and color count are selected using brewer.pal(n, name), where name is a character string. The result is a set of hexadecimal codes. Configuration can be confirmed by using display.brewer.pal(n, name).
ggplot Commands
The scale commands for ggplot are scale_fill_brewer() and scale_color_brewer().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# RColorBrewer data <- data.frame(obs = LETTERS[1:8], yval = c(1.25, 1.75, 1.5, 2.25, 2.75, 2.5, 1.4, 2)) # Sequential colors p1 <- ggplot(data, aes(x = obs, y = yval, fill = obs)) + geom_bar() + scale_fill_brewer(palette = "Blues") # Qualitative colors p2 <- ggplot(data, aes(x = obs, y = yval, fill = obs)) + geom_bar() + scale_fill_brewer(palette = "Set2") # Qualitative colors p3 <- ggplot(data, aes(x = obs, y = yval, fill = obs)) + geom_bar() + scale_fill_brewer(palette = "RdYlGn") multiplot(p1, p2, p3, layout = matrix(c(1, 2, 3), nrow = 1)) |