alt text

(source: http://www.flickr.com/photos/andysretrocomputers/4006418370/in/photostream/)

ggplot2 Reference and Examples (Part 2) - Colours

Author: Jo-fai Chow (woobe1208@yahoo.com)

Introduction

One of my goals this year is to master the art of graphics in R with ggplot2. Unfortunately, my brain can't cope with all the details. That's why I decided to create this page as a R graphics cheat sheet for years to come. You will need the R package {ggplot2} for the following plots. The qlot() function used extensively in the previous reference card has its limitation. From now on, ggplot() is used. It is a natural progression of learning {ggplot2}.

Objective

The main purpose of this reference card is to provide a quick reference to the parameters needed for various colour settings.

References:

Reference Card (Part 1): http://rpubs.com/woobe/ggplot2_ref_part01

R Markdown file (Part 1): https://dl.dropbox.com/u/103222/R_Markdown_Html/jchow_ggplot2_ref_part01.rmd

Reference Card (Part 2): http://rpubs.com/woobe/ggplot2_ref_part02

R Markdown file (Part 2): https://dl.dropbox.com/u/103222/R_Markdown_Html/jchow_ggplot2_ref_part02.rmd

Samsung Smartphone Data: http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones

Qualitative Palettes

Available parameters:

  1. Accent (8 levels)
  2. Dark2 (8)
  3. Paired (12)
  4. Pastel1 (9)
  5. Pastel2 (8)
  6. Set1 (9)
  7. Set2 (8)
  8. Set3 (12)
## Using qualitative palettes (change the parameter: palette = ???)
plot1 <- ggplot(samsungData, aes(x = as.factor(subject), fill = activity)) + 
    geom_bar(colour = "black", position = "stack") + scale_fill_brewer(palette = "Accent") + 
    labs(title = "Accent")

plot of chunk unnamed-chunk-6

Sequential Palettes

Available parameters:

  1. Blues
  2. BuGn
  3. BuPu
  4. GnBu
  5. Greens
  6. Greys
  7. Oranges
  8. OrRd
  9. PuBu
  10. PuBuGn
  11. PuRd
  12. Purples
  13. RdPu
  14. Reds
  15. YlGn
  16. YlGnBu
  17. YlOrBr
  18. YlOrRd
## Using sequential palettes (change the parameter: palette = ???)
plot1 <- ggplot(samsungData, aes(x = as.factor(subject), fill = activity)) + 
    geom_bar(colour = "black", position = "stack") + scale_fill_brewer(palette = "Blues") + 
    labs(title = "Blues")

plot of chunk unnamed-chunk-9

Diverging Palettes

Available parameters:

  1. BrBG
  2. PiYG
  3. PRGn
  4. PuOr
  5. RdBu
  6. RdGy
  7. RdYlBu
  8. RdYlGn
  9. Spectral
## Using diverging palettes (change the parameter: palette = ???)
plot1 <- ggplot(samsungData, aes(x = as.factor(subject), fill = activity)) + 
    geom_bar(colour = "black", position = "stack") + scale_fill_brewer(palette = "BrBG") + 
    labs(title = "BrBG")

plot of chunk unnamed-chunk-12

Different Colours for Positive and Negative Values

## Generate random values
set.seed(1234)
x <- c(1:20)
y <- rnorm(20) * 10
df <- data.frame(x = x, y = y, pos = (y >= 0))

## Using 'pos' in data frame to fill
plot1 <- ggplot(df, aes(x = x, y = y, fill = pos)) + geom_bar(stat = "identity", 
    position = "identity") + scale_fill_manual(values = c("red", "steelblue"))

plot2 <- ggplot(df, aes(x = x, y = y, fill = pos)) + geom_bar(stat = "identity", 
    position = "identity", colour = "black", size = 0.25) + scale_fill_manual(values = c("red", 
    "steelblue"), guide = FALSE)

plot of chunk unnamed-chunk-14

Manual Colour

# Graph with manual colours
ggplot(samsungData, aes(x = as.factor(subject), fill = activity)) + geom_bar(colour = "black", 
    position = "stack") + scale_fill_manual(values = c("#000000", "#736F6E", 
    "#C0C0C0", "#98AFC7", "#6698FF", "#153E7E")) + labs(title = "Manaul")

plot of chunk unnamed-chunk-15

Code reference (Top 50 Most Viewed Colours)

alt text

(source: http://www.computerhope.com/cgi-bin/htmlcolor.pl)