-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreadme_raw.Rmd
183 lines (150 loc) · 5.92 KB
/
readme_raw.Rmd
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
---
title: "The Harry Potter Colour Palettes"
author:
- "Alejandro Jiménez Rico"
date: "`r Sys.Date()`"
output:
md_document:
variant: markdown_github
toc: true
toc_depth: 1
---
<style>
img {
max-width: 100%;
max-height: 100%;
}
</style>
# tl;dr
Use the color scales in this package to make plots that make use of the palette extracted from the *Harry Potter* movie franchise.
Install **hp** like any R package:
```{r}
library(devtools)
devtools::install_github("aljrico/harrypotter", force = TRUE)
library(harrypotter)
```
For base plots, use the `hp()` function to generate a palette:
```{r setup, include=FALSE}
library(harrypotter)
knitr::opts_chunk$set(echo = TRUE, fig.retina=2, fig.width=7, fig.height=5)
```
```{r tldr_base, message=FALSE}
x <- y <- seq(-8*pi, 8*pi, len = 40)
r <- sqrt(outer(x^2, y^2, "+"))
filled.contour(cos(r^2)*exp(-r/(2*pi)),
axes=FALSE,
color.palette=hp,
asp=1)
```
For ggplot, use `scale_colour_hp()` and `scale_fill_hp()`:
```{r, tldr_ggplot, message=FALSE}
library(ggplot2)
ggplot(data.frame(x = rnorm(10000), y = rnorm(10000)), aes(x = x, y = y)) +
geom_hex() + coord_fixed() +
scale_fill_hp(movie = "hufflepuff") + theme_bw()
```
# Introduction
The [**harrypotter**](http:://github.com/aljrico/harrypotter) package
brings to R colour scales created by [Alejandro Jimenez Rico](https://github.com/aljrico) from the [**Harry Potter** film series](https://en.wikipedia.org/wiki/Harry_Potter_(film_series))
# The Colour Scales
The package contains eight color scales: One for each movie of the franchise.
```{r for_repeat, include=FALSE}
n_col <- 128
img <- function(obj, nam) {
image(1:length(obj), 1, as.matrix(1:length(obj)), col=obj,
main = nam, ylab = "", xaxt = "n", yaxt = "n", bty = "n")
}
```
```{r begin, message=FALSE, include=FALSE}
library(harrypotter)
library(scales)
library(colorspace)
library(dichromat)
```
### Movies
```{r show_scales, echo=FALSE,fig.height=3.575}
par(mfrow=c(8, 1), mar=rep(1, 4))
img(rev(hp(n_col, movie = 1)), 1)
img(rev(hp(n_col, movie = 2)), 2)
img(rev(hp(n_col, movie = 3)), 3)
img(rev(hp(n_col, movie = 4)), 4)
img(rev(hp(n_col, movie = 5)), 5)
img(rev(hp(n_col, movie = 6)), 6)
img(rev(hp(n_col, movie = 7)), 7)
img(rev(hp(n_col, movie = 8)), 8)
```
### Houses
```{r show_scales2, echo=FALSE,fig.height=4}
par(mfrow=c(4, 1), mar=rep(1, 4))
img(rev(hp(n_col, movie = "slytherin")), "Slytherin")
img(rev(hp(n_col, movie = "gryffindor")), "Gryffindor")
img(rev(hp(n_col, movie = "ravenclaw")), "Ravenclaw")
img(rev(hp(n_col, movie = "hufflepuff")), "Hufflepuff")
```
# Usage
The `hp()` function produces the Harry Potter color scale. You can choose
the other color scale options using the `movie` parameter.
The package also contains color scale functions for **ggplot**
plots: `scale_color_hp()` and `scale_fill_hp()`. As with `hp()`, you can use the other scales with the `movie` argument
in the `ggplot` scales. Here the scale from the 7th is used for a cloropleth
map of U.S. unemployment:
```{r, ggplot2}
unemp <- read.csv("http://datasets.flowingdata.com/unemployment09.csv",
header = FALSE, stringsAsFactors = FALSE)
names(unemp) <- c("id", "state_fips", "county_fips", "name", "year",
"?", "?", "?", "rate")
unemp$county <- tolower(gsub(" County, [A-Z]{2}", "", unemp$name))
unemp$county <- gsub("^(.*) parish, ..$","\\1", unemp$county)
unemp$state <- gsub("^.*([A-Z]{2}).*$", "\\1", unemp$name)
county_df <- map_data("county", projection = "albers", parameters = c(39, 45))
names(county_df) <- c("long", "lat", "group", "order", "state_name", "county")
county_df$state <- state.abb[match(county_df$state_name, tolower(state.name))]
county_df$state_name <- NULL
state_df <- map_data("state", projection = "albers", parameters = c(39, 45))
choropleth <- merge(county_df, unemp, by = c("state", "county"))
choropleth <- choropleth[order(choropleth$order), ]
ggplot(choropleth, aes(long, lat, group = group)) +
geom_polygon(aes(fill = rate), colour = alpha("white", 1 / 2), size = 0.2) +
geom_polygon(data = state_df, colour = "white", fill = NA) +
coord_fixed() +
theme_minimal() +
ggtitle("US unemployment rate by county") +
theme(axis.line = element_blank(), axis.text = element_blank(),
axis.ticks = element_blank(), axis.title = element_blank()) +
scale_fill_hp(movie="gryffindor")
ggplot(choropleth, aes(long, lat, group = group)) +
geom_polygon(aes(fill = rate), colour = alpha("white", 1 / 2), size = 0.2) +
geom_polygon(data = state_df, colour = "white", fill = NA) +
coord_fixed() +
theme_minimal() +
ggtitle("US unemployment rate by county") +
theme(axis.line = element_blank(), axis.text = element_blank(),
axis.ticks = element_blank(), axis.title = element_blank()) +
scale_fill_hp(movie="slytherin")
ggplot(choropleth, aes(long, lat, group = group)) +
geom_polygon(aes(fill = rate), colour = alpha("white", 1 / 2), size = 0.2) +
geom_polygon(data = state_df, colour = "white", fill = NA) +
coord_fixed() +
theme_minimal() +
ggtitle("US unemployment rate by county") +
theme(axis.line = element_blank(), axis.text = element_blank(),
axis.ticks = element_blank(), axis.title = element_blank()) +
scale_fill_hp(movie="ravenclaw")
ggplot(choropleth, aes(long, lat, group = group)) +
geom_polygon(aes(fill = rate), colour = alpha("white", 1 / 2), size = 0.2) +
geom_polygon(data = state_df, colour = "white", fill = NA) +
coord_fixed() +
theme_minimal() +
ggtitle("US unemployment rate by county") +
theme(axis.line = element_blank(), axis.text = element_blank(),
axis.ticks = element_blank(), axis.title = element_blank()) +
scale_fill_hp(movie="hufflepuff")
```
The ggplot functions also can be used for discrete scales with the argument
`discrete=TRUE`.
```{r discrete}
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(size=4, aes(colour = factor(carb))) +
scale_color_hp(discrete=TRUE, movie = 1) +
theme_bw()
```