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
Reduce "object 'geometry' not found" error with geom_sf() #2872
Comments
There's a third case that works, when you're actually mapping the geometry aesthetic just like all other aesthetics: library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(ggplot2)
nc_gpkg <- sf::st_read(system.file("gpkg/nc.gpkg", package = "sf"), quiet = TRUE)
# works, uses proper mapping for geometry column
ggplot(nc_gpkg) +
geom_sf(aes(fill = AREA, geometry = geom)) Created on 2018-08-29 by the reprex package (v0.2.0). So if we routinely mapped geometry columns, just like we do with all other data columns, the problem wouldn't exist. Maybe that would be good practice at least within all the ggplot2 documentation and examples? The downside to your proposed solution is that it is not universal. If a data frame contains a column called |
Thinking some more about this, it seems the problem is that However, this got me thinking: Since this is a generic problem that may come up over and over, maybe we could solve it by adding a function such as ggplot_add.Layer <- function(object, plot, object_name) {
object$setup_layer(plot)
plot$layers <- append(plot$layers, object)
... This might enable all sorts of new applications. Would just require careful evaluation of whether anything that is currently done in |
Agreed. Actually, one of the things that frustrate me is the very inconsistency that One more thing is that I have to copy and paste these lines every time I implement a new Lines 238 to 246 in 71cb174
So, in general, I agree with you that always specifying the Yet, specifying |
|
It would probably make sense to put them into a function, just like Lines 84 to 94 in 71cb174
|
I mean, there will be many variants of Now I'm thinking I need something like |
Anyway, this seems reasonable to me. May I create a PR to improve examples?
|
@yutannihilation I have created a PR (#2875) that solves the issue in a general way and also eliminates the need for copying the geometry detection code all over the place. Could you take a look? @hadley Do you see any showstoppers? The only change visible to the outside world is an additional, optional argument in the Examples: library(ggplot2)
nc_gpkg <- sf::st_read(system.file("gpkg/nc.gpkg", package = "sf"), quiet = TRUE)
# works now
ggplot(nc_gpkg) +
geom_sf(aes(fill = AREA)) # works now
ggplot(nc_gpkg) +
stat_sf_coordinates()
#> Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may
#> not give correct results for longitude/latitude data # still needs explicit mapping because not an sf layer
ggplot(nc_gpkg) +
geom_errorbarh(
aes(geometry = geom,
xmin = stat(x) - 0.1,
xmax = stat(x) + 0.1,
y = stat(y),
height = 0.04),
stat = "sf_coordinates"
)
#> Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may
#> not give correct results for longitude/latitude data Created on 2018-08-30 by the reprex package (v0.2.0). |
Thanks, it seems fine. But, what was in my mind was a bit different; yours won't cover this case:
IIUC, requires us to create a new I think mechanism of auto-mapping should be an generic function because it depends on the class of object, not the type of the geom, in general. This may be too general, though. I will send a PR that illustrates my idea... |
I hadn't thought about the grouping issue, but I think it can be solved with my approach. The logic would be "if the data frame for this layer has grouping information and a group aesthetic is not set for this layer, then map the grouping information to group for this layer". This can be done in the The key idea is that there are things that we want to be layer specific but they depend on the global data frame and mapping, and |
Ah, Thanks, it makes sense. Here's my PoC. We may eventually need some sort of this |
Oops, sorry, I was wrong. My PoC requires |
I'm not sure whether a generic is the right approach, because one data frame could match multiple criteria (for example, both be grouped and contain a geometry column). It seems to me more akin to a checklist of conditions and corresponding actions on the mapping. |
I'm happy to add auto-grouping to my PR, but I'd like to hear @hadley's position first. As it stands right now, the PR is essentially a bug fix, even if it adds a new extension mechanism. If we add auto-grouping, we're fundamentally changing how ggplot2 works and that's not appropriate for the upcoming bug-fix release. One option could be to implement auto-grouping to make sure the API works and then disable it for the upcoming release. Alternatively, if Hadley thinks this whole approach of |
Sorry, I forgot the upcoming release is for bug-fix... I understand. Will have a closer look on your PR later! |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
Currently,
geom_sf()
works without specifyinggeometry
aes only when:sf
object has a geometry column namedgeometry
sf
object is passed directly togeom_sf()
, not via the plot dataI'm afraid it's a bit confusing that the example of
nc
won't work if we just replacenc
with anothersf
object.Created on 2018-08-29 by the reprex package (v0.2.0).
I want to let
ggplot(nc_gpkg) + geom_sf(aes(fill = AREA))
simply work. For example, implementingfortify.sf()
seems to solve our problem quickly.But, I'm not fully sure this does the right thing... I feel this is similar kind of problem that we wanted to map grouping variable of
grouped_df
automatically togroup
aes, but couldn't (#2378). Are there any smarter way to create mappings automatically?The text was updated successfully, but these errors were encountered: