Cyrus Mohammadian Cyrus Mohammadian - 4 months ago 24
R Question

How to use cshapes and ggplot2 to make a choropleth map in R?

I'm having trouble doing something very basic. I've done this hundreds of times with no problem with other maps but I can't get a cshapes shapefile to map properly using ggplot2 (as an example I'm trying to map "AREA" as the fill, which is a variable that comes with the cshapes shapefile). Here is the code I'm using:

world <- cshp(date=as.Date("2009-1-1"))
world@data$id <- rownames(world@data)
world.df = fortify(world, region="COWCODE")
world.df <- join(world.df, world@data, by="id")
ggplot() + geom_polygon(data=world.df,
aes(x = long, y = lat, group = group,fill = AREA))

What I end up with is the following:enter image description here, which as you can see is missing data for the eastern hemisphere. Not sure what's going on, any assistance is much appreciated.


The id you created did not match the id in world.df, thus NAs were introduced with joining by id.

If you set region to and join by SP_ID it works:

world <- cshp(date=as.Date("2009-1-1"))

world.df = fortify(world, region="SP_ID")
names(world.df)[6] <- "SP_ID"

world.df <- join(world.df, world@data)

enter image description here