[R-sig-Geo] FW: ggmap, hexbin and length distortion in Lat/long
Mike Bock
mbock at environcorp.com
Fri May 24 17:21:16 CEST 2013
It was suggested that r-sig-geo may be the appropriate venue for this question so I am reposting here, thanks
-----Original Message-----
From: Mike Bock
Sent: Friday, May 24, 2013 9:52 AM
To: 'r-help at r-project.org'
Subject: ggmap, hexbin and length distortion in Lat/long
I am working with spatial data in ggmap, generally with great success. I have a huge data set with the coordinates in NAD 83 UTM Zone 11 (meters). To map the data the coordinates were converted to Lat Long in GIS prior to use in R and ggmap/ggplot. I am using hexagonal binning to aggregate the data :
#create bins and calculate stats
hb<-hexbin(DF$lon,DF$lat,xbins=80,IDs=TRUE)
hb.avg<-hexTapply(hb,DF$Res,mean,na.rm=TRUE)
hb.mx<-hexTapply(hb,DF$Res,max,na.rm=TRUE)
hb.p80<-hexTapply(hb,DF$Res,quantile,prob=0.80,na.rm=TRUE)
#create df for ggplot
hx_dat <- data.frame(hcell2xy(hb), count = hb at count,
xo = hb at xcm, yo = hb at ycm, Mean=hb.avg,Max=hb.mx,
p80=hb.p80)
#Base Map
#BBox is the bounding box
Base<-get_map(BBox,source='google')
m_hx<-ggmap(Base,legend = "bottom", base_layer=ggplot(aes(x=x,y=y),data=hx_dat))
#Map of means
a<-0.55
hc<-'grey60'
m_hx+geom_hex(aes(x = x, y = y, fill = Mean),
color = hc, ,alpha=a,stat = "identity") +
scale_fill_gradientn("Mean",colours=rev(rainbow(4)),trans='sqrt')
...and so on for other stats
I can also run statistical analyses on hx_dat.
By creating hexbins based on lat/long it seems there will be distortion due to the differences in length of a degree at different locations on the earth's surface. What is the most efficient way to eliminate this distortion? Should I run hexbin in NAD83 and convert the x/y coordinates to Lat Long? Can I get ggmap to convert the baselayer to NAD84 and just do everything in NAD(my preferred option)?
I have tried converting Lat Long to NAD84 and back but the coordinates are coming up in the eastern Pacific and not in California, so I am missing something and I am not sure that is the best way to solve the problem anyway. Thanks in advance, any help is greatly appreciated Mike
Michael J. Bock, PhD | Senior Manager
mbock at environcorp.com
________________________________
This message contains information that may be confidenti...{{dropped:8}}
More information about the R-sig-Geo
mailing list