annotation_map_tile() changes color depending on order

I first posted this as an issue on the ggspatial Github repo, but thought there might be more activity here. My apologies if this is a faux pas – I can delete one or the other.

The carto light basemap looks very different depending on when I include annotation_map_tile() in my ggplot. (See bottom for my reprex.)

When I include it first, the basemap is light:

ggplot() +
  tiles +
  geom_spatvector(data = corners, fill = NA, color = "black")

tiles-first

The image is darker when I include it after another layer:

ggplot() +
  geom_spatvector(data = corners, fill = NA, color = "black") +
  tiles

tiles-second

I imagine the tiles are the same, so maybe it's something to do with opacity? I have set alpha = 1, and the color difference remains when I ensure there is no panel background.

Reprex

library(ggplot2)
library(ggspatial)
library(terra)

corners <- vect(
  data.frame(lon = c(91.11226, 91.24588), lat = c(23.38367, 23.48849)),
  geom = c("lon", "lat"),
  crs = "epsg:4326")
tiles <- annotation_map_tile(type = "cartolight", zoom = 14, alpha = 1)
ggplot() +
  tiles +
  geom_spatvector(data = corners, fill = NA, color = "black")
ggsave("tiles-first.png", width = 5, height = 5)
ggplot() +
  geom_spatvector(data = corners, fill = NA, color = "black") +
  tiles
ggsave("tiles-second.png", width = 5, height = 5)

The effect is the same when I include theme(panel.background = element_rect(fill = "white")) after ggplot()

As with so many things spatial, this is simply a matter of CRS. Have now found this is caused by reprojecting the tiles without warning, which darkens the image. I didn't think this was the case because I never got the warning message, "Coordinate system already present. Adding new coordinate system, which will replace the existing one", in either of the version of the plotting.

ggplot() +
  coord_sf(expand = F, crs = "epsg:3857") +
  geom_sf(data = corners, fill = NA, color = "black") +
  tiles +
  coord_sf(crs = "epsg:3857")
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.