Skip to content

Commit db8cc73

Browse files
updated prepare_boxly to display outlier using type=2
1 parent 1765036 commit db8cc73

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

R/prepare_boxly.R

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,15 @@ prepare_boxly <- function(meta,
150150
# Calculate summary statistics and add these variables into tbl
151151
plotds <- mapply(
152152
function(s, u) {
153-
t <- as.vector(summary(s[[y]]))
153+
vals <- stats::quantile(s[[y]], probs = c(0, 0.25, 0.5, 0.75, 1),
154+
type = 2, na.rm = TRUE, names = FALSE)
154155

155156
if (nrow(s) > 5) {
156-
iqr.range <- t[5] - t[2]
157-
upper_outliers <- t[5] + iqr.range * 1.5
158-
lower_outliers <- t[2] - iqr.range * 1.5
159-
s$outlier <- ifelse((s[[y]] > upper_outliers | s[[y]] < lower_outliers), s[[y]], NA)
157+
iqr.range <- vals[4] - vals[2] # Q3 - Q1 (type=2)
158+
upper_outliers <- vals[4] + iqr.range * 1.5 # Q3 + 1.5*IQR
159+
lower_outliers <- vals[2] - iqr.range * 1.5 # Q1 - 1.5*IQR
160+
s$outlier <- ifelse((s[[y]] > upper_outliers | s[[y]] < lower_outliers),
161+
s[[y]], NA)
160162
} else if (nrow(s) > 0) {
161163
s$outlier <- NA
162164
} else {
@@ -168,18 +170,21 @@ prepare_boxly <- function(meta,
168170
# mutate ans for output
169171
if (nrow(s) > 0) {
170172
ans <- s
171-
ans$min <- t[1]
172-
ans$q1 <- t[2]
173-
ans$median <- t[3]
174-
ans$mean <- t[4]
175-
ans$q3 <- t[5]
176-
ans$max <- t[6]
173+
# ensure vals and mean_val exist (vals from quantile(..., type=2) earlier)
174+
mean_val <- mean(s[[y]], na.rm = TRUE)
175+
176+
ans$min <- vals[1]
177+
ans$q1 <- vals[2]
178+
ans$median <- vals[3]
179+
ans$mean <- mean_val
180+
ans$q3 <- vals[4]
181+
ans$max <- vals[5]
177182

178183
ans
179184
}
180185
},
181-
split(tbl, tbl[, c(obs_var, obs_group, x)]),
182-
names(split(tbl, tbl[, c(obs_var, obs_group, x)], sep = ", ")),
186+
split(tbl, list(tbl[[obs_var]], tbl[[obs_group]], tbl[[x]])),
187+
names(split(tbl, list(tbl[[obs_var]], tbl[[obs_group]], tbl[[x]]), sep = ", ")),
183188
SIMPLIFY = FALSE
184189
)
185190

0 commit comments

Comments
 (0)