Skip to content

Annotation_map does not work with coord_sf #3653

Closed

Description

@markpayneatwork

When usingcoord_sf() to change the projection of a map, the background layer produced by annotation_map() is not getting reprojected.

First, a plot without changing projection, using the annotation_map layer to give more context to North Carolina

library(ggplot2)
library(sf)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

ggplot() +
  annotation_map(map_data("state"),fill="antiquewhite",colour="darkgrey")+
  geom_sf(data = nc)

image

However, changing the projection looses the annotation layer, but reprojects the geometry layer

ggplot() +
  annotation_map(map_data("state"),fill="antiquewhite",colour="darkgrey")+
  geom_sf(data = nc)+
  coord_sf(crs = st_crs(3347))

image

It looks to me like the annotation_layer is not being reprojected.

Activity

  1. clauswilke commented on Dec 4, 2019

    @clauswilke
    Member

    This happens because annotation_map() is old code that predates geom_sf(). Only sf layers are reprojected. While it would be nice to reproject other layers, there are numerous technical hurdles that make me think it's not going to happen anytime soon.

    At this point, the best way forward is probably to amend the documentation accordingly.

  2. added this to the ggplot2 3.4.0 milestone on Feb 7, 2020
  3. thomasp85 commented on May 18, 2022

    @thomasp85
    Member

    @clauswilke has this been fixed by #3659?

  4. clauswilke commented on May 18, 2022

    @clauswilke
    Member

    Yes, correct, just need to set the appropriate default crs. This particular case is actually included in the examples, so I think we're good as is.

    library(ggplot2)
    library(sf)
    #> Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1
    nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
    
    ggplot() +
      annotation_map(map_data("state"),fill="antiquewhite",colour="darkgrey")+
      geom_sf(data = nc) +
      coord_sf(crs = st_crs(3347), default_crs = st_crs(4326))

    Created on 2022-05-18 by the reprex package (v2.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions