Home > Enterprise >  Adding continuous/gradient fill to map
Adding continuous/gradient fill to map

Time:02-20

After lurking around for some time, this is my first question here - I'm an absolute novice so please forgive any lack of knowledge about this forum's customs and technical details of R.

I want to plot a map of Germany, in which the fill of the federal states reflects the inhabitants' preferences, like I have done here: Excel example

My data looks as follows:

Bundesland Sausage
Baden-Württemberg 28,7%
Bayern 31,7%
Berlin 20%
Brandenburg 20,8%
Bremen 19,2%
Hamburg 20,6%
Hessen 24,4%
Mecklenburg-Vorpommern 24,8%
Niedersachsen 26,5%
Nordrhein-Westfalen 26,6%
Rheinland-Pfalz 28,9%
Saarland 27,3%
Sachsen 22,5%
Sachsen-Anhalt 25%
Schleswig-Holstein 26,6%
Thüringen 24,1%

I want to plot the map like this:

#loading libraries
    library(ggplot2)
    library(raster)

#retrieving map
germany <- getData(country = "Germany", level = 1)

#drawing map as plot
ggplot()  
geom_polygon(data = germany,
             aes(x = long, y = lat, group = group))

Which provides me with this this plot.

My question now is: how can I integrate the data from the table above as a gradient fill for the different federal states?

And: is it possible to show the percentages in the plot in the respective federal state instead of having a gradient legend next to the plot?

Thanks a lot for any advice, it is much appreciated!

CodePudding user response:

This should provide a starter, using simple features (package sf) and joining sausage data to the Bundesland data.
I've created the Sausages data, assuming the percentages are numeric values, if your data is as set out in the table (i.e. as "12,5%" strings) there will be a bit of work to convert these (generally it is much better to add data to a question in a reproducible manner ie using the output of dput(your_data) or as a data frame, i.e. dat <- data.frame(your_variables...) ).
Apologies for using the point decimal notation.

library(ggplot2)
library(raster)
library(dplyr)
library(sf)

dat <- structure(list(Bundesland = c("Baden-Württemberg", "Bayern", 
                                     "Berlin", "Brandenburg", "Bremen", "Hamburg", "Hessen", "Mecklenburg-Vorpommern", 
                                     "Niedersachsen", "Nordrhein-Westfalen", "Rheinland-Pfalz", "Saarland", 
                                     "Sachsen", "Sachsen-Anhalt", "Schleswig-Holstein", "Thüringen"
), Sausage = c(0.287, 0.317, 0.2, 0.208, 0.192, 0.206, 0.244, 
               0.248, 0.265, 0.266, 0.289, 0.273, 0.225, 0.25, 0.266, 0.241)), class = "data.frame", row.names = c(NA, 
                                                                                                                   -16L))


germany <- 
  getData(country = "Germany", level = 1) %>% 
  st_as_sf() %>% 
  left_join(dat, by = c("NAME_1" = "Bundesland"))

ggplot(germany)  
  geom_sf(aes(fill = Sausage)) 
  geom_sf_text(aes(label = scales::percent(Sausage))) 
  scale_fill_gradient(low = "yellow", high = "red") 
  theme(legend.position = "none")

Created on 2022-02-20 by the enter image description here

  • Related