R/fct_violinplot.R

Defines functions create_violinplot

##Initial code shamelessly borrowed from the iAtlas Portal
##https://github.com/CRI-iAtlas/shiny-iatlas/blob/09da641f37e7acdc149853a6b818775328054189/functions/violinplot.R

create_violinplot <- function(
  df, 
  x_col = "x",
  y_col = "y",
  key_col = NA,
  color_col = NA,
  label_col = NA,
  split_col = NA,
  xlab = "",
  ylab = "", 
  title = "", 
  source_name = NULL, 
  fill_colors = NA, 
  points = NULL,
  showlegend = T) {
  
  
  if(is.na(key_col)) key_col <- x_col
  if(is.na(color_col)) color_col <- x_col
  if(is.na(label_col)) label_col <- x_col
  if(is.na(split_col)) split_col <- x_col
  
  wrapr::let(
    alias = c(
      X = x_col,
      Y = y_col,
      KEY = key_col,
      COLOR = color_col,
      SPLIT = split_col,
      LABEL = label_col),
    plot_ly(
      df,
      x = ~X,
      y = ~Y,
      split = ~SPLIT,
      color = ~COLOR,
      key = ~KEY,
      text = ~LABEL,
      points = points,
      source = source_name,
      colors = fill_colors,
      type = 'violin',
      hoverinfo = 'text',
      showlegend = showlegend,
      box = list(
        visible = TRUE
      ),
      meanline = list(
        visible = TRUE
      ))) %>%
    layout(
      title = title,
      xaxis = list(title = xlab),
      yaxis = list(title = ylab)
    ) %>% 
    format_plotly() %>%
    I
}
allaway/kairos documentation built on Jan. 2, 2023, 1:44 a.m.