Skip to content

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

Closed
@clauswilke

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)

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