dkahle / ggmap

A package for plotting maps in R with ggplot2
767 stars 231 forks source link

Non-square maps misalignes pixels #245

Open lorenzwalthert opened 6 years ago

lorenzwalthert commented 6 years ago

This command

get_googlemap(size = c(360, 420))

run from the tidyup branch, produces a noisy picture, essentially the same as https://github.com/dkahle/ggmap/issues/17. I fixed it in my branch essentially the same way as it was fixed in #17: https://github.com/lorenzwalthert/ggmap/commit/c6b68ebbb02929b4d628410bfbd0fdff32ac3152

However, this was quick and dirty, maybe this had unexpected side effects.

mfaytak commented 5 years ago

I have recently tried to make use of this fix to make publication-quality maps which are not square-shaped. The fix does not actually allow for the user to select most possible image sizes in that most (but not all) image sizes re-introduce the "noisy image" issue (as in issue #17). For instance, I am able to get this to produce a nice, intelligible map (test1.pdf):

map_center <- c(lon=7.054235, lat=11.514064)
test <- get_googlemap(center=map_center, zoom=4, size=c(640,320))
ggmap(test)

test1.pdf

But a "noisy" map (test2.pdf) is produced again if size is replaced with any number of other vectors. I am having trouble figuring out a generalization that makes both 640, 320 (used in this post) and 420, 360 (as in the first post in this issue) work with this fix, but where a value like 720, 360 always produces a "noisy" map.

map_center <- c(lon=7.054235, lat=11.514064)
test <- get_googlemap(center=map_center, zoom=4, size=c(642,321))
ggmap(test)

test2.pdf

A warning is issued if the dimensions aren't multiples of each other, e.g. size=c(2n,n). For instance, if size is set to c(641,320) (test3.pdf):

Warning message:
In matrix(map, nrow = scale * size[2], ncol = scale * size[1]) :
  data length [819200] is not a sub-multiple or multiple of the number of columns [1282]

test3.pdf

But if they are, no warning is issued, and the map is still "noisy", as in test2.pdf where size=c(642,321).

lorenzwalthert commented 5 years ago

No idea how to solve that quickly. You could also try an alternative approach with https://github.com/paleolimbot/ggspatial.