Cyrus Mohammadian
Cyrus Mohammadian

Reputation: 5193

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:

  library(cshapes)
  library(ggplot2)
  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))
     +coord_equal()

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.

Upvotes: 1

Views: 511

Answers (2)

Cyrus Mohammadian
Cyrus Mohammadian

Reputation: 5193

Ok so I figured out the problem. When I was inspecting the data frame created by fortify() and then remerged with the original data, I noticed that NA's were produced in the merge. Not sure why. So I decided to use the ?help function for fortify() to see if I was missing an argument and lo and behold it says "Rather than using this function, I now recomend using the broom package, which implements a much wider range of methods. fortify may be deprecated in the future." -I had never seen this before and likely explains why I never had trouble in the past. So I checked out library(broom) and the equivalent function is tidy(), which works just fine, like so:

library(broom)
library(cshapes)
library(ggplot2)
library(dplyr)
world <- cshp(date=as.Date("2009-1-1"))
world@data$id <- rownames(world@data)
world.df = tidy(world)
world.df$arrange<-1:192609 ###Needs be reordered (something fortify did automatically)###
world.df <- join(world.df, world@data, by="id")
world.df<-arrange(world.df, arrange)
ggplot() + geom_polygon(data=world.df, 
  aes(x = long, y = lat, group = group,fill = AREA))
  +coord_equal()

Which produces the following:

enter image description here

Upvotes: 3

erc
erc

Reputation: 10131

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

Upvotes: 3

Related Questions