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