I'm new to geoplotting and I want to use sf to plot a map of Germany on the state level, with state values colored by a variable in a separate data set. How do I best go about this?
I have loaded the data and the sf object like this:
library(sf)
library(tidyverse)
theme_set(theme_bw())
library(rnaturalearth)
library(rnaturalearthdata)
dat <- read.table(text="
state value
Sachsen 10
Bayern 30
Rheinland-Pfalz 50
Saarland 70
Schleswig-Holstein 90
Niedersachsen 100
Nordrhein-Westfalen 80
Baden-Württemberg 60
Brandenburg 40
Mecklenburg-Vorpommern 20
Bremen 40
Hamburg 60
Hessen 15
Berlin 10
Thüringen 80
Sachsen-Anhalt 20
", header=T)
ger_fedstates <- ne_states(country = "germany")
sp::plot(ne_states(country = "germany"))
How do I now color each state by the value in dat$value? I'm at a loss of how to best combine these two.
As you loaded the
tidyverseandsfyou can just useleft_join(). You can use as well data fromgiscoR, that provides the names of the states in Deustch (giscoRsource is GISCO, the Eurostat agency for spatial data), so the names can match seamlessly. Then again, useggplot2(ortmap,mapsfor baseplot) to create your plot. Key advice here is to usesfpackage instead ofsp, sincespis superseeded bysf:Created on 2023-02-21 with reprex v2.0.2