Question

I'm trying to plot a polygon shapefile with ggplot2 and I'm getting some odd results. I use the following code to read in the shapefile with a single polygon:

zctaSp<-readShapePoly("zctaSp.shp")

Then I plot using a couple of different approaches two of which work, but the one I need does not.

PLOT 1: The shape looks correct with this:

plot(zctaSp)

plot1

PLOT 2: The shape also looks correct with this which seems nearly identical to plot 2:

ggplot(data=zctaSp, aes(x=long, y=lat, group=group)) + geom_polygon()

plot2

PLOT 3: But the shape is mangled using this:

ggplot(data=zctaSp, aes(x=long, y=lat, group=group)) + geom_polygon()    
atl <- qmap('atlanta', zoom=11, color="bw")
atl + geom_polygon(data=zctaSp, aes(x=long, y=lat, group=group), alpha=1)

plot3

I put the shapefile at http://bit.ly/1nnlAg3.

Note that I did also try doing the plotting after running the fortify command along the lines of Hadley Wickham's instructions at this link and this did not improve things.

Était-ce utile?

La solution

try:

library(ggmap)
library(rgdal)
# Data using NAD83 - epsg: 4269
zct <- readOGR(dsn = 'D:/Programacao/R/Stackoverflow/22387624',
               layer = 'zctaSp')
zctdf <- fortify(zct)

# Project to wgs84
wgs84proj <- CRS('+init=epsg:4326')
zct_g <- spTransform(zct, wgs84proj)
zctgdf <- fortify(zct_g)
map_loc <- get_map(location = c(lon = mean(zctgdf$lon), mean(zctgdf$lat)),
                   source = 'google', zoom = 11)
map <- ggmap(map_loc, extent = 'device')
map + 
  geom_polygon(data=zctgdf, aes(x=long, y=lat, group=group), alpha=.8)

ggmap

A map from Qgis

enter image description here

Regarding the projections used in this exercise,the NAD 83 / WGS84 may be a pitfall. There are slightly differences between NAD83 and WGS84. NAD83 rely on GRS80 datum, which have a realization quite similar to WGS84 but not the same.

For these ggmap's one should always use unprojected WGS84 (epsg4326).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top