From d075380559d9d2d039bddeefe38e581540701099 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Fri, 5 Nov 2021 23:05:40 +0100 Subject: [PATCH] ranking: Add plot method --- DESCRIPTION | 1 + NAMESPACE | 1 + R/ranking.R | 56 +++++++++++++++++++++++++++++++++++++ man/plot.geposan_ranking.Rd | 20 +++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 man/plot.geposan_ranking.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 4738307..a74b1fe 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,6 +26,7 @@ Imports: rlang Suggests: biomaRt, + plotly, rlog, stringr, usethis diff --git a/NAMESPACE b/NAMESPACE index 4d93e2b..4a4b515 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +S3method(plot,geposan_ranking) S3method(print,geposan_comparison) S3method(print,geposan_preset) export(analyze) diff --git a/R/ranking.R b/R/ranking.R index e55917d..7898bc9 100644 --- a/R/ranking.R +++ b/R/ranking.R @@ -37,6 +37,62 @@ ranking <- function(analysis, weights) { ) } +#' S3 method for plotting a ranking. +#' +#' @param gene_sets A list of gene sets (containing vectors of gene IDs) that +#' will be highlighted in the plot. +#' @param labels Labels for the gene sets. +#' +#' @seealso ranking() +#' +#' @export +plot.geposan_ranking <- function(ranking, gene_sets = NULL, labels = NULL) { + if (!requireNamespace("plotly", quietly = TRUE)) { + stop("Please install \"plotly\" to use this function.") + } + + plot <- plotly::plot_ly() |> + plotly::add_trace( + data = ranking, + x = ~rank, + y = ~score, + color = "All genes", + type = "scatter", + mode = "markers", + hoverinfo = "skip" + ) |> + plotly::layout( + xaxis = list(title = "Rank"), + yaxis = list(title = "Score") + ) + + if (length(gene_sets) > 0) { + # Take out the genes to be highlighted. + gene_set_data <- ranking[gene %chin% unlist(gene_sets)] + + # Add labels for each gene set. + for (i in seq_along(gene_sets)) { + gene_set_data[gene %chin% gene_sets[[i]], label := labels[i]] + } + + # Include gene information which will be used for laebling + gene_set_data <- merge(gene_set_data, genes, by.x = "gene", by.y = "id") + + plot <- plot |> plotly::add_trace( + data = gene_set_data, + x = ~rank, + y = ~score, + color = ~label, + text = ~name, + type = "scatter", + mode = "markers", + marker = list(size = 20) + ) + } + + plot +} + #' Find the best weights to rank the results. #' #' This function finds the optimal parameters to [ranking()] that result in the diff --git a/man/plot.geposan_ranking.Rd b/man/plot.geposan_ranking.Rd new file mode 100644 index 0000000..3bfaa6b --- /dev/null +++ b/man/plot.geposan_ranking.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ranking.R +\name{plot.geposan_ranking} +\alias{plot.geposan_ranking} +\title{S3 method for plotting a ranking.} +\usage{ +\method{plot}{geposan_ranking}(ranking, gene_sets = NULL, labels = NULL) +} +\arguments{ +\item{gene_sets}{A list of gene sets (containing vectors of gene IDs) that +will be highlighted in the plot.} + +\item{labels}{Labels for the gene sets.} +} +\description{ +S3 method for plotting a ranking. +} +\seealso{ +ranking() +}