You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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~
@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?
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?
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.
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.
@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.
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.
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:)
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)
Activity
yutannihilation commentedon Jan 10, 2020
One thing in my mind is
map_data()for sf.microly commentedon Jan 11, 2020
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.
yutannihilation commentedon Jan 11, 2020
Agreed, those ongoing pull requests are definitely needed to replace geom_map(). Thanks.
microly commentedon Jan 11, 2020
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.
yutannihilation commentedon Jan 11, 2020
Isn't this can be done with st_as_sf(coords = c("x", "y"))...?
microly commentedon Jan 11, 2020
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~
clauswilke commentedon Jan 12, 2020
@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 atgeom_map()but then converts everything into the sf framework?yutannihilation commentedon Jan 12, 2020
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 likesf_data()should belong to sf (or some sf extension package).Sounds good. But, isn't it possible to convert data.frame transparently in
Layer$setup_layer()(when some option likeconvert = TRUEis specified) so that we don't need another geom?microly commentedon Jan 12, 2020
@clauswilke
Wonderful idea!
@yutannihilation
I think it could be done in the stat step.
yutannihilation commentedon Jan 12, 2020
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
microly commentedon Jan 12, 2020
@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.
microly commentedon Jan 12, 2020
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.
clauswilke commentedon Jan 12, 2020
@yutannihilation When you say "so we don't need another geom", do you mean we just modify
geom_map()so it works withcoord_sf(), either as default or when a specific parameter is set? This sounds reasonable to me. The currentgeom_map()code is only a thin wrapper aroundgeom_polygon()anyways, so a more generalgeom_map()would simply provide two alternative wrappers.microly commentedon Jan 12, 2020
@clauswilke
Agreed!
We can just refactor the geom_map().
yutannihilation commentedon Jan 12, 2020
@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 modifyinggeom_map()sounds reasonable to me.25 remaining items
microly commentedon Mar 12, 2020
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.
microly commentedon Mar 12, 2020
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:)
microly commentedon Mar 12, 2020
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.
clauswilke commentedon Mar 12, 2020
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
microly commentedon Mar 12, 2020
I see~
Thank you:)
yutannihilation commentedon Jun 27, 2020
Now that #3659 is merged, let's add this to the milestones of the next release.
clauswilke commentedon Jun 27, 2020
I think it's more important to mark
coord_map()andcoord_quickmap()as deprecated and to show howgeom_map()can be used withcoord_sf(). (Look at the documentation forannotation_map()for an example.)Specifically, here are a few proposed changes for the examples provided with
geom_map():Created on 2020-06-26 by the reprex package (v0.3.0)
yutannihilation commentedon Jun 27, 2020
Thanks, agreed. We need to review the related documentations. The new example looks good to me!
thomasp85 commentedon May 18, 2022
@clauswilke are you interested in making a conclusion on this? As far as I understand we landed on simply updating the docs?
clauswilke commentedon May 18, 2022
@thomasp85 Yes, correct, update the documentation and the examples and otherwise leave everything as is. I can add it to my todo list.
fixes tidyverse#3721
fixes #3721 (#4865)