Skip to content
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

report a bug of geom_sf: the legend does not appear, even if show.legend is TRUE #3636

Closed
microly opened this issue Nov 22, 2019 · 9 comments

Comments

@microly
Copy link
Contributor

microly commented Nov 22, 2019

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:

library(ggplot2)
library(sf)

nc <- st_read(system.file("shape/nc.shp", package = "sf"))
ggplot(nc) + geom_sf(show.legend = TRUE)

a

@clauswilke
Copy link
Member

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.

@microly
Copy link
Contributor Author

microly commented Nov 25, 2019

Thanks for your reply, and sorry for this late reply.
I think that I may not ask a right question.
I have noticed the lack of any scale for geometry mapping.
Dose it make sense that create a scale for geometry, like "scale_geometry_identity"?
I know it is useless to draw the main plot, but it would be helpful to draw the legend of geom_sf layer, especially when there are multiple layers in the plot (map).
@clauswilke

@yutannihilation
Copy link
Member

So, are you saying you want to show the type of geometry (point, line, polygon) on the legend? If so, I understand the need to some extent, but we'll never have scale_geometry_*() because geometry is not an aesthetic you can map to some scale. Polygon is polygon.

Besides, IIUC, the legend is not implemented to show the differences of the type of Geom among layers. For example, I guess this is not what you want:

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)

@yutannihilation
Copy link
Member

Btw, I'm a bit skeptical about this part.

when there are multiple layers in the plot

If you want to identify each layer, probably you'll need to map the IDs of the layers to some aesthetic (e.g. colour) anyway.

@microly
Copy link
Contributor Author

microly commented Nov 26, 2019

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.

@yutannihilation
Copy link
Member

geom_sf automatically determine the legend type, such as point, line and polygon.

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...

@microly
Copy link
Contributor Author

microly commented Nov 26, 2019

thanks!
I know that one PR cannot solve the problem, so i am preparing multiple PRs.
By now, I think I can solve the problem, but I need to improve the codes before submitting the PRs.

@teunbrand
Copy link
Collaborator

Running #3636 (comment) reprex without the show.legend seems to run as expected on current main branch. Can this issue be closed? If not, would anyone mind explaining what still isn't correct about the behaviour?

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

@yutannihilation
Copy link
Member

Thanks for the reprex, I agree we can close this issue now.

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

No branches or pull requests

5 participants