(source: http://www.flickr.com/photos/andysretrocomputers/4006418370/in/photostream/)
Author: Jo-fai Chow (woobe1208@yahoo.com)
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}.
The main purpose of this reference card is to provide a quick reference to the parameters needed for various colour settings.
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
Available parameters:
## 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")
Available parameters:
## 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")
Available parameters:
## 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")
## 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)
# 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")