Misalignment of ggmap and ggplot geom_points?

1,098 views
Skip to first unread message

JBlack

unread,
Mar 8, 2012, 5:18:04 PM3/8/12
to ggplot2
Hi,

Can anyone shed light on what I'm doing wrong here? I would expect the
data points and the google raster map to align better that what I'm
seeing.

# 1st 2 points at fixed latitudes (46, 43),
# 2nd 2 points on known points of land (Cape George, Sable Island)
# while the fixed latitudes line up with the axis labels, the points
of land
# suggest that the Google map is not aligned properly

lon = c(-67.2, -67.2,-61.95640829749075, -60.1334953);
lat = c(46, 43, 45.83594917946984, 43.9634132);
size = c(100,100,100,100);
myData = data.frame(lon,lat,size)

require(ggmap)
lonlatC <- c(lon=mean(myData$lon), lat=mean(myData$lat))
osm <- ggmap(center=lonlatC, source = "google",
zoom=6,maptype="satellite")
ggmapplot(osm, maprange = FALSE, expand = FALSE,
base_layer=ggplot(myData, aes(x=lon, y=lat))) +
geom_point(aes(size=size))

Thanks
Jerry

JBlack

unread,
Mar 8, 2012, 8:04:44 PM3/8/12
to ggp...@googlegroups.com
The attached pdf shows the effect...

Thanks
Jerry
ggmap_point_mismatch.pdf

AJSmit

unread,
Mar 9, 2012, 1:23:36 AM3/9/12
to ggp...@googlegroups.com
Hi,


Paul Hiemstra suggests setting an often to the Google raster to align its position with that of the points. It works as an interim solution, but in the long run I think it will remain unsatisfactory as it does not get to the heart of the problem. Paul suggests the Google Maps bounding box definition is wrong somehow, and I find that specifying an offset indeed corrects it.

I found that setting MINIMUMSIZE = TRUE in the GetMap.bbox() function brings things into better alignment, but it is not perfect. I have not yet had time to play around some more.

AJ
Message has been deleted

David Kahle

unread,
Mar 9, 2012, 10:28:09 AM3/9/12
to AJSmit, ggp...@googlegroups.com
There is actually a bigger problem here which can't be solved by simply changing the bounding box as suggested.

The problem is that at very low zooms (very low scales, big extents), the plotting is unreliable as evidenced by the code below (you will need the dev version for the code to work) :

library(devtools)
dev_mode()
install_github('ggmap', 'dkahle')
library(ggmap)


gc <- geocode('the white house')

qmap('the white house', zoom = 20, maptype = 'satellite') +
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

qmap('the white house', zoom = 18, maptype = 'satellite') +
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

qmap('the white house', zoom = 16, maptype = 'satellite') +
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

qmap('the white house', zoom = 14, maptype = 'satellite') +
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

  

qmap('the white house', zoom = 10) +
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

  

qmap('the white house', zoom = 8) +
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

  

qmap('the white house', zoom = 7) + # first observed effect (very slight)
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')  

qmap('the white house', zoom = 6) + # first real observed effect
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

  

qmap('the white house', zoom = 5) + # effect more pronounced
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

  

qmap('the white house', zoom = 4) + # woe!
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

qmap('the white house', zoom = 3) + # white house in canada?
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

qmap('the white house', zoom = 2) + # white house approaching santa's workshop
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')

qmap('the white house', zoom = 2, source = 'osm') + # osm has effect, too
  geom_point(aes(x = lon, y = lat), data = gc, colour = 'red')


So for the time being be very cautious with zooms <= 7.  I'll work to resolve the issue as soon as possible.

Thanks
david.


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

David Kahle

unread,
Mar 9, 2012, 7:58:20 PM3/9/12
to AJSmit, jerryb...@gmail.com, ggp...@googlegroups.com
Hi Jerry, AJ -

This problem is now solved in the development version -

library(devtools)
dev_mode()
install_github('ggmap', 'dkahle')
library(ggmap)

lon = c(-67.2, -67.2,-61.95640829749075, -60.1334953);
lat = c(46, 43, 45.83594917946984, 43.9634132);
size = c(100,100,100,100);
myData = data.frame(lon,lat,size)

require(ggmap)
lonlatC <- c(lon=mean(myData$lon), lat=mean(myData$lat))
osm <- ggmap(center=lonlatC, source = "google",
zoom=6,maptype="satellite")
ggmapplot(osm, maprange = FALSE, expand = FALSE,
         base_layer=ggplot(myData, aes(x=lon, y=lat))) +
           geom_point(aes(size=size))

Cheers
dk.

On Mar 9, 2012, at 12:23 AM, AJSmit wrote:

David Kahle

unread,
Mar 9, 2012, 8:11:12 PM3/9/12
to JBlack, ggp...@googlegroups.com
BTW, you probably want to change some of the parameters...  Here's how it can be done.

lon <- c(-67.2, -67.2,-61.95640829749075, -60.1334953)
lat <- c(46, 43, 45.83594917946984, 43.9634132)
size <- 1:4
myData <- data.frame(lon, lat)

require(ggmap)
lonlatC <- c(lon=mean(myData$lon), lat=mean(myData$lat))
osm <- ggmap(
  center = lonlatC, source = "google", 
  zoom = 7, maptype = "satellite"
)
ggmapplot(osm, maprange = TRUE, expand = FALSE, 
  base_layer = ggplot(myData, aes(x=lon, y=lat))
) + geom_point(aes(size=size), colour = 'red')

dk.



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
<ggmap_point_mismatch.pdf>

JBlack

unread,
Mar 11, 2012, 6:14:03 PM3/11/12
to ggp...@googlegroups.com, AJSmit, jerryb...@gmail.com
Thank you. works well!

AJSmit

unread,
Mar 12, 2012, 8:32:52 AM3/12/12
to ggp...@googlegroups.com, AJSmit, jerryb...@gmail.com
Dear David,

That's great, it works perfectly!

Much appreciated,
AJ
Reply all
Reply to author
Forward
0 new messages