-
Notifications
You must be signed in to change notification settings - Fork 12
/
README.Rmd
236 lines (184 loc) · 7.06 KB
/
README.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
---
output: github_document
editor_options:
chunk_output_type: console
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
# urbnthemes <a href="https://UrbanInstitute.github.io/urbnthemes/"><img src="man/figures/hexsticker.png" align="right" height="160"/></a>
```{r, echo = FALSE}
knitr::opts_chunk$set(
comment = "#>",
fig.path = "man/figures/README-"
)
knitr::opts_chunk$set(warning = FALSE)
```
## Overview
`urbnthemes` is a set of tools for creating Urban Institute-themed plots and maps in R. The package extends `ggplot2` with print and map themes as well as tools that make plotting easier at the Urban Institute.
A comprehensive set of examples is available at the [Urban Institute R Users Group website](https://UrbanInstitute.github.io/r-at-urban/graphics-guide.html).
## Stay up-to-date
Sign up [here](https://app.smartsheet.com/b/form/9029a0a8254e4044a52cdebaebe343bf) to stay up-to-date with R package releases and R resources from the Urban Institute. We will not share your information and we will not email more than once per month.
## Installation
```
install.packages("remotes")
remotes::install_github("UrbanInstitute/urbnthemes", build_vignettes = TRUE)
```
## Fonts
The Urban Institute uses [Lato](https://fonts.google.com/specimen/Lato) font for publications. After installing `urbnthemes`, submit `urbnthemes::lato_test()` to see if Lato is imported and registered.
If Lato isn't imported and registered, install [Lato](https://fonts.google.com/specimen/Lato) and then submit `urbnthemes::lato_import()`. If you are on a Windows, you may need to install [ghostscript](https://www.ghostscript.com/download.html) and then submit `Sys.setenv(R_GSCMD = "link to the ghostscript .exe")` before running `urbnthemes::lato_import()`.
Waffle charts with glyphs require fontawesome. `fontawesome_test()` and `fontawesome_install()` are the fontawesome versions of the above functions. Be sure to install fontawesome from [here](https://github.com/hrbrmstr/waffle/tree/master/inst/fonts).
## Usage
Always load `library(urbnthemes)` after `library(ggplot2)` or `library(tidyverse)`.
```{r basic-example, message=FALSE}
library(tidyverse)
library(urbnthemes)
set_urbn_defaults(style = "print")
ggplot(data = mtcars, mapping = aes(factor(cyl))) +
geom_bar() +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(x = "Number of Cylinders",
y = "Count") +
remove_ticks() +
labs(title = "Most Cars Have 8 Cylinders",
subtitle = "1974 Motor Trend US magazine")
```
```{r scatter-plot-1}
ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) +
geom_point() +
scale_x_continuous(expand = expansion(mult = c(0.002, 0)),
limits = c(0, 6),
breaks = 0:6) +
scale_y_continuous(expand = expansion(mult = c(0, 0.002)),
limits = c(0, 40),
breaks = 0:8 * 5) +
labs(x = "Weight (thousands of pounds)",
y = "City MPG") +
scatter_grid()
```
```{r scatter-plot-2}
mtcars %>%
mutate(cyl = paste(cyl, "cylinders")) %>%
ggplot(aes(x = wt, y = mpg, color = cyl)) +
geom_point() +
scale_x_continuous(expand = expansion(mult = c(0.002, 0)),
limits = c(0, 6),
breaks = 0:6) +
scale_y_continuous(expand = expansion(mult = c(0, 0.002)),
limits = c(0, 40),
breaks = 0:8 * 5) +
labs(x = "Weight (thousands of pounds)",
y = "City MPG") +
scatter_grid()
```
```{r line-plot}
library(gapminder)
gapminder %>%
filter(country %in% c("Australia", "Canada", "New Zealand")) %>%
mutate(country = factor(country, levels = c("Canada", "Australia", "New Zealand"))) %>%
ggplot(aes(year, gdpPercap, color = country)) +
geom_line() +
scale_x_continuous(expand = expansion(mult = c(0.002, 0)),
breaks = c(1952 + 0:12 * 5),
limits = c(1952, 2007)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.002)),
breaks = 0:8 * 5000,
labels = scales::dollar,
limits = c(0, 40000)) +
labs(x = "Year",
y = "Per capita GDP (US dollars)")
```
```{r area-plot}
txhousing %>%
filter(city %in% c("Austin","Houston","Dallas","San Antonio","Fort Worth")) %>%
group_by(city, year) %>%
summarize(sales = sum(sales)) %>%
ggplot(aes(x = year, y = sales, fill = city)) +
geom_area(position = "stack") +
scale_x_continuous(expand = expansion(mult = c(0, 0)),
limits = c(2000, 2015),
breaks = 2000 + 0:15) +
scale_y_continuous(expand = expansion(mult = c(0, 0.2)),
labels = scales::comma) +
labs(x = "Year",
y = "Home sales")
```
## Branding
```{r branding}
library(ggplot2)
library(urbnthemes)
set_urbn_defaults()
plot <- ggplot(data = mtcars, mapping = aes(factor(cyl))) +
geom_bar() +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(x = "Number of Cylinders",
y = "Count") +
remove_ticks()
urbn_plot(plot, urbn_logo_text(), ncol = 1, heights = c(30, 1))
```
## Notes and Sources
```{r notes-and-sources}
library(ggplot2)
library(urbnthemes)
set_urbn_defaults()
plot <- ggplot(data = mtcars, mapping = aes(factor(cyl))) +
geom_bar() +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(x = "Number of Cylinders",
y = NULL) +
remove_ticks()
urbn_plot(
urbn_y_title(string = "Count"),
plot,
urbn_logo_text(),
urbn_source(text = "This is a long source. This is a long source. This is a long source. This is a long source. This is a long source. This is a long source. This is a long source. ",
width = 155),
urbn_note(text = "This is a long note. This is a long note. This is a long note. This is a long note. This is a long note. This is a long note. This is a long note. This is a long note.",
width = 155,
plural = TRUE),
ncol = 1,
heights = c(1, 30, 1.5, 2.5, 2.5)
)
```
Core themes:
* `set_urbn_defaults()`
* `theme_urbn_print()`
* `theme_urbn_map()`
Formatting functions:
* `urbn_plot()`
* `urbn_title()`
* `urbn_subtitle()`
* `urbn_y_title()`
* `urbn_note()`
* `urbn_source()`
* `urbn_logo_text()`
* `scatter_grid()`
* `remove_ticks()`
* `add_axis()`
* `remove_axis()`
* `get_legend()`
* `remove_legend()`
* `urbn_geofacet`
Palette functions:
* `palette_urbn_main`
* `palette_urbn_diverging`
* `palette_urbn_quintile`
* `palette_urbn_politics`
* `palette_urbn_cyan`
* `palette_urbn_gray`
* `palette_urbn_yellow`
* `palette_urbn_magenta`
* `palette_urbn_green`
* `palette_urbn_spacegray`
* `palette_urbn_red`
Utility functions:
* `lato_test()`
* `lato_install()`
* `fontawesome_test()`
* `fontawesome_install()`
* `view_palette()`
In development:
* `undo_urbn_defaults()`
* `save_urbn_print()`
## Getting help
Contact [Aaron R. Williams]([email protected]) or [Kyle Ueyama]([email protected]) with feedback or questions.
## Code of conduct
Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.