21
21
# ' finite, boundary effect of default density estimation will be corrected by
22
22
# ' reflecting tails outside `bounds` around their closest edge. Data points
23
23
# ' outside of bounds are removed with a warning.
24
+ # ' @param quantile.colour,quantile.color,quantile.linewidth,quantile.linetype
25
+ # ' Default aesthetics for the quantile lines. Set to `NULL` to inherit from
26
+ # ' the data's aesthetics. Set `quantile.linetype = 1` for regular quantiles.
24
27
# ' @export
25
28
# ' @references Hintze, J. L., Nelson, R. D. (1998) Violin Plots: A Box
26
29
# ' Plot-Density Trace Synergism. The American Statistician 52, 181-184.
@@ -91,11 +94,44 @@ geom_violin <- function(mapping = NULL, data = NULL,
91
94
... ,
92
95
trim = TRUE ,
93
96
bounds = c(- Inf , Inf ),
97
+ quantile.colour = NULL ,
98
+ quantile.color = NULL ,
99
+ quantile.linetype = 0L ,
100
+ quantile.linewidth = NULL ,
101
+ draw_quantiles = deprecated(),
94
102
scale = " area" ,
95
103
na.rm = FALSE ,
96
104
orientation = NA ,
97
105
show.legend = NA ,
98
106
inherit.aes = TRUE ) {
107
+
108
+ extra <- list ()
109
+ if (lifecycle :: is_present(draw_quantiles )) {
110
+ deprecate_soft0(
111
+ " 3.6.0" ,
112
+ what = " geom_violin(draw_quantiles)" ,
113
+ with = " geom_violin(quantiles.linetype)"
114
+ )
115
+ check_numeric(draw_quantiles )
116
+
117
+ # Pass on to stat when stat accepts 'quantiles'
118
+ stat <- check_subclass(stat , " Stat" , current_call(), caller_env())
119
+ if (" quantiles" %in% stat $ parameters()) {
120
+ extra $ quantiles <- draw_quantiles
121
+ }
122
+
123
+ # Turn on quantile lines
124
+ if (! is.null(quantile.linetype )) {
125
+ quantile.linetype <- max(quantile.linetype , 1 )
126
+ }
127
+ }
128
+
129
+ quantile_gp <- list (
130
+ colour = quantile.color %|| % quantile.colour ,
131
+ linetype = quantile.linetype ,
132
+ linewidth = quantile.linewidth
133
+ )
134
+
99
135
layer(
100
136
data = data ,
101
137
mapping = mapping ,
@@ -110,6 +146,8 @@ geom_violin <- function(mapping = NULL, data = NULL,
110
146
na.rm = na.rm ,
111
147
orientation = orientation ,
112
148
bounds = bounds ,
149
+ quantile_gp = quantile_gp ,
150
+ !!! extra ,
113
151
...
114
152
)
115
153
)
@@ -140,7 +178,7 @@ GeomViolin <- ggproto("GeomViolin", Geom,
140
178
flip_data(data , params $ flipped_aes )
141
179
},
142
180
143
- draw_group = function (self , data , ... , flipped_aes = FALSE ) {
181
+ draw_group = function (self , data , ... , quantile_gp = list ( linetype = 0 ), flipped_aes = FALSE ) {
144
182
data <- flip_data(data , flipped_aes )
145
183
# Find the points for the line to go all the way around
146
184
data <- transform(data ,
@@ -159,25 +197,28 @@ GeomViolin <- ggproto("GeomViolin", Geom,
159
197
newdata <- vec_rbind0(newdata , newdata [1 ,])
160
198
newdata <- flip_data(newdata , flipped_aes )
161
199
162
- # Draw quantiles if requested, so long as there is non-zero y range
163
- if (" quantile" %in% names(newdata )) {
200
+ violin_grob <- GeomPolygon $ draw_panel(newdata , ... )
164
201
165
- quantiles <- newdata [! is.na(newdata $ quantile ),]
166
- quantiles $ group <- match(quantiles $ quantile , unique(quantiles $ quantile ))
202
+ if (! " quantile" %in% names(newdata ) ||
203
+ all(quantile_gp $ linetype == 0 ) ||
204
+ all(quantile_gp $ linetype == " blank" )) {
205
+ return (ggname(" geom_violin" , violin_grob ))
206
+ }
167
207
168
- quantile_grob <- if (nrow(quantiles ) == 0 ) {
169
- zeroGrob()
170
- } else {
171
- GeomPath $ draw_panel(quantiles , ... )
172
- }
208
+ # Draw quantiles if requested, so long as there is non-zero y range
209
+ quantiles <- newdata [! is.na(newdata $ quantile ),]
210
+ quantiles $ group <- match(quantiles $ quantile , unique(quantiles $ quantile ))
211
+ quantiles $ linetype <- quantile_gp $ linetype %|| % quantiles $ linetype
212
+ quantiles $ linewidth <- quantile_gp $ linewidth %|| % quantiles $ linewidth
213
+ quantiles $ colour <- quantile_gp $ colour %|| % quantiles $ colour
173
214
174
- ggname(" geom_violin" , grobTree(
175
- GeomPolygon $ draw_panel(newdata , ... ),
176
- quantile_grob )
177
- )
215
+ quantile_grob <- if (nrow(quantiles ) == 0 ) {
216
+ zeroGrob()
178
217
} else {
179
- ggname( " geom_violin " , GeomPolygon $ draw_panel(newdata , ... ) )
218
+ GeomPath $ draw_panel(quantiles , ... )
180
219
}
220
+
221
+ ggname(" geom_violin" , grobTree(violin_grob , quantile_grob ))
181
222
},
182
223
183
224
draw_key = draw_key_polygon ,
0 commit comments