Permalink
Newer
Older
100644 63 lines (47 sloc) 1.72 KB
@tavisrudd
Mar 22, 2011
1
#http://learnr.wordpress.com/2010/01/21/ggplot2-crayola-crayon-colours/
2
library(Cairo)
3
library(XML)
4
library(ggplot2)
5
library(colorspace)
6
7
## scrape the data from Wikipedia
8
crayola <- readHTMLTable(
9
htmlParse("http://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors"),
10
stringsAsFactors=FALSE)[[2]]
11
12
13
## take a subset and assign names to the columns
14
crayola <- crayola[, c("Hex Code", "Issued", "Retired")]
15
names(crayola) <- c("colour", "issued", "retired")
16
17
18
## remove duplicates
19
crayola <- crayola[!duplicated(crayola$colour), ]
20
21
crayola$retired[crayola$retired == ""] <- 2010
22
23
## colours <- ddply(crayola,
24
## .(colour),
25
## transform,
26
## year=issued:retired)
27
28
## p <- (ggplot(colours, aes(year, 1, fill=colour))
29
## + geom_bar(width=1, position="fill", binwidth=1)
30
## + theme_bw()
31
## + scale_fill_identity())
32
33
p1 <- (ggplot(colours, aes(year, 1, fill=colour))
34
+ geom_area(position="fill", colour="white")
35
+ theme_bw() + scale_fill_identity())
36
37
## configure our axes
38
labels <- c(1903, 1949, 1958, 1972, 1990, 1998, 2010)
39
breaks <- labels - 1
40
p2 <- (p1
41
+ scale_x_continuous("", breaks=breaks, labels=labels, expand=c(0, 0))
42
+ scale_y_continuous("", expand=c(0, 0))
43
+ opts(axis.text.y=theme_blank(), axis.ticks=theme_blank()))
44
45
p2
46
47
sort.colours <- function(col) {
48
c.rgb = col2rgb(col)
49
c.RGB = RGB(t(c.rgb) %*% diag(rep(1/255, 3)))
50
c.HSV = as(c.RGB, "HSV")@coords
51
order(c.HSV[, 1], c.HSV[, 2], c.HSV[, 3])
52
}
53
54
colours <- ddply(colours,
55
.(year),
56
function(d) {
57
d[rev(sort.colours(d$colour)),]
58
})
59
60
p2 %+% colours
61
62
## etags --language=none --regex='/\([^ \t]+\)[ \t]*<-[ \t]*function/\1/' *.R