R/visualize_ddiwas_severity.R

Defines functions visualize_ddiwas_severity

Documented in visualize_ddiwas_severity

#' Visualize DDIWAS results
#'
#' @param ddiwas_results A tibble
#' @param ddiwas_title A string
#' @param severity_vector A list
#' @param colors_vector A list
#' @param max.or A numeric
#' @return A highcharter plot \code{hc}
#' @export

visualize_ddiwas_severity <- function(ddiwas_results, ddiwas_title, severity_vector, colors_vector, min.or = 0.2, max.or=10, num_tests) {
  # last updated: 2020-06-11
  library(tidyverse)
  library(EmpiricalCalibration)
  library(highcharter)
  PATH <- "../data/"

  # for high chart legend
  x <- c("Drug:", "RxCUI ingredient:",
         "OR:", "se:", "pvalue:",
         "drugbank_description:",
         "nA:", "nB:","nC:","nD","Severity")
  y <- sprintf("{point.%s}", c("rxcui_ingr_name", "rxcui_ingr",
                               "or", "se","pval",
                               "drugbank_description",
                               "nA","nB","nC","nD","severity"))
  tltip <- tooltip_table(x, y)
  # color variables for vertical line
  color.black <- "#000000"
  # vertical line at OR = 1
  plotLine <- list(label = list(), color = color.black, width = 2, value = 1)
  # horizontal lines at p=0.05 and bonferroni-corrected p-value
  pvalue_horizontal_line <-
    list(
      label = list(),
      width = 2,
      value = -log10(0.05),
      color = 'blue')
  bonferroni_horizontal_line <-
    list(
      label = list(),
      width = 2,
      value = -log10(0.05/num_tests),
      color = 'red'
    )
  # Create chart variable and define axis parameters
  hc <- highchart() %>%
    hc_title(text = ddiwas_title) %>%
    hc_xAxis(
      title=list(text="Odds Ratio"),
      type = 'logarithmic',
      min=min.or,
      max=max.or,
      plotLines = list(plotLine)
    ) %>%
    hc_yAxis(title = list(text="-log10(P)"),
             plotLines = list(pvalue_horizontal_line, bonferroni_horizontal_line)
    )
  # Plot drugs
  num_vector <- seq(1, length(severity_vector), by = 1)
  for (i in num_vector){
    dfgroup<-filter(ddiwas_results, severity == severity_vector[i])
    hc <- hc %>%
      hc_add_series(
        name=severity_vector[i],
        dfgroup, type = "scatter",
        hcaes(x = or, y= -log10(pval),value = rxcui_ingr_name),
        marker = list(
          fillColor = colors_vector[[i]][1],
          lineColor = colors_vector[[i]][2],
          lineWidth = 0.5)
      )
  }
  hc <- hc %>% hc_tooltip(useHTML = TRUE, pointFormat = tltip)
  return(hc)
}
patrickwu510/ddiwas documentation built on June 26, 2020, 6:56 a.m.