@@ -72,8 +72,8 @@ StatYdensity <- ggproto("StatYdensity", Stat,
72
72
compute_group = function (self , data , scales , width = NULL , bw = " nrd0" , adjust = 1 ,
73
73
kernel = " gaussian" , trim = TRUE , na.rm = FALSE , flipped_aes = FALSE ) {
74
74
if (nrow(data ) < 2 ) {
75
- cli :: cli_warn( " Groups with fewer than two data points have been dropped. " )
76
- return (data_frame0() )
75
+ ans <- data_frame0( x = data $ x , n = nrow( data ) )
76
+ return (ans )
77
77
}
78
78
range <- range(data $ y , na.rm = TRUE )
79
79
modifier <- if (trim ) 0 else 3
@@ -101,15 +101,21 @@ StatYdensity <- ggproto("StatYdensity", Stat,
101
101
data , scales , width = width , bw = bw , adjust = adjust , kernel = kernel ,
102
102
trim = trim , na.rm = na.rm
103
103
)
104
+ if (any(data $ n < 2 )) {
105
+ cli :: cli_warn(
106
+ " Cannot compute density for groups with fewer than two data points."
107
+ )
108
+ }
104
109
105
110
# choose how violins are scaled relative to each other
106
111
data $ violinwidth <- switch (scale ,
107
112
# area : keep the original densities but scale them to a max width of 1
108
113
# for plotting purposes only
109
- area = data $ density / max(data $ density ),
114
+ area = data $ density / max(data $ density , na.rm = TRUE ),
110
115
# count: use the original densities scaled to a maximum of 1 (as above)
111
116
# and then scale them according to the number of observations
112
- count = data $ density / max(data $ density ) * data $ n / max(data $ n ),
117
+ count = data $ density / max(data $ density , na.rm = TRUE ) *
118
+ data $ n / max(data $ n ),
113
119
# width: constant width (density scaled to a maximum of 1)
114
120
width = data $ scaled
115
121
)
0 commit comments