[R] problems with cloropleth map of Japan

Guadalupe Bastos guadalupebastos at gmail.com
Tue Apr 29 03:07:12 CEST 2014


Hi:
I have recently started working with creating maps in R. I have been able
to create cloropleth maps for Europe without a problem.
However, when I try doing the same for Japan, which shapefile is downloaded
from the website http://gadm.org/country , R stops working. The message
that pops up is "Error: R for windows GUI front-end has stopped working". I
can see the map was created at the back of the screen, but I'm not able to
save it.
I don't know if this is a bug or if there's something I'm doing wrong. The
idea of this code is that Japan only has a value for all the country of my
variable (the value is 2.15). And what I want is for Japan to be shaded in
the corresponding color. So it will be only one color for the whole country.
Here is the code. You'll need to download the shapefile from
http://gadm.org/country and un-zip in the directory you are working.

I hope you can help me. Thanks in advance!

rm(list=ls(all=TRUE))
#--------------------------------------------------------------
#USER INSTRUCTIONS:
#First time user: install packages
#install.packages(c("sp", "maptools", "rgdal"), dependencies=TRUE)  #It's
very important that this package is installed this way
  #Install this package if it's the first time you run this
#install.packages("ggplot2")
#install.packages("ggmap")
#install.packages("rgeos")
#install.packages("foreign")

#----------------------------------------------------------------------------
# Libraries we use:
library(maptools)
library(ggplot2)
library(ggmap)
library(rgeos)
library(foreign)


#-------------------------------------------------------------------------------
# JAPAN
#-------------------------------------------------------------------------------
#Download shapefile from
#http://gadm.org/country

# read administrative boundaries (change folder appropriately)
japMap <- readShapePoly(fn="JPN_adm/JPN_adm0.shp")

summary(japMap)
# data
japEdu <- data.frame(1,2); names(japEdu) <- c('GEO','Value')
japEdu$GEO <- 114
japEdu$Value <- 2.15

japMapDf=fortify(japMap, region='ID_0')

# merge map and data
japEduMapDf <- merge(japMapDf, japEdu, by.x="id", by.y="GEO")
japEduMapDf <- japEduMapDf[order(japEduMapDf$order),]

# ggplot mapping
# data layer
m0 <- ggplot(data=japEduMapDf)

# empty map (only borders)
m1 <- m0 + geom_path(aes(x=long, y=lat, group=group), color='gray') +
coord_equal()

# fill with education expenditure data
m2 <- m1 + geom_polygon(aes(x=long, y=lat, group=group, fill=Value))
m2


More information about the R-help mailing list