Skip to content

Handle NA gracefully in geom_sf #3546

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

Merged
merged 5 commits into from
Nov 4, 2019
Merged

Conversation

thomasp85
Copy link
Member

@thomasp85 thomasp85 commented Oct 1, 2019

Fixes #3536

This is an alternative implementation to the fix in #3537 that respects the different geometry types interpretation of NA. As the problem of NA's is unique to the vectorised constructors there is no need to handle geometrycollections in any special way as they will always be treated as scalars and grid accepts a single NA in gpar()

library(ggplot2)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3

pt <- st_point(1:2)

outer <- matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE)
pl <- st_polygon(list(outer))

# point is shown
sf1 <- st_sf(geometry = st_sfc(pl, pt))
ggplot(sf1) + geom_sf()

# point is still shown as expected
sf2 <- st_sf(geometry = st_sfc(st_geometrycollection(list(pl, pt))))
ggplot(sf2) + geom_sf()

# NA is handled fine for collections
ggplot(sf2) + geom_sf(colour = NA)

# as well as for vectorised geometries
sf3 <- st_sf(geometry = st_sfc(pt, st_point(3:4)), one_na = c(1, NA))
ggplot(sf3) + geom_sf(aes(size = one_na))

Created on 2019-10-01 by the reprex package (v0.2.1)

@thomasp85 thomasp85 added this to the ggplot2 3.3.0 milestone Oct 1, 2019
Copy link
Member

@clauswilke clauswilke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this in principle. A few comments:

@@ -107,6 +100,10 @@ remove_missing <- function(df, na.rm = FALSE, vars = names(df), name = "",

df
}
detect_missing <- function(df, vars, finite = FALSE) {
vars <- intersect(vars, names(df))
!cases(df[, vars, drop = FALSE], if (finite) is_finite else is_complete)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line correct? Where does is_complete come from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a function defined in utilitites.r. detect_missing() is more or less lifted from remove_missing() so that sf_grobs() can use the same logic as handle_na()

NEWS.md Outdated
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* `geom_sf()` now removes rows can't be plotted due to `NA` aesthetics (#3546, @thomasp85)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"removes rows that can't be plotted"

@clauswilke
Copy link
Member

Could you also verify that the example from #3483 still works?

@thomasp85
Copy link
Member Author

thomasp85 commented Oct 1, 2019

Could you link to the original bug report in one of the commit messages: #3536

It is linked at the top of the PR. Why would you want it in a commit message (that will get squashed away during merge)?

Could you also verify that the example from #3483 still works?

I've done that already (but forgot to mention). Do you want a repress of it?

Copy link
Member

@yutannihilation yutannihilation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Sorry for commenting this late...

@yutannihilation
Copy link
Member

yutannihilation commented Oct 27, 2019

One minor problem of this approach to move the process to outside of handle_na() is that na.rm no longer controls whether or not to warn; we need to choose always be quiet or always warn. I feel the latter is slightly better, but am not fully sure what should be done here.

> test_check("ggplot2")

── 1. Failure: geom_sf() removes rows containing missing aes (@test-geom-sf.R#85

`expect_identical(...)` did not produce any warnings.

── 2. Failure: geom_sf() removes rows containing missing aes (@test-geom-sf.R#89

`expect_identical(...)` did not produce any warnings.

── 3. Failure: geom_sf() removes rows containing missing aes (@test-geom-sf.R#94

`expect_identical(...)` did not produce any warnings.

(https://travis-ci.org/tidyverse/ggplot2/jobs/591938837#L1219-L1227)

@lock
Copy link

lock bot commented May 5, 2020

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/

@lock lock bot locked and limited conversation to collaborators May 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: geom_sf() is now too aggressive in removing rows
3 participants