tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.52k stars 2.03k forks source link

fortify causes fatal error #1302

Closed cmohamma closed 9 years ago

cmohamma commented 9 years ago

I've been using the fortify() command along with the region= option to render shapefile into a mappable object with no problem for sometime but all of a sudden the same script that was running without complications last week crashes my computer now. I've been able to isolate the problem. If i remove the region=" " command it works but the id that is used does not match up with the id column in the data making it impossible to merge data to the fortify object. I updated R and all my packages but that did not help. Any advice would be much appreciated!

hadley commented 9 years ago

Unfortunately there's not much I can do without a reproducible example

cmohamma commented 9 years ago

My apologies! I used a shapefile of lebanon's admin.2 districts, but the problem arises with virtually any shapefile I use (see gadm.org/download select lebanon for my specific case but any shp will do)

library(ggplot2); library(rgdal)
map <- readOGR("path", verbose=FALSE)
map.f <- fortify(map, region="ID")
map.plot <- merge(map.f, map@data, by="ID")

Those commands give me a fatal error that crashes my R session. I found a work around doing this:

map@data$id <- rownames(map@data)
map@data<-merge(map, map@data, by="ID")
map.f<-fortify(map)

I'm not sure why the original script doesn't work though... Thanks so much for all your help as usual!

hadley commented 9 years ago

Can you please make an example that I can copy and paste into an R session?

FrankRuns commented 9 years ago

I'm also having this issue. I've updated all the packages I'm using (ggplot2, maptools, rgdal). I'm using the latest version of r studio - Version 0.98.507 (also running R version 3.2.2 (2015-08-14)). I'm running Mac OSX Version 10.9.5. The source of the shapefile is US Census. From there I select county subdivisions and then the state of Florida to download the files. In r-studio I navigate to that directory and then...

library(ggplot2); library(rgdal)

# Read the shapefile with maptools
area <- readShapePoly("tl_2012_12_cousub")

# Check that I'm using the right name -- I'll use "NAME"
names(area@data)
 [1] "STATEFP"  "COUNTYFP" "COUSUBFP" "COUSUBNS" "GEOID"    "NAME"     "NAMELSAD"
 [8] "LSAD"     "CLASSFP"  "MTFCC"    "CNECTAFP" "NECTAFP"  "NCTADVFP" "FUNCSTAT"
[15] "ALAND"    "AWATER"   "INTPTLAT" "INTPTLON"

# Just for reference... using ggplot's fortify function without specifying region it's ok
area.f <- fortify(area)

# However, when I specify region I get the crash (which I show below)
area.f <- fortify(area, region = "NAME")

And then this screen occurs every time... regardless of shapefile I'm using

screen shot 2015-09-27 at 4 09 04 pm

What other information can I provide? How can I get past this?

technocrat commented 9 years ago

I'm unsure what is causing this problem, but you can get a Florida county map by

library(rgdal)
library(dplyr)
library(ggplot2)
dsn <- "data/cb_2014_us_county_5m/" # data/ or your path
layer <- "cb_2014_us_county_5m"
county <- readOGR(dsn, layer)
map <- fortify(county, region = AFFGEOID)
florida <- c("0500000US12001", "0500000US12003", "0500000US12005", "0500000US12007", "0500000US12009", "0500000US12011", "0500000US12013", "0500000US12015", "0500000US12017", "0500000US12019", "0500000US12021", "0500000US12023", "0500000US12027", "0500000US12029", "0500000US12031", "0500000US12033", "0500000US12035", "0500000US12037", "0500000US12039", "0500000US12041", "0500000US12043", "0500000US12045", "0500000US12047", "0500000US12049", "0500000US12051", "0500000US12053", "0500000US12055", "0500000US12057", "0500000US12059", "0500000US12061", "0500000US12063", "0500000US12065", "0500000US12067", "0500000US12069", "0500000US12071", "0500000US12073", "0500000US12075", "0500000US12077", "0500000US12079", "0500000US12081", "0500000US12083", "0500000US12085", "0500000US12086", "0500000US12087", "0500000US12089", "0500000US12091", "0500000US12093", "0500000US12095", "0500000US12097", "0500000US12099", "0500000US12101", "0500000US12103", "0500000US12105", "0500000US12107", "0500000US12109", "0500000US12111", "0500000US12113", "0500000US12115", "0500000US12117", "0500000US12119", "0500000US12121", "0500000US12123", "0500000US12125", "0500000US12127", "0500000US12129", "0500000US12131", "0500000US12133")
flmap <- filter(map, id %in% florida)
b = ggplot(data = flmap)
b + geom_map(map=flmap, aes(x=long, y=lat, map_id=id, group=group),fill="white", color="black", size=0.3) 

in the R console (I haven't gotten this to work in RStudio)

> sessionInfo() 
R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggmap_2.5.2         VennDiagram_1.6.16  futile.logger_1.4.1 knitr_1.11         
 [5] rmarkdown_0.8       maps_2.3-11         maptools_0.8-36     SciencesPo_1.3.7   
 [9] reshape2_1.4.1      ggplot2_1.0.1       pander_0.5.2        xtable_1.7-4       
[13] RMySQL_0.10.6       DBI_0.3.1           dplyr_0.4.3         acs_1.2            
[17] rgdal_1.0-7         sp_1.2-0           

loaded via a namespace (and not attached):
 [1] lattice_0.20-33      colorspace_1.2-6     htmltools_0.2.6      yaml_2.1.13         
 [5] chron_2.3-47         XML_3.98-1.3         foreign_0.8-66       lambda.r_1.1.7      
 [9] jpeg_0.1-8           plyr_1.8.3           stringr_1.0.0        rgeos_0.3-12        
[13] munsell_0.4.2        gtable_0.1.2         RgoogleMaps_1.2.0.7  mapproj_1.2-4       
[17] evaluate_0.7.2       labeling_0.3         parallel_3.2.1       proto_0.3-10        
[21] Rcpp_0.12.0          geosphere_1.4-3      scales_0.3.0         formatR_1.2         
[25] rjson_0.2.15         png_0.1-7            digest_0.6.8         stringi_0.5-5       
[29] RJSONIO_1.3-0        tools_3.2.1          magrittr_1.5         lazyeval_0.1.10     
[33] RSQLite_1.0.0        futile.options_1.0.0 MASS_7.3-44          data.table_1.9.4    
[37] assertthat_0.1       R6_2.1.1            

(Unless you need higher resolution, I much recommend the Census cartographic boundary files at 5M used here.)

florida

FrankRuns commented 9 years ago

Sorry for wasting anyone's time... I think this is a memory issue for me. When I take a smaller piece of the data, this code works fine on my machine Using the same source and files as I mention above, if I remove rows 81, 86, and 96 ... fortify() works fine.

library(ggplot2); library(rgdal); library(ggmap)

area <- readOGR(dsn = "Downloads/tl_2012_12_cousub", layer = "tl_2012_12_cousub")

# Removing those rows...
names <- levels(area@data$NAME)
area <- area[area$NAME %in% names[-c(81, 86, 96)],]
area.f <- fortify(area, region="NAME")

# And fortify has no problem...
area.f <- fortify(sub.area, region="NAME")

mapage <- get_map(location = c(lon = -83.5, lat = 27.77),
                    color = "color",
                    zoom = 7)

ggmap(mapage) +
      geom_polygon(aes(x = long,
                       y = lat,
                       group = group),
                   data = area.f,
                   color = "black",
                   fill = "red",
                   alpha = 0.5) +
      labs(x = "Longitude",
           y = "Latitude")

Still need to figure out those gaps though.

Thanks for looking at this. @cmohamma perhaps this is also your issue?