Skip to content

geom_bin_2d() should recognise 0-width data #6207

Closed

Description

@teunbrand

In the example below, the computation goes wrong because in the y-direction we don't have any width.

library(ggplot2)
df <- data.frame(x = 1:5, y = 1)

ggplot(df, aes(x, y)) +
  geom_bin_2d()
#> Warning: Computation failed in `stat_bin2d()`.
#> Caused by error in `bin2d_breaks()` at ggplot2/R/stat-bin2d.R:64:5:
#> ! `origin` must be a number, not `NaN`.

Created on 2024-11-27 with reprex v2.1.1

I think the reasonable thing to do in such a case is to choose 1 bin for the y-direction with 1 width.

Activity

  1. teunbrand commented on Nov 28, 2024

    @teunbrand
    CollaboratorAuthor

    Seems to apply more broadly to any 2D binning:

    library(ggplot2)
    df <- data.frame(x = 1:5, y = 1, z = 5)
    
    ggplot(df, aes(x, y, z = z)) +
      stat_summary_2d()
    #> Warning: Computation failed in `stat_summary2d()`.
    #> Caused by error in `bin2d_breaks()` at ggplot2/R/stat-summary-2d.R:104:5:
    #> ! `origin` must be a number, not `NaN`.

    Created on 2024-11-28 with reprex v2.1.1

  2. added
    bug
    on Dec 2, 2024
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions