-
Notifications
You must be signed in to change notification settings - Fork 2.1k
report a bug of geom_sf: the legend does not appear, even if show.legend is TRUE #3636
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
Comments
What do you envision the legend should show in this example? The only two aesthetic mappings in your plot are position aesthetics, and they have guides along the axes. |
Thanks for your reply, and sorry for this late reply. |
So, are you saying you want to show the type of Besides, IIUC, the legend is not implemented to show the differences of the type of library(ggplot2)
library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 6.1.1
sf1 <- st_sf(geometry = st_sfc(st_point(c(2, 2))))
sf2 <- st_sf(geometry = st_sfc(st_linestring(rbind(c(0, 0), c(2, 1)))))
sf3 <- st_sf(geometry = st_sfc(st_polygon(list(rbind(c(0, 1), c(0, 2), c(1, 2), c(1, 1), c(0, 1))))))
ggplot() +
geom_sf(data = sf1, aes(colour = "layer 1"), show.legend = "point") +
geom_sf(data = sf2, aes(colour = "layer 2"), show.legend = "line") +
geom_sf(data = sf3, aes(colour = "layer 3"), show.legend = "polygon") Created on 2019-11-26 by the reprex package (v0.3.0) |
Btw, I'm a bit skeptical about this part.
If you want to identify each layer, probably you'll need to map the IDs of the layers to some aesthetic (e.g. |
thanks for your kind reply! @yutannihilation I understand all you said. The ggplot2 is a very powerful tool, but it is not as convenient as GIS softwares in drawing maps by geom_sf, especailly in the part of lenged. As a user of both ggplot2 and GIS, I try to make up the gaps between them by preparing some PRs. The first one can enable geom_sf automatically determine the legend type, such as point, line and polygon. I will submit it soon, please consider it. |
Yeah, I think we all agree with you here (c.f. #3572 (comment)), so PR for this is welcome. But, I'm afraid it won't solve your problem... |
thanks! |
Running #3636 (comment) reprex without the library(ggplot2)
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
sf1 <- st_sf(geometry = st_sfc(st_point(c(2, 2))))
sf2 <- st_sf(geometry = st_sfc(st_linestring(rbind(c(0, 0), c(2, 1)))))
sf3 <- st_sf(geometry = st_sfc(st_polygon(list(rbind(c(0, 1), c(0, 2), c(1, 2), c(1, 1), c(0, 1))))))
ggplot() +
geom_sf(data = sf1, aes(colour = "layer 1")) +
geom_sf(data = sf2, aes(colour = "layer 2")) +
geom_sf(data = sf3, aes(colour = "layer 3")) Created on 2023-12-14 with reprex v2.0.2 |
Thanks for the reprex, I agree we can close this issue now. |
According to the documentation, the legend of geom_sf should be always showed if show.legend is TRUE.
However, if there is no aesthetics other than geometry, the legend of geom_sf does not appear even if show.legend is TRUE.
Below is an example:
The text was updated successfully, but these errors were encountered: