Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
whiteheadedduck opened this issue Mar 22, 2020 · 1 comment
Closed

Comments

@whiteheadedduck
Copy link

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"
@clauswilke
Copy link
Member

We have a solution but it's going to be a while until it can be released: #3659

In the mean time, you can use the trick described here:
https://wilkelab.org/practicalgg/articles/goode.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants