Skip to content

geom_rect rectangle disappears when used together with coord_sf in R #3909

Closed
@whiteheadedduck

Description

@whiteheadedduck

I am plotting a map with ggplot2 and would like to have a rectangle surrounding it. I have tried to create the rectangle using geom_rect function, but whenever I include coord_sf function in the code chunk, the rectangle disappears. Below are the maps I got with the following codes. Classes of zone_map and ecoregion_polygon objects are "sf" and both are read in using st_read function.

ggplot() + 
  geom_sf(data = zone_map, color = "black", fill = gray62_transparent) +
  geom_sf(data = ecoregion_polygon, color = "black", fill = burlywood1_transparent) +
  geom_rect(aes(xmin = 20, xmax = 50, ymin = 33, ymax = 47), color = "black", fill = NA)

enter image description here

ggplot() + 
  geom_sf(data = zone_map, color = "black", fill = gray62_transparent) +
  geom_sf(data = ecoregion_polygon, color = "black", fill = burlywood1_transparent) +
  geom_rect(aes(xmin = 20, xmax = 50, ymin = 33, ymax = 47), color = "black", fill = NA) +
  coord_sf(crs = "+proj=laea +lat_0=38 +lon_0=35")

enter image description here

As you see in the second map, the geom_rect rectangle disappears upon addition of coord_sf line. How can I solve this issue? I have tried to create another polygon using st_polygon function, but I obtained the same results. I also tried setting both objects' crs as the azimuthal "+proj=laea +lat_0=38 +lon_0=35" projection, which I want to plot my map in, and I got the same results. Below are the st_crs outputs of zone_map and ecoregion_polygon objects.

> st_crs(zone_map); st_crs(ecoregion_polygon)
Coordinate Reference System:
  EPSG: 4326 
  proj4string: "+proj=longlat +datum=WGS84 +no_defs"
Coordinate Reference System:
  EPSG: 4326 
  proj4string: "+proj=longlat +datum=WGS84 +no_defs"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions