Skip to content

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

Closed

Description

@microly

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

Activity

  1. yutannihilation commented on Oct 28, 2019

    @yutannihilation
    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)
    }

  2. microly commented on Oct 28, 2019

    @microly
    ContributorAuthor

    OK, thanks for your work~

  3. added a commit that references this issue on Nov 4, 2019
    bad6f38
  4. lock commented on May 5, 2020

    @lock

    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/

  5. locked and limited conversation to collaborators on May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions