dkahle / ggmap

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

Darken option ignored when changing the axis limits #342

Closed RonEfrat closed 1 year ago

RonEfrat commented 1 year ago

Hi,

I loaded a map and plotted it using ggmap, with added routes on it. I wanted to use the "darken" option, but it only works if I do not change the axes limits.

Example:

m1 = get_googlemap(center = c(mean(dt$lon), mean(dt$lat)),
                         style = c('feature:administrative.country|element:labels|visibility:off',
                                   'feature:administrative.locality|element:labels|visibility:off'),
                           zoom = 4, maptype = 'terrain', source = 'google')

This is working:

ggmap(m1, darken = c(0.2, "white")) +
  geom_path(data = dt, aes(x = lon, y = lat, group = ID), 
            size  = 1.5)

But this ignores the darken command:

ggmap(m1, darken = c(0.2, "white")) +
  geom_path(data = dt, aes(x = lon, y = lat, group = ID), 
            size  = 1.5) +
  xlim(16,40) +
  scale_y_continuous(breaks = c(15, 20, 25, 30, 35), limits = c(12.5,37.5))

*I tried only with the xlim or only with the scale_y_continuous, both ignore the darken

Wil appreciate your help.

Thanks!

dkahle commented 1 year ago

Hey @RonEfrat - Hard for me to reproduce that without dt, but I get the idea. The problem is probably that the scale_y_continuous() is filtering the boundary points of the rectangle that's doing the darkening. Instead of setting the limits with a scale function, it's usually better to do that with a coord function. Usually that's coord_cartesian(ylim = c( _ , _ )), but in this case you'd use coord_map( ylim = c(12.5, 37.5) ) with the same argument. Hope that helps!