library(ggplot2)   
library(ggmap)
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(ggthemes)
library(broom)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(readr)
library(stringr)
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## 
## The following object is masked from 'package:purrr':
## 
##     flatten
library(leaflet)
library(leaflet.providers)
library(RColorBrewer)
library(htmlwidgets)
geojson_data <- st_read("stanford-vj304zk6062-geojson.json") 
## Reading layer `stanford-vj304zk6062-geojson' from data source 
##   `/home/jpearlma@macalester.edu/Intro to Data Science/mt/stanford-vj304zk6062-geojson.json' 
##   using driver `GeoJSON'
## Simple feature collection with 290 features and 15 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 10.96139 ymin: 55.33625 xmax: 24.1724 ymax: 69.05904
## Geodetic CRS:  WGS 84
clean_municipality_name <- function(name) {
  name <- gsub("Ã¥", "a", name)
  name <- gsub("ä", "a", name)
  name <- gsub("ö", "o", name)
  name <- gsub("Ö", "O", name)
  name <- gsub("Ä", "A", name)
  name <- gsub("Ã…", "A", name)
  return(name)}
geojson_data <- geojson_data %>%
  dplyr::mutate(municipality = clean_municipality_name(name_2)) 
swemun <- read.csv("qog_swemun_edit_ts_16.csv") %>% 
  dplyr::filter(year == 2014)
facet_swemun <- read.csv("qog_swemun_edit_ts_16.csv") %>%
  dplyr::filter(year %in% c(2001, 2007, 2014))

facet_spec <- facet_swemun %>% 
  dplyr::select(scb_forborn2, year, municipality) 

sweden_years <- geojson_data %>% 
  left_join(facet_spec, by = c("municipality" = "municipality")) 
## Warning in sf_column %in% names(g): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 1 of `x` matches multiple rows in `y`.
## ℹ Row 178 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
swemun_spec <- swemun %>% 
  dplyr::select(scb_forborn2, year, municipality)
sweden <- geojson_data %>% 
  left_join(swemun_spec, by = c("municipality" = "municipality"))
facet_map <- ggplot(sweden_years, aes(fill = scb_forborn2)) + 
  geom_sf(show.legend = T, color = "black", size = 0.1, stroke = 0.2) + 
  scale_fill_distiller(name = "(%) Foreign Born",
                       palette = "BuPu", direction = +1) + 
  theme_map() +
  theme(legend.position = "right") + 
  labs(title = "Sweden's Municipality Populations Born Outside of Sweden (2001 - 2014)",
       caption = "Visualization By: Jackson Pearlman (2023);
       
       Data from: Dahlström, Carl & Maria Tyrberg (2016).
       Politics, Institutions and Services in Swedish
       Municipalities, 1980-2015, version 01 April 2016.
       University of Gothenburg: The Quality of Government Institute,
       http://www.qog.pol.gu.se;
       
       Municipality Polygons from: Hijmans, Robert J.,
       University of California, Berkeley, Museum of Vertebrate Zoology.
       Second-level Administrative Divisions, Sweden, 2015.
       Retrieved from https://geodata.lib.utexas.edu/catalog/stanford-vj304zk6062" , 
       subtitle = "General Gradual Increase in Foreign Born Population Across Municipalities"
       ) +
  facet_wrap(~year)
facet_map

Multiple choropleth maps of Sweden's municipalities in the years 2001, 2007, and 2014. The shaded regions correspond to the percentage of the municipality population that were born outside of Sweden. As the years increase, the overall percentage of foreign born population increases, with more and more areas in the <5% to the 10% and up values.

ggsave("pearlman_revised_map.jpeg", facet_map, height = 8, width = 11, dpi = 1080)