Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly join data and geometry using ggmap

Tags:

r

ggmap

An image is worth a thousand words: Data does not match geometry

Observed behaviour: As can be seen from the image above, countries' names do not match with their actual geometries.

Expected behaviour: I would like to properly join a data frame with its geometry and display the result in ggmap.

I've previously joined different data frames, but things get wrong by the fact that apparently ggmap needs to "fortify" (actually I don't know what really means) data frame in order to display results.

This is what I've done so far:

library(rgdal)
library(dplyr)
library(broom)
library(ggmap)

# Load GeoJSON file with countries.
countries = readOGR(dsn = "https://gist.githubusercontent.com/ccamara/fc26d8bb7e777488b446fbaad1e6ea63/raw/a6f69b6c3b4a75b02858e966b9d36c85982cbd32/countries.geojson")

# Load dataframe.
df = read.csv("https://gist.githubusercontent.com/ccamara/fc26d8bb7e777488b446fbaad1e6ea63/raw/a6f69b6c3b4a75b02858e966b9d36c85982cbd32/sample-dataframe.csv")

# Join geometry with dataframe.
countries$iso_a2 = as.factor(countries$iso_a2)
countries@data = left_join(countries@data, df, by = c('iso_a2' = 'country_code'))

# Convert to dataframe so it can be used by ggmap.
countries.t = tidy(countries)

# Here's where the problem starts, as by doing so, data has been lost!

# Recover attributes' table that was destroyed after using broom::tidy.
countries@data$id = rownames(countries@data) # Adding a new id variable.
countries.t = left_join(countries.t, countries@data, by = "id")

ggplot(data = countries.t,
       aes(long, lat, fill = country_name, group = group)) +
  geom_polygon() +
  geom_path(colour="black", lwd=0.05) + # polygon borders
  coord_equal() +
  ggtitle("Data and geometry have been messed!") +
  theme(axis.text = element_blank(), # change the theme options
        axis.title = element_blank(), # remove axis titles
        axis.ticks = element_blank()) # remove axis ticks
like image 318
ccamara Avatar asked Sep 18 '17 10:09

ccamara


2 Answers

While your work is a reasonable approach - I would like to rethink your design, mainly because of two simple reasons:

1) while GeoJSON is the future, R still heavily relies on the sp package and its correspondent sp* objects - very soon you wish you had switched early on. It`s just about the packages and most of them (if not all) rely on sp* objects.

2) ggplot has great plotting capabilities combined with ggmap - but its still quite limited compared to sp* in combination with leaflet for R etc.

probably the fastest way to go is simple as:

library(sp)
library(dplyr)
library(geojsonio)
library(dplyr)
library(tmap)

#get sp* object instead of geojson
countries <- geojsonio::geojson_read("foo.geojson",what = "sp")

#match sp* object with your data.frame
countries@data <- dplyr::left_join(countries@data, your_df, by = 
c("identifier_1" = "identifier_2"))

#creates a fast and nice looking plot / lots of configuration available
p1 <- tm_shape(countries) +
      tm_polygons() 
p1

#optional interactive leaflet plot
tmap_leaflet(p1)

It is written out of my head / bear with me if there are minor issues.

It is a different approach but its at least in my eyes a faster and more concise approach in R right now (hopefully geojson will receive more support in the future).

like image 114
Christian Avatar answered Nov 12 '22 03:11

Christian


There is a reason for the messed up behaviour.

countries starts out as a large SpatialPolygonsDataFrame with 177 elements (and correspondingly 177 rows in countries@data). When you perform left_join on countries@data and df, the number of elements in countries isn't affected, but the number of rows in countries@data grows to 210.

Fortifying countries using broom::tidy converts countries, with its 177 elements, into a data frame with id running from 0 to 176. (I'm not sure why it's zero-indexed, but I usually prefer to specify the regions explicitly anyway).

Adding id to countries@data based on rownames(countries@data), on the other hand, results in id values running from 1 to 210, since that's the number of rows in countries@data after the earlier join with df. Consequently, everything is out of sync.

Try the following instead:

# (we start out right after loading countries & df)

# no need to join geometry with df first

# convert countries to data frame, specifying the regions explicitly
# (note I'm using the name column rather than the iso_a2 column from countries@data;
# this is because there are some repeat -99 values in iso_a2, and we want
# one-to-one matching.)
countries.t = tidy(countries, region = "name")

# join with the original file's data
countries.t = left_join(countries.t, countries@data, by = c("id" = "name"))

# join with df
countries.t = left_join(countries.t, df, by = c("iso_a2" = "country_code"))

# no change to the plot's code, except for ggtitle
ggplot(data = countries.t,
       aes(long, lat, fill = country_name, group = group)) +
  geom_polygon() +
  geom_path(colour="black", lwd = 0.05) +
  coord_equal() +
  ggtitle("Data and geometry are fine") +
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank())

enter image description here

p.s. You don't actually need the ggmap package for this. Just the ggplot2 package that it loads.

like image 25
Z.Lin Avatar answered Nov 12 '22 04:11

Z.Lin