Skip to content

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

Closed

Description

@microly

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

Activity

  1. clauswilke commented on Nov 22, 2019

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

  2. microly commented on Nov 25, 2019

    @microly
    ContributorAuthor

    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

  3. yutannihilation commented on Nov 26, 2019

    @yutannihilation
    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)

  4. yutannihilation commented on Nov 26, 2019

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

  5. microly commented on Nov 26, 2019

    @microly
    ContributorAuthor

    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.

  6. yutannihilation commented on Nov 26, 2019

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

  7. microly commented on Nov 26, 2019

    @microly
    ContributorAuthor

    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.

  8. teunbrand commented on Dec 14, 2023

    @teunbrand
    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

  9. yutannihilation commented on Dec 14, 2023

    @yutannihilation
    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions