Skip to content

Unexpected failure of density plot without any error or warning #3250

Closed

Description

@clauswilke

I encountered the following example when teaching density plots. If a discrete variable is encoded as numerical (e.g., 0/1), and that variable is mapped onto fill, the variable values get replaced with NA and no fill is applied/no grouping is calculated. Importantly, this happens without any kind of error or warning message, so students get no indication of what they have done wrong, or that something went wrong at all.

It's not immediately clear to me whether and how we can catch this situation, but it would be good if we could. Reprex follows.

library(ggplot2)

# desired plot
p1 <- ggplot(mtcars, aes(mpg, fill = factor(am))) + geom_density(alpha = 0.4)
p1

# broken plot because factor() is missing
p2 <- ggplot(mtcars, aes(mpg, fill = am)) + geom_density(alpha = 0.4)
p2

# fill has been set to NA
head(layer_data(p2))
#>            y PANEL        x    density    scaled  ndensity     count  n
#> 1 0.01922476     1 10.40000 0.01922476 0.2829075 0.2829075 0.6151923 32
#> 2 0.01951519     1 10.44599 0.01951519 0.2871815 0.2871815 0.6244861 32
#> 3 0.01980689     1 10.49198 0.01980689 0.2914740 0.2914740 0.6338204 32
#> 4 0.02010390     1 10.53796 0.02010390 0.2958448 0.2958448 0.6433248 32
#> 5 0.02040182     1 10.58395 0.02040182 0.3002289 0.3002289 0.6528583 32
#> 6 0.02070509     1 10.62994 0.02070509 0.3046918 0.3046918 0.6625630 32
#>   group ymin       ymax fill weight colour alpha size linetype
#> 1    -1    0 0.01922476   NA      1  black   0.4  0.5        1
#> 2    -1    0 0.01951519   NA      1  black   0.4  0.5        1
#> 3    -1    0 0.01980689   NA      1  black   0.4  0.5        1
#> 4    -1    0 0.02010390   NA      1  black   0.4  0.5        1
#> 5    -1    0 0.02040182   NA      1  black   0.4  0.5        1
#> 6    -1    0 0.02070509   NA      1  black   0.4  0.5        1

Created on 2019-04-17 by the reprex package (v0.2.1)

Activity

  1. clauswilke commented on Apr 17, 2019

    @clauswilke
    MemberAuthor

    I believe the problematic code lives here:

    ggplot2/R/stat-.r

    Lines 109 to 110 in 8359806

    unique <- uniquecols(old)
    missing <- !(names(unique) %in% names(new))

    Non-unique data columns are silently dropped. We could issue a warning for non-unique missing columns. I just don't know how often such a warning might flag cases that are actually correct.

  2. added a commit that references this issue on Apr 19, 2019
    5c0059a
  3. added
    wip
    on May 21, 2019
  4. added this to the ggplot2 3.4.0 milestone on Feb 7, 2020
  5. clauswilke commented on May 11, 2022

    @clauswilke
    MemberAuthor

    @thomasp85 Since we're considering cleanup and breaking changes, I would bring up this issue once more, which is an eternal thorn in my side. It comes up every year when I teach ggplot2. I made a draft PR a long time ago that I could revive.

    Since all this code does is create a warning, it's not really going to break anything. But, there's the risk of false-positive warnings. The way to avoid false positives is to allow geoms to indicate that the dropped columns were dropped on purpose, and I believe 2-3 geoms in ggplot2 itself would require this modification to avoid spurious warnings.

  6. thomasp85 commented on May 12, 2022

    @thomasp85
    Member

    Yeah, let's revive it and have a look... maybe the best thing is simply to ship the warning and get feedback from users as to when it's a false positive so we can tune it after the fact

  7. clauswilke commented on May 12, 2022

    @clauswilke
    MemberAuthor

    Ok. Will try to get this done next week.

  8. self-assigned this
    on May 20, 2022
  9. added a commit that references this issue on Jun 9, 2022
    c245ae6
  10. added a commit that references this issue on Jul 7, 2022
    60f8654
  11. removed
    wip
    on Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions