-
Notifications
You must be signed in to change notification settings - Fork 2k
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
default_aesthetics in geom_sf does not handle NULL properly #3963
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Confirmed. Here's a minimal reprex. The error happens when the data is NOT an sf and the scale of colour or fill is discrete. library(ggplot2)
nc <- sf::read_sf(system.file("shape/nc.shp", package="sf"))
nc$BIR74_bin <- cut_number(nc$BIR74, 3)
nc_tibble <- tibble::as_tibble(nc)
ggplot(nc) +
geom_sf(aes(fill = BIR74_bin)) ggplot(nc_tibble) +
geom_sf(aes(geometry = geometry, fill = BIR74_bin))
#> Error in if (type == "point") {: argument is of length zero Created on 2020-04-26 by the reprex package (v0.3.0) |
Specifically, things work the moment Lines 51 to 59 in d3d47be
library(ggplot2)
nc <- sf::read_sf(system.file("shape/nc.shp", package="sf"))
nc$BIR74_bin <- cut_number(nc$BIR74, 3)
nc_tibble <- tibble::as_tibble(nc)
# doesn't work
ggplot(nc_tibble) +
geom_sf(aes(geometry = geometry, fill = BIR74_bin))
#> Error in if (type == "point") {: argument is of length zero
# works
ggplot(nc_tibble) +
geom_sf(aes(geometry = geometry, fill = BIR74_bin), show.legend = "polygon") Created on 2020-04-26 by the reprex package (v0.3.0) |
I've created a PR to fix this: #3964 |
Related problem. Maybe library(ggplot2)
library(patchwork)
p1 <- rbind(c(1,1), c(2,2), c(3,3))
s1 <- rbind(c(0,3), c(0,4), c(1,5), c(2,5))
s2 <- rbind(c(0.2,3), c(0.2,4), c(1,4.8), c(2,4.8))
s3 <- rbind(c(0,4.4), c(0.6,5))
d_sf <- sf::st_sf(
g_point = sf::st_sfc(sf::st_multipoint(p1)),
g_line = sf::st_sfc(sf::st_multilinestring(list(s1,s2,s3))),
v = "a"
)
p1 <- ggplot(d_sf) +
geom_sf(aes(geometry = g_point, colour = "a"))
p2 <- ggplot(d_sf) +
geom_sf(aes(geometry = g_line, colour = "a"))
p1 / p2 Created on 2020-04-26 by the reprex package (v0.3.0) |
I mean, the absence of |
Good point. I'll look into it tomorrow. |
It's getting a bit convoluted, but here you go. library(ggplot2)
library(patchwork)
p1 <- rbind(c(1,1), c(2,2), c(3,3))
s1 <- rbind(c(0,3), c(0,4), c(1,5), c(2,5))
s2 <- rbind(c(0.2,3), c(0.2,4), c(1,4.8), c(2,4.8))
s3 <- rbind(c(0,4.4), c(0.6,5))
d_sf <- sf::st_sf(
g_point = sf::st_sfc(sf::st_multipoint(p1)),
g_line = sf::st_sfc(sf::st_multilinestring(list(s1,s2,s3))),
v = "a"
)
p1 <- ggplot(d_sf) +
geom_sf(aes(geometry = g_point, colour = "a"))
p2 <- ggplot(d_sf) +
geom_sf(aes(geometry = g_line, colour = "a"))
p1 / p2 Created on 2020-04-26 by the reprex package (v0.3.0) |
* make legend detection code more robust. fixes #3963 * properly infer geometry from the right column * add unit test * add news.md
When plotting a map using a discrete fill scale, I get the error below. The source seems to be default_aesthetics not handling a NULL value for "type" (the line numbers may be off, I was adding some debugging prints to check out internal values):
Passing a value for legend (or character value for show.legend) works fine:
You can repeat this using the data from this stack overflow post this stack overflow post and some small changes. Moving the fill aesthetic inside the aes mapping gives the error:
The text was updated successfully, but these errors were encountered: