Error with function strengthen ggplot2

I get this error using the fortify method in ggplot2:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'proj4string' for signature '"NULL"'

The code is as follows:

 > library(maptools) > gpclibPermit() > library(ggplot2) > library(rgdal) > library(rgeos) > library(ggmap) > brMap <- readShapePoly("Google/BRASIL.shp") > brMapDF <- fortify(brMap) # This actually works # But this donยดt > brMapDF <- fortify(brMap, region="UF") Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'proj4string' for signature '"NULL"' 

This happens with all the shapefiles that I have, so I tried (in the code above) with the shapefile that I found in stackoverflow Formatted the ggplot2 map , data https://docs.google.com/file/d/0B_coFit6AovfcEFkbHBjZEJaQ1E/ edit

+4
source share
1 answer

This is a bit of a workaround, but if you duplicate the UF column as the identifier column, as shown in the wiki example from my comments when preparing the data, the default values โ€‹โ€‹for fortify will use the first column in the spatial data frame to separate accordingly, adding names under the id column .

 library(maptools) library(ggplot2) library(sp) library(rgdal) library(rgeos) brMap <- readShapePoly("Google/BRASIL", IDvar = "UF", proj4string = CRS("+init=epsg:4236"), repair = TRUE, verbose = TRUE) brMap@data $id <- brMap@data $UF brMapDF <- fortify(brMap) 

The resulting structure for brMapDF:

 'data.frame': 9316 obs. of 7 variables: $ long : num -68.6 -68.7 -68.8 -68.8 -68.9 ... $ lat : num -11.1 -11.2 -11.2 -11.1 -11.1 ... $ order: int 1 2 3 4 5 6 7 8 9 10 ... $ hole : logi FALSE FALSE FALSE FALSE FALSE FALSE ... $ piece: Factor w/ 37 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ... $ group: Factor w/ 81 levels "AC.1","AL.1",..: 1 1 1 1 1 1 1 1 1 1 ... $ id : chr "AC" "AC" "AC" "AC" ... 
+1
source

Source: https://habr.com/ru/post/1487572/


All Articles