ggplot choropleth map merging trouble

Hello all, im quite new to all this Rstudio. but im trying to make a choropleth map of Denmark. ive got the map to show and work from a .rds file from gadm.org. but when i have to fill each polygon with other data (csv), i fall short and have no idea how to continue.

library(sp)

DK <- readRDS("dk2.rds")

library(tibble)
library(dplyr)
library(ggplot2)

##here the map is showing and everything is fine

ggplot(data = DK, aes(x=long, y=lat, group=group)) +
  geom_polygon(fill="white", color="blue") +
  coord_map()

##now trying to add the csv file

DKtal <- read.csv("DKtal2.csv")

##merging the two files

final_map <- merge(DK, DKtal, by = "NAME_2")

##plotting the new map with the fill set at "tal" which should assign a value to some of the regions in denmark. but nothing happens

ggplot(data = final_map, aes(x=long, y=lat, group=group)) +
  geom_polygon(color="white", fill=tal) +
  coord_map()

##all it gives me is this message 
##Regions defined for each Polygons
##Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomPolygon,  : 
##  object 'tal' not found

So all in all I'm having problems with merging the two datasets correct i think.
any body who has an idea on what to do here?

When you work with data from gadm.org, you are downloading rds file that contains sf or sp object.

I think you need to convert also the data you read from csv to the corresponding format (sf or sp) and work with sf :package: to merge the data.

Also, Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

1 Like

I'd strongly recommend that you look into the sf package if you are going to be working with spatial data in R. It is quite a bit easier to work with than the previous standard spatial package, sp. In fact there was recently a great series of blogposts about making maps with sf and ggplot2: Drawing beautiful maps programmatically with R, sf and ggplot2 — Part 1: Basics

As to your specific question about merging non-spatial data with spatial data, the sf package allows you to use (nearly) all the dplyr functions on spatial objects as you would on any R data frame. So, you can use left_join() to merge data from a non-spatial data frame (like from your CSV) with an sf object, given that they have a column in common to join on.

Below, I grab the Denmark spatial data that you reference and then create a subset and remove the the spatial data from it to create a standard data frame (similar to what you might have in your CSV). Then I join that data frame with the sf object and plot it.

library(tidyverse)
library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.1.3, PROJ 4.9.2

# read sf file
dk <- read_rds(gzcon(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_DNK_2_sf.rds")))

# make sf subet
dk_a <- select(dk, 1:3, 7)

# make non spatial subset
dk_b <- dk %>% 
  select(4:7) %>% 
  st_set_geometry(NULL) %>% 
  mutate(my_rate = runif(99))  # make up some data to plot

# join spatial and non spatial
dk_merged <- left_join(dk_a, dk_b, by = "NAME_2")

# plot
ggplot(data = dk_merged) +
  geom_sf(aes(fill = my_rate))

Created on 2018-11-18 by the reprex package (v0.2.1)

5 Likes

You are a miracle worker, thanks.

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.