Choropleth mapping using ggplot2

Hello all, I am busy with my thesis on urban economic geography. I am trying to map an attribute, namely ECI (economic complexity index), for 234 South African municipality districts. I have the shape file for the map and a data frame consisting of 2 columns: 1. Code for each municipality (matching to the defining code in the shape file) and 2. eci for each municipality. I created an ID for the shape file before fortifying it and merged it with the eci.df, and plotted it. However, it does seem to work properly, I feel like there is something incorrect with my eci.df.

Here is the code, would really appreciate any assistance.

ECI data frame

eci_df <- data.frame(Income_ECI_Data$CAT_B, Income_ECI_Data$ECI)

names(eci_df)[1] <- 'Code'
names(eci_df)[2] <- 'eci'

read municipality polygons

setwd("/Users/michellegeorgiou/Desktop/MuniMap")
muni <- readOGR(dsn=".", layer="Local_Municipalities_2016")

fortify and merge: muni.df is used in ggplot

muni@data$id <- rownames(muni@data)
muni.df <- fortify(muni)
muni.df <- left_join(muni.df, muni@data, by="id")
muni.df <- merge(muni.df, eci_df, by.x="CAT_B", by.y="Code", all.x=T, all.y=T)

create the map layers

ggp <- ggplot(data=muni.df, aes(x=long, y=lat, group=group)) +
geom_polygon(aes(fill=eci)) + # draw polygons
geom_path(color="grey") + # draw boundaries
coord_equal() +
scale_fill_gradient(low = "#ffffcc", high = "#ff4444", space = "Lab", na.value = "grey50", guide = "colourbar")

render the map

print(ggp)

The output isn't correct. If I comment out the geom_path, the country is distorted by predominantly filled in with sections whited out but with it as part of the code, it shapes the map correctly but doesn't fill it in right (as shown in the attached figure)

Can anyone help?
Thank you in advance :slight_smile:

This is a common problem in thematic mapping with older tools. It's easy to get geocoded arguments reversed.

The sf package provides a superior alternative and it's geometry column in a pre-fortified data frame makes it simple to join data values by geolocation.

My post on coloring thematic maps illustrates the use of sf for the purpose.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.