Skip to content

Mark geom_map() as superseded #3721

Closed

Description

@yutannihilation

I believe we are already in the mood of moving away from geom_map() as geom_sf() almost superseded it. But, AFAIK, this hasn't been discussed formally so far. Considering we keep developing many codes around geom_sf(), I think it's good to mark geom_map() soft-deprecated before we face difficulties about the different behaviors between them.

Here I file this issue to discuss what functionalities are missing from geom_sf() and its related functions, which might drive people to fall back to geom_map(). Any thoughts?

Edit: to be clear, I don't think we can remove geom_map() considering its wide spread usages (so I chose the word "soft-deprecated"). Still, I believe we can discourage the usage of geom_map() to prevent further confusions by improving geom_sf() or providing some alternatives.

Activity

  1. yutannihilation commented on Jan 10, 2020

    @yutannihilation
    MemberAuthor

    One thing in my mind is map_data() for sf.

  2. microly commented on Jan 11, 2020

    @microly
    Contributor

    geom_map can accept data.frame object, but geom_sf has to use sf object.

    Before deprecate geom_map, I think #3659 , #3655 and #3654 should be accomplished.

  3. yutannihilation commented on Jan 11, 2020

    @yutannihilation
    MemberAuthor

    Agreed, those ongoing pull requests are definitely needed to replace geom_map(). Thanks.

  4. microly commented on Jan 11, 2020

    @microly
    Contributor

    Maybe, sf_data() is needed to transform data.frame to sf for geom_sf(), just as map_data() did for geom_map().

    It can be another solution for #3659 , #3655 and #3654.

    As I mentioned, #3655 (comment), it is a issue about data, so just handle the data.

  5. yutannihilation commented on Jan 11, 2020

    @yutannihilation
    MemberAuthor

    sf_data() is needed to transform data.frame to sf for geom_sf()

    Isn't this can be done with st_as_sf(coords = c("x", "y"))...?

  6. microly commented on Jan 11, 2020

    @microly
    Contributor

    1.st_as_sf() can only output point(s), if the input is a data.frame.
    However, the users may want to draw a line or a polygon.

    2.Sf indeed supply more constructor, such as st_linestring and st_polygon, to create a line or polygon.
    But it is too complicated for ggplot2 users, I think most of them are not familiar with the internal structure of sf geometry.

    3.So, a light weighted and user friendly sf_data() would be needed.
    It can only handle point, line, polygon and polygon with hole, this is enough for ggplot2 users because the geometry (points) and multiple geometry (multipoint) look the same in the plot.

    it may be sth like:

    sf_data(data, coords = c("x", "y"), crs = 4326, group = NULL, subgroup = NULL, type = c("point", "line", "polygon"))

    data, a data.frame
    coords, just like the coords for st_as_sf
    crs, coordinate reference system
    group, for creating a line or polygon
    subgroup, for the hole in the polygon, just like geom_polygon
    type, sf geometry type

    This is a tentative suggestion, please forgive me if it is wrong~

  7. clauswilke commented on Jan 12, 2020

    @clauswilke
    Member

    @microly I agree with your general thoughts on this, but I'd like to propose a different approach: How about a geom_map_sf() that takes exactly the same inputs at geom_map() but then converts everything into the sf framework?

  8. yutannihilation commented on Jan 12, 2020

    @yutannihilation
    MemberAuthor

    1.st_as_sf() can only output point(s), if the input is a data.frame.
    However, the users may want to draw a line or a polygon.

    Ah, makes sense. But, I'm a bit skeptical about implementing it in ggplot2. IIUC, in the pre-sf days, we needed to define ggplot2's own convention about the map data and provide the utilities (like various fortify() methods) for this. So, I feel something like sf_data() should belong to sf (or some sf extension package).

    How about a geom_map_sf() that takes exactly the same inputs at geom_map() but then converts everything into the sf framework?

    Sounds good. But, isn't it possible to convert data.frame transparently in Layer$setup_layer() (when some option like convert = TRUE is specified) so that we don't need another geom?

  9. microly commented on Jan 12, 2020

    @microly
    Contributor

    @clauswilke
    Wonderful idea!

    @yutannihilation
    I think it could be done in the stat step.

  10. yutannihilation commented on Jan 12, 2020

    @yutannihilation
    MemberAuthor

    I feel the geometry column should be determined before stat. Probably it also can be done in the stat step, but setup_layer() seems the better place to me.

    ggplot2/R/layer-sf.R

    Lines 29 to 37 in 0183011

    # automatically determine the name of the geometry column
    # and add the mapping if it doesn't exist
    if ((isTRUE(self$inherit.aes) && is.null(self$mapping$geometry) && is.null(plot$mapping$geometry)) ||
    (!isTRUE(self$inherit.aes) && is.null(self$mapping$geometry))) {
    if (is_sf(data)) {
    geometry_col <- attr(data, "sf_column")
    self$mapping$geometry <- as.name(geometry_col)
    }
    }

  11. microly commented on Jan 12, 2020

    @microly
    Contributor

    @yutannihilation
    sorry, I misunderstand your comment.

    Another geom seems to be needed.
    For short, geom_sf() is for sf object, geom_map_sf() is for data.frame.
    This would be more clear and user-friendly.

    As I see, a lot of ggplot2 users do not have enough knowlege about spatial data object, such as sf.

  12. microly commented on Jan 12, 2020

    @microly
    Contributor

    Just like the author of #3717, a lot of ggplot2 users know few about sf.
    They just want to throw a data.frame into ggplot2 and get a map as easily as possible.
    Then, geom_map_sf() fill the bill.

  13. clauswilke commented on Jan 12, 2020

    @clauswilke
    Member

    @yutannihilation When you say "so we don't need another geom", do you mean we just modify geom_map() so it works with coord_sf(), either as default or when a specific parameter is set? This sounds reasonable to me. The current geom_map() code is only a thin wrapper around geom_polygon() anyways, so a more general geom_map() would simply provide two alternative wrappers.

  14. microly commented on Jan 12, 2020

    @microly
    Contributor

    @clauswilke
    Agreed!

    We can just refactor the geom_map().

  15. yutannihilation commented on Jan 12, 2020

    @yutannihilation
    MemberAuthor

    @microly
    I see, I agree with you to some extent. Having different geoms for sf and for data.frame sounds good.

    @clauswilke
    I meant to say about extending geom_sf() to accept data.frame with conversion, but modifying geom_map() sounds reasonable to me.

  16. 25 remaining items

  17. microly commented on Mar 12, 2020

    @microly
    Contributor

    I do not mean to change the ggplot2 API.

    I think a new stat may be needed to solve this problem.
    The stat can transform the data.frame (with its crs) to sf.
    Three sf geometry types are enough: point, line, polygon (with holes).

    #3659 is great, especially it can work with geom_mark_hull(), it is wonderful!

    But there is a question:
    If users have to transform the crs of data.frame before ploting, they can plot without #3659 .
    All they need to do is that just transform the crs to the same as the sf of first layer.

    As a GISer, I saw a lot of ggplot2 users that know few about sf (or GIS) and just want to plot one or some of data.frames onto a map.
    Usually, the task is too urgent for them to learn sf.
    Or they only need to draw very a few map, it is too expensive for them to learn sf.
    This problem makes them very frustrated.
    So I hope that ggplot2 can be convenience enough for them.

  18. microly commented on Mar 12, 2020

    @microly
    Contributor

    If users can noly throw the data.frame into ggplot2 and tell ggplot2 the crs, then ggplot2 can draw the data onto a map.
    This will make them very happy:)

  19. microly commented on Mar 12, 2020

    @microly
    Contributor

    I saw this problem from a view of GISer, not a desinger of ggplot2, so please forgive me if my idea is wrong or even foolish.

  20. clauswilke commented on Mar 12, 2020

    @clauswilke
    Member

    Users will be able to transform their data frame with one function call, see this example:
    #3659 (comment)

    Also, @paleolimbot is working on additional stats that make this easier. Not all of them need to live in ggplot2 itself: https://cran.r-project.org/web/packages/ggspatial/index.html

  21. microly commented on Mar 12, 2020

    @microly
    Contributor

    I see~
    Thank you:)

  22. added this to the ggplot2 3.4.0 milestone on Jun 27, 2020
  23. yutannihilation commented on Jun 27, 2020

    @yutannihilation
    MemberAuthor

    Now that #3659 is merged, let's add this to the milestones of the next release.

  24. clauswilke commented on Jun 27, 2020

    @clauswilke
    Member

    I think it's more important to mark coord_map() and coord_quickmap() as deprecated and to show how geom_map() can be used with coord_sf(). (Look at the documentation for annotation_map() for an example.)

    Specifically, here are a few proposed changes for the examples provided with geom_map():

    # modified from `geom_map()` examples, part "Better example"
    library(maps)
    library(ggplot2)
    
    crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
    
    # Equivalent to crimes %>% tidyr::pivot_longer(Murder:Rape)
    vars <- lapply(names(crimes)[-1], function(j) {
      data.frame(state = crimes$state, variable = j, value = crimes[[j]])
    })
    crimes_long <- do.call("rbind", vars)
    
    states_map <- map_data("state")
    
    # current example, leave in
    ggplot(crimes, aes(map_id = state)) +
      geom_map(aes(fill = Murder), map = states_map) +
      expand_limits(x = states_map$long, y = states_map$lat)

    # current example, remove
    last_plot() + coord_map()

    # proposed new example
    ggplot(crimes, aes(map_id = state)) +
      geom_map(aes(fill = Murder), map = states_map) +
      # crs = 102003 is an Albers equal area projection, see: https://epsg.io/102003
      coord_sf(crs = 102003, xlim = c(-125, -70), ylim = c(25, 52))

    # current example, remove
    ggplot(crimes_long, aes(map_id = state)) +
      geom_map(aes(fill = value), map = states_map) +
      expand_limits(x = states_map$long, y = states_map$lat) +
      facet_wrap( ~ variable)

    # proposed new example
    ggplot(crimes_long, aes(map_id = state)) +
      geom_map(aes(fill = value), map = states_map) +
      coord_sf(crs = 102003, xlim = c(-125, -70), ylim = c(25, 52)) +
      facet_wrap( ~ variable)

    Created on 2020-06-26 by the reprex package (v0.3.0)

  25. yutannihilation commented on Jun 27, 2020

    @yutannihilation
    MemberAuthor

    Thanks, agreed. We need to review the related documentations. The new example looks good to me!

  26. thomasp85 commented on May 18, 2022

    @thomasp85
    Member

    @clauswilke are you interested in making a conclusion on this? As far as I understand we landed on simply updating the docs?

  27. clauswilke commented on May 18, 2022

    @clauswilke
    Member

    @thomasp85 Yes, correct, update the documentation and the examples and otherwise leave everything as is. I can add it to my todo list.

  28. self-assigned this
    on May 18, 2022
  29. added a commit that references this issue on Jun 7, 2022
    7dee073
  30. added a commit that references this issue on Jun 10, 2022
    819d014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions