-
Notifications
You must be signed in to change notification settings - Fork 6
/
intro-to-hp.Rmd
173 lines (141 loc) · 5.63 KB
/
intro-to-hp.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
---
title: "The Game of Thrones Colour Palettes"
author:
- "Alejandro Jiménez Rico"
date: "`r Sys.Date()`"
output:
rmarkdown::html_vignette:
toc: true
toc_depth: 1
vignette: >
%\VignetteIndexEntry{The Game of Thrones Colour Palettes}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
<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 inspired by the TV show *Game of Thrones*.
Install **got** like any R package:
```{r}
library(devtools)
devtools::install_github("aljrico/gameofthrones", force = TRUE)
library(gameofthrones)
```
For base plots, use the `got()` function to generate a palette:
```{r setup, include=FALSE}
library(gameofthrones)
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=got,
asp=1)
```
For ggplot, use `scale_colour_got()` and `scale_fill_got()`:
```{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_got(house = "baratheon") + theme_bw()
```
# Introduction
The [**gameofthrones**](http:://github.com/aljrico/gameofthrones) package
brings to R colour scales created by [Alejandro Jimenez Rico](https://github.com/aljrico) from the [**Game of Thrones** TV show](https://en.wikipedia.org/wiki/Game_of_Thrones) s
# The Colour Scales
The package contains many colour scales.
```{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(gameofthrones)
library(scales)
library(colorspace)
library(dichromat)
```
### Houses
```{r show_scales2, echo=FALSE,fig.height=4}
par(mfrow=c(4, 1), mar=rep(1, 4))
img(rev(got(n_col, house = "martell")), "martell")
img(rev(got(n_col, house = "stark")), "stark")
img(rev(got(n_col, house = "targaryen2")), "targaryen2")
img(rev(got(n_col, house = "baratheon")), "baratheon")
```
# Usage
The `got()` function produces the Game of Thrones color scale. You can choose
the other color scale options using the `house` parameter.
The package also contains color scale functions for **ggplot**
plots: `scale_color_got()` and `scale_fill_got()`. As with `got()`, you can use the other scales with the `house` argument
in the `ggplot` scales. Here this 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_got(house="stark")
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_got(house="martell")
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_got(house="targaryen2")
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_got(house="baratheon")
```
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_got(discrete=TRUE, house = "targaryen2") +
theme_bw()
```