Skip to content

No warning in stat_ydensity when dropping groups #4111

Closed
@andrewwbutler

Description

@andrewwbutler

Hi,

I noticed the following behavior in geom_violin when there are fewer than three data points in a given violin. In this scenario, one of the violins gets dropped without a warning.

library(ggplot2)
table(mtcars$cyl, mtcars$am)
#>    
#>      0  1
#>   4  3  8
#>   6  4  3
#>   8 12  2
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) + 
  geom_violin(scale = "width", aes(fill = factor(am))) 

Created on 2020-07-01 by the reprex package (v0.3.0)

It seems a related issue was addressed in stat_density in #2142 . I would propose that stat_ydensity be updated similarly here

if (nrow(data) < 3) return(new_data_frame())
Happy to put together a small PR for this if people agree.

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