Previously working ggplot2 script now returning fatal error at fortify

642 views Asked by At

I have previously written a script to create a colored map of the US, with each state colored based on some simulated data. The idea is to later be able to replace the simulated data with some measure. It was written to be self-contained and originally ran just fine, but now crashes when the fortify {ggplot2} command is run.

I believe this is due to a problem with the fortify command, as it returns a fatal error and restarts R at that point. Here is the code up to the point of the fatal error:

###Load libraries
library(maptools)
library(ggplot2)
library(ggmap)
library(rgdal)
library(dplyr)

#Set working directory to where you want your files to exist (or where they already exist)
#Download, read and translate coord data for shape file of US States
if(!file.exists('tl_2014_us_state.shp')){
        download.file('ftp://ftp2.census.gov/geo/tiger/TIGER2014/STATE/tl_2014_us_state.zip',
                      'tl_2014_us_state.zip')
        files <- unzip('tl_2014_us_state.zip')
        tract <- readOGR(".","tl_2014_us_state") %>% spTransform(CRS("+proj=longlat +datum=WGS84"))
} else {
        tract <- readShapeSpatial("./tl_2014_us_state.shp") #%>% spTransform(CRS("+proj=longlat +datum=WGS84"))
}
# shape<-readShapeSpatial("./fao/World_Fao_Zones.shp") 

#Download reference data for state names and abbreviations - a matter of convenience if there are 
#states for which you have no data
if(!file.exists('states.csv')){
        download.file('http://www.fonz.net/blog/wp-content/uploads/2008/04/states.csv',
                      'states.csv')
        states <- read.csv('states.csv')
} else {
        states <- read.csv('states.csv')
}

#simulated data for plotting values of some 'characteristic'
mydata           <- data.frame(rnorm(51, 0, 1)) #51 "states" in the state dataset
names(mydata)[1] <- 'value' #give the simulated column of data a name

#Turn geo data into R dataframe
tract_geom<-fortify(tract,region="STUSPS") #STUSPS is the state abbreviation which will act as a key for merge

The script stops working at the line above and crashes R with a fatal error. I have tried a workaround described in another post, in which you place an explicit "id" column in the spatial dataframe, which fortify then uses as the key by default. With this modification the lines:

tract@data$id <- tract@data$STUSPS
tract_geom    <- fortify(tract)

would replace tract_geom<-fortify(tract,region="STUSPS") in the previous code, where STUSPS is the key for a later data merge.

Unfortunately, when I then fortify the tract data, the id column is not the state abbreviation as expected, but is instead a vector of characters between "0" and "55" (56 unique values). It appears that the state abbreviations (of which there are 56) are somehow being transformed into numbers and then into characters.

I am working on figuring out why this is happening and looking for a fix. If the fortify function worked with the region argument, that would be ideal, but if I can get the workaround to work, that would be great too. Any help would be greatly appreciated. I have looked at the documentation and at solutions to various similar problems and have come up short (even tried ArcGIS).

2

There are 2 answers

0
Patrick Williams On BEST ANSWER

I was able to solve my own question by running update.packages(). I'm not entirely sure which package was the culprit, but it could have been maptools, rgdal, or sp, as these were among the packages to be updated that may have influenced the problem.

In the end, after updates, the script runs in its original form with the line tract_geom<-fortify(tract,region="STUSPS") intact. Thank you to those who helped me work through this problem.

4
Serban Tanasa On

Try:

readOGR(..., stringsAsFactors=FALSE, ...)