-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathreadme_raw.Rmd
241 lines (172 loc) · 7.87 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
---
title: "The Game of Thrones Colour Palettes"
author:
- "Alejandro Jiménez Rico"
date: "`r Sys.Date()`"
output:
md_document:
variant: markdown_github
toc: true
toc_depth: 1
---
This package provides a round of palettes inspired by the *Game of Thrones* TV show.
At its first version, it simply contains the palettes of some of the houses of Westeros. They have been chosen manually, taking into account its consistency with all the existing branding of the franchise, but its suitability for data visualisation.
> Information visualization is just a language with everything to be discovered.
The colour palette should be beautiful, useful for plotting data and should relate to desired style; in this case, should relate to the _Song of Ice and Fire_ world. Some of the colours might change in future versions, in order to find this balance between suitability for plotting and relatability to the images from the universe.
<center><img src="https://raw.githubusercontent.com/aljrico/aljrico.github.io/master/_posts/images/show_scales2-1.png" ></center>
Installation
-------------
Just copy and execute this bunch of code and you'll have the last version of the package installed:
```{r, eval = FALSE}
devtools::install_github("aljrico/gameofthrones")
```
And you can now use it:
```{r}
library(gameofthrones)
```
Usage
-----
The default colour scale of the package is the one of the house *Targaryen*. If you prefer to choose another one, you'll need to specify which house you want the palette from.
Let's say that you want a palette made from the house **Targaryen**.
```{r}
pal <- got(250, option = "Targaryen")
image(volcano, col = pal)
```
<center><img src="https://raw.githubusercontent.com/aljrico/aljrico.github.io/master/_posts/images/unnamed-chunk-3-1.png" ></center>
Or that you want burn something down using *wildfire*.
```{r}
pal <- got(250, option = "Wildfire")
image(volcano, col = pal)
```
### ggplot2
Of course, this package has specific functions to behave seamlessly with the best data visiualisation library available.
The package contains colour scale functions for **ggplot2** plots: `scale_color_got()` and `scale_fill_got()`.
Here is a made up example using the colours from the house of **Martell**,
```{r}
library(ggplot2)
ggplot(data.frame(x = rnorm(1e4), y = rnorm(1e4)), aes(x = x, y = y)) +
geom_hex() +
coord_fixed() +
scale_fill_got(option = "Martell") +
theme_bw()
```
and **Baratheon**
```{r}
ggplot(data.frame(x = rnorm(1e4), y = rnorm(1e4)), aes(x = x, y = y)) +
geom_hex() +
coord_fixed() +
scale_fill_got(option = "Baratheon") +
theme_bw()
```
You can also use it to create this cloropeths of the U.S Unemployment:
```{r}
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") +
labs(subtitle = "House Tully palette") +
theme(axis.line = element_blank(), axis.text = element_blank(),
axis.ticks = element_blank(), axis.title = element_blank()) +
scale_fill_got(option = "Tully")
```
But what if you want discrete scales? These functions also can be used for discrete scales with the argument `discrete = TRUE`. This argument, when TRUE, sets a finite number of sufficiently spaced colours within the selected palette to plot your data.
```{r}
library(gridExtra)
gg1 <- ggplot(diamonds, aes(factor(color), fill=factor(cut))) +
geom_bar(colour = "black") +
scale_fill_got(discrete = TRUE, option = "Margaery") +
ggtitle("Lady Margaery") +
theme_minimal()
gg2 <- ggplot(diamonds, aes(factor(color), fill=factor(cut))) +
geom_bar(colour = "black") +
scale_fill_got(discrete = TRUE, option = "Daenerys", direction = - 1) +
ggtitle("Daenerys Stormborn") +
theme_minimal()
grid.arrange(gg1,gg2)
```
Note that you can also play with the direction of the colour map. Either the default `direction = 1` or the opposite `direction = -1`.
```{r}
library(geojsonio)
library(tidyverse)
spdf <- geojson_read("https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/communes.geojson", what = "sp")
# Since it is a bit to much data, I select only a subset of it:
spdf = spdf[ substr(spdf@data$code,1,2) %in% c("06", "83", "13", "30", "34", "11", "66") , ]
# I need to fortify the data AND keep trace of the commune code! (Takes 2 minutes)
library(broom)
spdf_fortified <- tidy(spdf, region = "code")
# read data
data=read.table("https://www.r-graph-gallery.com/wp-content/uploads/2017/12/data_on_french_states.csv", header=T, sep=";")
head(data)
# Make the merge
spdf_fortified = spdf_fortified %>%
left_join(. , data, by=c("id"="depcom"))
# Note that if the number of restaurant is NA, it is in fact 0
spdf_fortified$nb_equip[ is.na(spdf_fortified$nb_equip)] = 0.001
p1 <- ggplot() +
geom_polygon(data = spdf_fortified, aes(fill = nb_equip, x = long, y = lat, group = group) , size=0, alpha=0.9) +
theme_void() +
scale_fill_got(option = "Wildfire", trans = "log", breaks=c(1,5,10,20,50,100), name="Number of restaurant", guide = guide_legend( keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"), label.position = "bottom", title.position = 'top', nrow=1) ) +
theme(legend.position = "none") +
coord_map()
p2 <- ggplot() +
geom_polygon(data = spdf_fortified, aes(fill = nb_equip, x = long, y = lat, group = group) , size=0, alpha=0.9) +
theme_void() +
scale_fill_got(option = "Wildfire", direction = -1, trans = "log", breaks=c(1,5,10,20,50,100), name="Number of restaurant", guide = guide_legend( keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"), label.position = "bottom", title.position = 'top', nrow=1) ) +
theme(legend.position = "none") +
coord_map()
grid.arrange(p1,p2, ncol = 2)
```
```{r}
gg1 <- ggplot(diamonds, aes(carat, fill = cut)) +
geom_density(position = "stack") +
scale_fill_got_d(option = "Stark2")
gg2 <- ggplot(mpg, aes(class)) +
geom_bar(aes(fill = drv), position = position_stack(reverse = TRUE), colour = "black") +
coord_flip() +
scale_fill_got_d(option = "Daenerys") +
theme(legend.position = "top") +
ylab("") +
xlab("Class")
grid.arrange(gg1,gg2)
```
```{r}
gg <- ggplot(diamonds, aes(carat, stat(count), fill = cut)) +
geom_density(position = "fill") +
xlab("") +
ylab("")
gg1 <- gg +
scale_fill_got(discrete = TRUE, option = "white_walkers", name = "") +
ggtitle("White Walkers Palette")
gg2 <- gg +
scale_fill_got(discrete = TRUE, option = "Lannister", name = "") +
ggtitle("House Lannister Palette")
gg3 <-gg +
scale_fill_got(discrete = TRUE, option = "Tyrell", name = "") +
ggtitle("House Tyrell Palette")
gg4 <- gg +
scale_fill_got(discrete = TRUE, option = "Stark", name = "") +
ggtitle("House Stark Palette")
gg5 <- gg +
scale_fill_got(discrete = TRUE, option = "Tully", name = "") +
ggtitle("House Tully Palette")
gg6 <- gg +
scale_fill_got(discrete = TRUE, option = "Martell", name = "") +
ggtitle("House Martell Palette")
grid.arrange(gg1,gg2,gg3,gg4,gg5,gg6)
```