luciano
luciano

Reputation: 13852

Using gghighlight with ggmap

I can make a map using ggmap using code below, but if I try to use gghighlight I get an error. Is it possible to use gghighlight with ggmap?

library(ggmap)

hdf <- get_map("houston, texas")

mu <- c(-95.3632715, 29.7632836); nDataSets <- sample(4:10,1)
chkpts <- NULL
for(k in 1:nDataSets){
  a <- rnorm(2); b <- rnorm(2);
  si <- 1/3000 * (outer(a,a) + outer(b,b))
  chkpts <- rbind(
    chkpts,
    cbind(MASS::mvrnorm(rpois(1,50), jitter(mu, .01), si), k)
  )
}
chkpts <- data.frame(chkpts)
names(chkpts) <- c("lon", "lat","class")

ggmap(hdf, extent = "normal") +
  geom_point(aes(x = lon, y = lat, colour = class), data = chkpts, alpha = .5) -> 
  my_plot

my_plot

enter image description here

library(gghighlight)

my_plot +
    gghighlight(class > 3, aes(x = lon, y = lat, colour = class), data = chkpts)

# Error in `check_bad_predicates()`:
# ! Did you mistype some argument name? Or, did you mean `==`?: data = chkpts
# Backtrace:
#  1. gghighlight::gghighlight(...)
#  2. gghighlight:::check_bad_predicates(predicates)

Upvotes: 1

Views: 74

Answers (1)

Z.Lin
Z.Lin

Reputation: 29125

I haven't used gghighlight for much, but I suspect it's comparing filter conditions against the data source in the base layer. Reversing the order so that the chkpts dataset is in ggplot() & the ggmap object is added later worked for me:

my_plot_reversed <- ggplot(chkpts, aes(x = lon, y = lat, colour = class)) +
  inset_ggmap(hdf) +
  geom_point(alpha = 0.5, size = 5) +
  coord_map(xlim = with(attr(hdf, "bb"), c(ll.lon, ur.lon)),
            ylim = with(attr(hdf, "bb"), c(ll.lat, ur.lat)))

my_plot
my_plot_reversed
# both look the same

my_plot_reversed + gghighlight(class > 3)
# but this works with gghighlight

plot with reversed layers

Note: the help file for coord_map mentioned it has been superseded by coord_sf, but for what it's worth, the ggmap version still uses coord_map, and there was a visible difference between the two plots when I tried to use coord_sf instead. Hence I stuck with coord_map in the suggested workaround.

Upvotes: 0

Related Questions