Skip to content

Commit

Permalink
label_outliers uses rownames/numbers if no labelvar
Browse files Browse the repository at this point in the history
detect_outliers returns now a list with values and positions
  • Loading branch information
abusjahn committed Nov 28, 2023
1 parent 186fd12 commit 2b2d57e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
46 changes: 28 additions & 18 deletions R/plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ ggcormat <- function(cor_mat, p_mat = NULL,
#' @param plotbase ggplot object to add labels to.
# ' @param xvar x variable for grouping.
# ' @param yvar y variable with possible outliers.
#' @param labelvar variable to use as label.
#' @param labelvar variable to use as label. If NULL, rownames or rownumbers are used.
#' @param coef coefficient for boxplot.stats, defaults to 1.5.
#' @param nudge_x nudge in x direction, defaults to 0.
#' @param nudge_y nudge in y direction, defaults to 0.
Expand All @@ -241,40 +241,46 @@ ggcormat <- function(cor_mat, p_mat = NULL,
# ' @examples
# todo: group by facet variables
#' @export
label_outliers <- function(plotbase, labelvar, #xvar, #yvar,
label_outliers <- function(plotbase, labelvar=NULL, #xvar, #yvar,
coef=1.5, nudge_x=0, nudge_y=0,
color="darkred", size=3, hjust=0,
face="bold") {
if(!requireNamespace("ggrepel", quietly = TRUE)) {
stop("ggrepel package is required")
}
plotdata <- plotbase$data
if(is.null(labelvar)) {
labelvar <- "Position"
plotdata <- mutate(plotdata, Position = row.names(plotdata))
}

plotlist <- ggplot_build(plotbase)
xvar <- plotbase$mapping[["x"]] |> as_label()
# plotlist[["plot"]][["layers"]][[1]][["computed_mapping"]][1] |>
# as.character() |>
# str_remove("~")
# plotlist[["plot"]][["layers"]][[1]][["computed_mapping"]][1] |>
# as.character() |>
# str_remove("~")
yvar <- plotbase$mapping[["y"]] |> as_label()
#plotbase$layers[[1]]$computed_mapping[2] |>
# plotlist[["plot"]][["layers"]][[1]][["computed_mapping"]][2] |>
# as.character() |>
# str_replace_all(
# c('^.+\\"(.+)\\".*'="\\1",
# "~"=""))
#plotbase$layers[[1]]$computed_mapping[2] |>
# plotlist[["plot"]][["layers"]][[1]][["computed_mapping"]][2] |>
# as.character() |>
# str_replace_all(
# c('^.+\\"(.+)\\".*'="\\1",
# "~"=""))
# groupvars <- xvar
# if(!is.null(plotlist$layout$facet$params$row)){
facet_rows <- names(plotlist$layout$facet$params$row)
# groupvars <- c(groupvars,facet_rows)
facet_rows <- names(plotlist$layout$facet$params$row)
# groupvars <- c(groupvars,facet_rows)
# }
# if(!is.null(plotlist$layout$facet$params$col)){
facet_cols <- names(plotlist$layout$facet$params$col)
facet_cols <- names(plotlist$layout$facet$params$col)
facet_wraps <- names(plotlist$layout$facet$params$facets)
groupvars <- c(xvar,facet_rows,facet_cols,facet_wraps)
outpositions <-
plotbase$data |>
group_by(across(all_of(groupvars))) |>
reframe(across(all_of(yvar),
~detect_outliers(.x, coef=coef))) |>
left_join(plotbase$data)
~detect_outliers(.x, coef=coef)$outliers)) |>
left_join(plotdata)
plotbase +
ggrepel::geom_text_repel(data=outpositions,
aes(label=!!sym(labelvar)),
Expand All @@ -297,14 +303,18 @@ label_outliers <- function(plotbase, labelvar, #xvar, #yvar,
#' @param x numeric vector.
#' @param coef coefficient for boxplot.stats, defaults to 1.5.
#'
#' @return A numeric vector with outliers.
#' @return A list with elements positions and outliers as numeric vectors.
#'
#' @examples
#' detect_outliers(rnorm(100))
#' @export
detect_outliers <- function(x, coef=1.5) {
qnt <- quantile(x, probs=c(.25, .75), na.rm = TRUE)
iqr <- diff(qnt)
upper <- qnt[2] + coef * iqr
lower <- qnt[1] - coef * iqr
out <- x[x > upper | x < lower]
o_pos <- which(x > upper | x < lower)
out <- list("positions"=o_pos,
"outliers"=x[o_pos])
return(out)
}
5 changes: 4 additions & 1 deletion man/detect_outliers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/label_outliers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2b2d57e

Please sign in to comment.