diff --git a/NAMESPACE b/NAMESPACE index 4a4b515..e729dad 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,11 +1,12 @@ # Generated by roxygen2: do not edit by hand -S3method(plot,geposan_ranking) S3method(print,geposan_comparison) S3method(print,geposan_preset) export(analyze) export(compare) export(optimal_weights) +export(plot_boxplot) +export(plot_scores) export(preset) export(ranking) import(data.table) diff --git a/R/plots.R b/R/plots.R new file mode 100644 index 0000000..839a51b --- /dev/null +++ b/R/plots.R @@ -0,0 +1,95 @@ +#' Plot a ranking as a scatter plot of scores. +#' +#' This function requires the package `plotly`. +#' +#' @param ranking The ranking to visualize. +#' @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. This is required if gene sets are +#' given and has to have the same length. +#' +#' @seealso ranking() +#' +#' @export +plot_scores <- 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 labeling + 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 +} + +#' Visualize a ranking by comparing gene sets in a boxplot. +#' +#' This function requires the package `plotly`. +#' +#' @param ranking The ranking to visualize. +#' @param gene_sets A list of gene sets (containing vectors of gene IDs) that +#' will be shown as separate boxes. +#' @param labels Labels for the gene sets. This is required if gene sets are +#' given and has to have the same length. +#' +#' @seealso ranking() +#' +#' @export +plot_boxplot <- function(ranking, gene_sets = NULL, labels = NULL) { + if (!requireNamespace("plotly", quietly = TRUE)) { + stop("Please install \"plotly\" to use this function.") + } + + data <- copy(ranking) + + # Add labels for each gene set. + for (i in seq_along(gene_sets)) { + data[gene %chin% gene_sets[[i]], label := labels[i]] + } + + # Label the other genes. + data[!gene %chin% unlist(gene_sets), label := "Other genes"] + + plotly::plot_ly( + data = data, + y = ~score, + color = ~label, + type = "box" + ) +} diff --git a/R/ranking.R b/R/ranking.R index 7898bc9..e55917d 100644 --- a/R/ranking.R +++ b/R/ranking.R @@ -37,62 +37,6 @@ 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 deleted file mode 100644 index 3bfaa6b..0000000 --- a/man/plot.geposan_ranking.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% 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() -} diff --git a/man/plot_boxplot.Rd b/man/plot_boxplot.Rd new file mode 100644 index 0000000..d2eba06 --- /dev/null +++ b/man/plot_boxplot.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plots.R +\name{plot_boxplot} +\alias{plot_boxplot} +\title{Visualize a ranking by comparing gene sets in a boxplot.} +\usage{ +plot_boxplot(ranking, gene_sets = NULL, labels = NULL) +} +\arguments{ +\item{ranking}{The ranking to visualize.} + +\item{gene_sets}{A list of gene sets (containing vectors of gene IDs) that +will be shown as separate boxes.} + +\item{labels}{Labels for the gene sets. This is required if gene sets are +given and has to have the same length.} +} +\description{ +This function requires the package \code{plotly}. +} +\seealso{ +ranking() +} diff --git a/man/plot_scores.Rd b/man/plot_scores.Rd new file mode 100644 index 0000000..a09c590 --- /dev/null +++ b/man/plot_scores.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plots.R +\name{plot_scores} +\alias{plot_scores} +\title{Plot a ranking as a scatter plot of scores.} +\usage{ +plot_scores(ranking, gene_sets = NULL, labels = NULL) +} +\arguments{ +\item{ranking}{The ranking to visualize.} + +\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. This is required if gene sets are +given and has to have the same length.} +} +\description{ +This function requires the package \code{plotly}. +} +\seealso{ +ranking() +}