-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
183 lines (140 loc) · 3.85 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
---
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "75%",
warning = FALSE,
message = FALSE,
fig.retina = 2,
fig.align = 'center'
)
```
# swemaps2
The `swemaps2` package provide map objects to easily make beautiful maps of Sweden in R.
```{r echo=FALSE}
library(swemaps2)
library(tidyverse)
library(sf)
county %>%
mutate(rn = rnorm(nrow(.), mean = 100, sd = 10)) %>%
ggplot(aes(fill = rn)) +
geom_sf(show.legend = FALSE) +
theme_swemap2() +
scale_fill_viridis_c(option = "magma")
```
This is the successor of [`swemaps`](https://github.com/reinholdsson/swemaps) by [reinholdsson](https://github.com/reinholdsson)
## Installation
You can install the package from github:
```{r eval=FALSE}
remotes::install_github("filipwastberg/swemaps2")
```
## Country maps
`swemaps2` contain simple features (sf) objects that make it easy to create maps over Sweden.
You can use these basic sf-objects to combine with data from SCB (using the [pxweb package](http://ropengov.github.io/pxweb/)).
```{r}
county
```
Using these objects it is easy to use ggplot to create beautiful maps for counties (Län) and municipality (Kommun):
```{r}
library(swemaps2)
library(tidyverse)
library(ggthemes)
county %>%
mutate(random_number = rnorm(nrow(.), mean = 100, sd = 10) ) %>%
ggplot(aes(fill = random_number)) +
geom_sf() +
scale_fill_viridis_c() +
theme_swemap2()
```
Municipality (kommun):
```{r}
municipality %>%
mutate(random_number = rnorm(nrow(.), mean = 100, sd = 10) ) %>%
ggplot(aes(fill = random_number)) +
geom_sf() +
scale_fill_viridis_c() +
theme_swemap2()
```
Using a package like `leaflet` or `mapview` you can create interactive maps:
```{r eval=FALSE}
library(mapview)
municipality %>%
mutate(random_number = rnorm(nrow(.), mean = 100, sd = 10)) %>%
mapView(zcol = "random_number")
```
```{r echo=FALSE}
knitr::include_graphics("man/figures/kommun-map.png")
```
The regional FA Region maps are also included:
```{r}
fa_region %>%
mutate(random_number = rnorm(nrow(.), mean = 100, sd = 10) ) %>%
ggplot(aes(fill = random_number)) +
geom_sf() +
scale_fill_viridis_c() +
theme_swemap()
```
## DeSO and RegSO
Statistics Sweden has two demographic maps that you can load using the `load_deso()` and `load_regso()` functions. These objects are big and works best if you first filter them.
```{r}
deso <- load_deso()
deso %>%
filter(kommunnamn == "Kalmar") %>%
mutate(random_number = rnorm(nrow(.), mean = 100, sd = 10)) %>%
ggplot(aes(fill = random_number)) +
geom_sf() +
labs(
title = "Map over Kalmar"
) +
theme_swemap2()
```
```{r eval=FALSE}
deso %>%
filter(kommunnamn == "Kalmar") %>%
mutate(random_number = round(rnorm(nrow(.), mean = 100, sd = 10), 0)) %>%
mapView(zcol = "random_number")
```
```{r echo=FALSE}
knitr::include_graphics("man/figures/kalmar-map.png")
```
## City maps
### Stockholm
In addition there are maps for the three largest Swedish cities Stockholm, Göteborg and Malmö.
The Stockholm map, for example, also has some additional population data:
```{r}
ggplot(sthlm, aes(fill = kvinnor_45)) +
geom_sf() +
scale_fill_viridis_c() +
labs(
title = "Stockholm women 45",
caption = "Source: https://dataportalen.stockholm.se"
) +
theme_swemap2()
```
## Göteborg
Gothenburg has the largest map:
```{r}
ggplot(gbg, aes(fill = area_km2)) +
geom_sf() +
scale_fill_viridis_c() +
theme_swemap2() +
labs(
title = "Gothenburg by area size",
caption = "Source: http://statistikdatabas.goteborg.se/pxweb/sv/"
)
```
## Malmö
```{r}
ggplot(malmo, aes(fill = area)) +
geom_sf() +
scale_fill_viridis_c(labels = scales::number) +
theme_swemap2() +
labs(
title = "Malmö by area size",
caption = "Source: https://malmo.dataplatform.se/"
)
```