R/outliers_boxplot_with_plotly.R

Defines functions outliers_boxplot_with_plotly

Documented in outliers_boxplot_with_plotly

#' Create a an interactive boxplot using plotly with annotated outliers.
#'
#' @param x A character vector or a numeric vector for x axis
#' @param y A character vector or a numeric vector for y axis
#' @param label A character vector with a label for each point
#' @param title A string which appears as tittle
#' @param bg_colour Background colour
#'
#' @return Dataframe
#' @export
#'
outliers_boxplot_with_plotly <- function(x, y, label, title = '', bg_colour = '#e5ecf6'){
  if(!requireNamespace("plotly", quietly = TRUE))
    stop("the 'plotly' package needs to be installed first")
  ## Check arguments
  if(!is.character(label))
    stop("'label' should be character")
  if(!is.character(title))
    stop("'title' should be character")
  if(!is.character(bg_colour))
    stop("'bg_colour' should be character")
  if(list(x, y, label) %>%
     lengths() %>% unique() %>% length()!= 1)
    stop("All vectors should have the same length ")
  boxplot <- plotly::plot_ly(type = 'box') %>%
    plotly::add_trace(
      x = x,
      y =  y,
      color = x,
      text = label,
      hoverinfo = 'text')%>%
    plotly::layout(title = title, plot_bgcolor = bg_colour)
  return(boxplot)
}
currocam/FascinRSCA documentation built on March 21, 2022, 6:29 a.m.