[R-sig-Geo] Tearing of polygons using ggmap & readOGR

boB Rudis bob at rudis.net
Mon Feb 1 04:12:07 CET 2016


Clip the state polygons with gIntersection. You cross-posted to
StackOverflow & here so there's a complete answer there as it's more
amenable to source code + commentary + images and will be around for
quite a while despite what folks may opine about mailing lists.

On Sun, Jan 31, 2016 at 8:03 PM, Tyler Frazier
<tyler.j.frazier at icloud.com> wrote:
> Hi Roger,
>
> Thanks for your reply and pointers.  I was looking for a simplified shapefile of American state boundaries and downloaded this one from ESRI, states_basic.
>
> https://www.arcgis.com/home/item.html?id=f7f805eb65eb4ab787a0a3e1116ca7e5 <https://www.arcgis.com/home/item.html?id=f7f805eb65eb4ab787a0a3e1116ca7e5>
>
> The .shp is only about 222 KB which was OK for plotting speed in R as well as its appearance.  I tried
>
> states <- getData('GADM', country='USA', level=1)
>
> as well as the census state subdivisions, but the resolution seemed to be too much for R to handle in a plot.
>
> The states_basic file from ESRI plots just fine with
>
> plot(states)  — I think the summary() command returns what would be expected of a shapefile that was read into R as SPDF.
>
>> summary(states)
> Object of class SpatialPolygonsDataFrame
> Coordinates:
>          min       max
> x -178.21760 -66.96927
> y   18.92179  71.40624
> Is projected: FALSE
> proj4string :
> [+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0]
> Data attributes:
>       STATE_NAME    DRAWSEQ       STATE_FIPS              SUB_REGION   STATE_ABBR
>  Alabama   : 1   Min.   : 1.0   01     : 1   South Atlantic    : 9   AK     : 1
>  Alaska    : 1   1st Qu.:13.5   02     : 1   Mountain          : 8   AL     : 1
>  Arizona   : 1   Median :26.0   04     : 1   West North Central: 7   AR     : 1
>  Arkansas  : 1   Mean   :26.0   05     : 1   New England       : 6   AZ     : 1
>  California: 1   3rd Qu.:38.5   06     : 1   East North Central: 5   CA     : 1
>  Colorado  : 1   Max.   :51.0   08     : 1   Pacific           : 5   CO     : 1
>  (Other)   :45                  (Other):45   (Other)           :11   (Other):45
>> states at data
>              STATE_NAME DRAWSEQ STATE_FIPS         SUB_REGION STATE_ABBR
> 0                Hawaii       1         15            Pacific         HI
> 1            Washington       2         53            Pacific         WA
> 2               Montana       3         30           Mountain         MT
>
> etc…
>
> sounds like I need to look into exactly what is happening as part of the fortify() command in order to achieve some consistency of integrating SpatialPolygonsDataFrames with the ggmap() function.  I may eventually go to tmap or mapview or perhaps leaflet, but have found ggplot:: / ggmap:: to be very useful packages (+geom_polygons, + geom_points etc…)and was hoping to keep working with their grammar of graphics.
>
> Tyler
>
>
>> On Jan 31, 2016, at 3:35 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>>
>> On Sun, 31 Jan 2016, Tyler Frazier wrote:
>>
>>> I'm trying to incorporate a simple shapefile of US states with ggmap and keep getting "tearing" of my polygons. I tried changing group=id to group=group which seemed to partially solve the problem, but not entirely.
>>>
>>> library(rgdal)
>>>
>>> states <- readOGR(dsn="shapefiles", layer="states")
>>>
>>> proj4string(states)
>>>
>>> [1] "+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0"
>>>
>>> states <- spTransform(states, CRS("+proj=longlat +datum=WGS84"))
>>
>> Unnecessary, as you should know from the +towgs84= tag.
>>
>>>
>>> states <- fortify(states)
>>
>> The fortify methods turn data from marine animals with structure into fish soup (a data frame adding attribute values spuriously to each coordinate, I believe). Unless the objects are exactly as fortify wants them, it derails.
>>
>> Do you know that they were polygons? Where did the shapefile come from? Did you plot it? Did you check that it contained what you thought it contained (summary())?
>>
>> Have you considered alternatives (tmap, mapview, ...)?
>>
>>>
>>> sstates <- get_map(location = c(-81, 35, -69, 45), zoom = 4, maptype = "watercolor")
>>>
>>> sstates <- ggmap(sstates)
>>>
>>> sstates <- sstates + geom_polygon(aes(x = long, y = lat, group=group), data = states, color ="white", fill ="orangered4", alpha = .4, size = .2)
>>>
>>> Here is an image of the output.
>>>
>>> http://pasteboard.co/1d7kiRfw.png <http://pasteboard.co/1d7kiRfw.png>
>>>
>>
>> Image posted OK, but posting HTML (unnecessary).
>>
>>>
>>>      [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>> --
>> Roger Bivand
>> Department of Economics, Norwegian School of Economics,
>> Helleveien 30, N-5045 Bergen, Norway.
>> voice: +47 55 95 93 55; fax +47 55 95 91 00
>> e-mail: Roger.Bivand at nhh.no
>> http://orcid.org/0000-0003-2392-6140
>> https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
>> http://depsy.org/person/434412
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list