/R/show_sig_fit.R

https://github.com/ShixiangWang/sigminer · R · 86 lines · 65 code · 6 blank · 15 comment · 14 complexity · 7a1649937611be951ec1fd4c9469fefe MD5 · raw file

  1. #' Show Signature Fit Result
  2. #'
  3. #' See [sig_fit] for examples.
  4. #'
  5. #' @inheritParams ggpubr::ggboxplot
  6. #' @inheritParams ggpubr::ggpar
  7. #' @inheritParams ggplot2::geom_boxplot
  8. #' @inheritParams sig_fit_bootstrap_batch
  9. #' @inheritParams show_sig_bootstrap
  10. #' @param fit_result result object from [sig_fit].
  11. #' @param samples samples to show, if `NULL`, all samples are used.
  12. #' @return a `ggplot` object.
  13. #' @export
  14. #' @seealso [sig_fit], [show_sig_bootstrap_exposure], [sig_fit_bootstrap], [sig_fit_bootstrap_batch]
  15. show_sig_fit <- function(fit_result, samples = NULL, signatures = NULL,
  16. plot_fun = c("boxplot", "violin", "scatter"),
  17. palette = "aaas",
  18. title = NULL,
  19. xlab = FALSE, ylab = "Signature exposure", legend = "none",
  20. width = 0.3, outlier.shape = NA,
  21. add = "jitter", add.params = list(alpha = 0.3),
  22. ...) {
  23. fun_setting <- plot_fun <- match.arg(plot_fun)
  24. plot_fun <- switch(plot_fun,
  25. boxplot = ggpubr::ggboxplot,
  26. violin = ggpubr::ggviolin,
  27. scatter = ggpubr::ggscatter
  28. )
  29. timer <- Sys.time()
  30. send_info("Started.")
  31. on.exit(send_elapsed_time(timer))
  32. send_info("Checking input format.")
  33. if (data.table::is.data.table(fit_result)) {
  34. dat <- data.table::copy(fit_result)
  35. } else if (is.list(fit_result)) {
  36. dat <- fit_result$expo
  37. } else if (is.matrix(fit_result)) {
  38. dat <- fit_result %>%
  39. as.data.frame() %>%
  40. tibble::rownames_to_column("Sig") %>%
  41. tidyr::pivot_longer(cols = -"Sig", names_to = "sample", values_to = "expo") %>%
  42. tidyr::pivot_wider(id_cols = "sample", names_from = "Sig", values_from = "expo") %>%
  43. data.table::as.data.table()
  44. } else {
  45. send_stop("Bad input, could you take a check?")
  46. }
  47. send_success("Checked.")
  48. send_info("Checking filters.")
  49. if (!is.null(samples)) {
  50. dat <- dplyr::filter(dat, .data$sample %in% samples)
  51. }
  52. if (!is.null(signatures)) {
  53. dat <- dplyr::select(dat, c("sample", signatures))
  54. }
  55. if (!nrow(dat) > 0) {
  56. send_stop("No data left to plot, could you check your input?")
  57. }
  58. send_info("Checked.")
  59. dat <- dat %>%
  60. dplyr::as_tibble() %>%
  61. tidyr::pivot_longer(-"sample", names_to = "sig", values_to = "exposure")
  62. send_info("Plotting.")
  63. ## Plotting
  64. if (isFALSE(fun_setting == "scatter")) {
  65. plot_fun(dat,
  66. x = "sig", y = "exposure", color = "sig", outlier.shape = outlier.shape,
  67. palette = palette, width = width, add = add, add.params = add.params,
  68. title = title, xlab = xlab, ylab = ylab, legend = legend, ...
  69. )
  70. } else {
  71. if (legend == "none") {
  72. send_warning("When plot_fun='scatter', setting legend='top' is recommended.")
  73. }
  74. plot_fun(dat,
  75. x = "sig", y = "exposure", color = "sample", shape = "sample",
  76. palette = palette,
  77. title = title, xlab = xlab, ylab = ylab, legend = legend, ...
  78. )
  79. }
  80. }