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' )
The swemaps2
package provide map objects to easily make beautiful maps of Sweden in R.
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
by reinholdsson
You can install the package from github:
remotes::install_github("filipwastberg/swemaps2")
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).
county
Using these objects it is easy to use ggplot to create beautiful maps for counties (Län) and municipality (Kommun):
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):
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:
library(mapview) municipality %>% mutate(random_number = rnorm(nrow(.), mean = 100, sd = 10)) %>% mapView(zcol = "random_number")
knitr::include_graphics("man/figures/kommun-map.png")
The regional FA Region maps are also included:
fa_region %>% mutate(random_number = rnorm(nrow(.), mean = 100, sd = 10) ) %>% ggplot(aes(fill = random_number)) + geom_sf() + scale_fill_viridis_c() + theme_swemap()
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.
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()
deso %>% filter(kommunnamn == "Kalmar") %>% mutate(random_number = round(rnorm(nrow(.), mean = 100, sd = 10), 0)) %>% mapView(zcol = "random_number")
knitr::include_graphics("man/figures/kalmar-map.png")
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:
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()
Gothenburg has the largest map:
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/" )
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/" )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.