R/plot_cds.R

Defines functions plot_cds

Documented in plot_cds

#' Returns barplot of mutation (cds) occurences
#'
#' plot_cds function uses mut_cds function in mut_cds.R file to get frequency data
#' of each mutation and return barplot using the data extracted
#'
#' @param mutFile A .tsv file containing COSMIC point mutations
#' @param hgncFile A .tsv file with complete approved human gene nomenclature IDs
#'
#' @return Returns barplot (descending order) using mutation frequency data
#' obtained from mut_cds function
#'
#' @examples
#' # Using data files available in the package: "MutantData.tsv" and "HGNC.tsv"
#'
#' # plot_cds("Data/MutantData.tsv", "Data/HGNC.tsv")
#'
#' @references
#' Kabacoff , R. (n.d.). Bar plots. Quick-R: Bar Plots. Retrieved November 21,
#' 2021, from https://www.statmethods.net/graphs/bar.html.
#'
#' Abedin, J., & Mittal, H. V. (n.d.). R Graphs. R Graphs Cookbook (Second
#' Edition) - Displaying values on top of or next to the bars. Retrieved November
#'  21, 2021, from https://subscription.packtpub.com/book/big_data_and_business_
#'  intelligence/9781783988785/6/ch06lvl1sec69/displaying-values-on-top-of-or-
#'  next-to-the-bars.
#'
#'@export
#'
plot_cds <- function(mutFile, hgncFile){
  # source mut_cds.R file to use the mut_cds function in this script
  #source("R/mut_cds.R")

  # get frequency table generated by mut_cds function
  data_cds <- CANMutVisual::mut_cds(mutFile, hgncFile)

  # re-order: Decreasing order to improve the visualization
  data_cds <- data_cds[order(data_cds$Freq, decreasing = TRUE), ]

  # Now, visualize this sorted data into bar plot
  bar <- barplot(data_cds$Freq,
          # Add title of the bargraph
          main = "MUTATION_CDS Frequency Plot",
          xlab = "Frequency", # x-axis label
          ylab = "Types of Mutation", # y-axis label
          names.arg = data_cds$cdsData, # labels
          # trial&error to fit all labels in y-axis
          cex.names = 0.425,
          # set different colors for each bars
          col = c("#B77CA5", "#A98BBA",
                  "#959BCA", "#7BABD3",
                  "#5FBAD5", "#47C7CF",
                  "#42D3C3", "#57DDB1",
                  "#78E59C", "#9EEA87",
                  "#C6EE74", "#EFEE69"),
          # create Horizontal bar graph
          # (for better visualization of each bar length)
          horiz = TRUE,
          xlim=c(0,200000),
            )
  # Add labels beside each bar to present specific frequency variables
  # 11000 added to format the labels so that it does not overlap with bars
  text(data_cds$Freq + 11000, bar, labels = as.character(data_cds$Freq), cex = 0.45)
  }

#[END]
Jiyun1201/CANMutVisual documentation built on Dec. 18, 2021, 1:38 a.m.