Skip to content

Report a bug of guide-bins: it cannot work with geom_sf. #3582

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

Closed
microly opened this issue Oct 23, 2019 · 3 comments · Fixed by #3593
Closed

Report a bug of guide-bins: it cannot work with geom_sf. #3582

microly opened this issue Oct 23, 2019 · 3 comments · Fixed by #3593

Comments

@microly
Copy link
Contributor

microly commented Oct 23, 2019

The guide-bins seems cannot work with geom_sf.

Some examples:

For geom_point, both "coloursteps" and "bins" guides work well:

library(ggplot2)
library(tibble)

ggplot(tibble(x = 1:10, y = x, z = x), aes(x, y, fill = z)) +
    geom_point(shape = 21, size = 10) +
    scale_fill_stepsn(guide = "coloursteps", colours = terrain.colors(10))

p1

ggplot(tibble(x = 1:10, y = x, z = x), aes(x, y, fill = z)) +
    geom_point(shape = 21, size = 10) +
    scale_fill_stepsn(guide = "bins", colours = terrain.colors(10))

p2

But, for geom_sf, only "coloursteps" can work.
There seems to be a bug in guide-bins.

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"))

ggplot() +
    geom_sf(aes(fill = AREA), data = nc) +
    scale_fill_stepsn(guide = "coloursteps", colours = terrain.colors(10))

p3

ggplot() +
    geom_sf(aes(fill = AREA), data = nc) +
    scale_fill_stepsn(guide = "bins", colours = terrain.colors(10))

# Error in do.call("cbind", lapply(guide$geoms, function(g) g$data$size/10))[seq_len(n_keys),  : 
# subscript out of bounds
@yutannihilation
Copy link
Member

Thanks, confirmed. The error occurs here when g$data doesn't contain size column. Probably it should be handled by the next if branch before getting subsetted?

ggplot2/R/guide-bins.R

Lines 321 to 327 in 6f5ffea

key_size_mat <- do.call("cbind",
lapply(guide$geoms, function(g) g$data$size / 10)
)[seq_len(n_keys), , drop = FALSE]
if (nrow(key_size_mat) == 0 || ncol(key_size_mat) == 0) {
key_size_mat <- matrix(0, ncol = 1, nrow = n_keys)
}

@microly
Copy link
Contributor Author

microly commented Oct 28, 2019

OK, thanks for your work~

yutannihilation added a commit that referenced this issue Nov 4, 2019
Fix #3582

* Subset key_size_mat only when it's not empty
@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 a pull request may close this issue.

2 participants