From 98ee52b122f41db594494d5f9f77b4bafd71cb34 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Sun, 14 Aug 2022 16:20:54 +0200 Subject: [PATCH] plots: Add rankings correlation plot --- NAMESPACE | 1 + R/plots.R | 106 +++++++++++++++++++++++++++++++ man/plot_rankings_correlation.Rd | 32 ++++++++++ 3 files changed, 139 insertions(+) create mode 100644 man/plot_rankings_correlation.Rd diff --git a/NAMESPACE b/NAMESPACE index 8a00b14..60d16ad 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,7 @@ export(plot_boxplot) export(plot_chromosomes) export(plot_positions) export(plot_rankings) +export(plot_rankings_correlation) export(plot_scores) export(plot_scores_by_position) export(preset) diff --git a/R/plots.R b/R/plots.R index 49e63b5..ee70814 100644 --- a/R/plots.R +++ b/R/plots.R @@ -204,6 +204,112 @@ plot_rankings <- function(rankings, gene_sets) { plot } +#' Plot a scatter plot to compare two rankings. +#' +#' This function requires the package `plotly`. +#' +#' @param ranking_x The ranking to be shown on the X axis. +#' @param ranking_y The ranking to be shown on the Y axis. +#' @param name_x Title of the X axis. +#' @param name_y Title of the Y axis. +#' @param gene_sets A named list of vectors of gene IDs to highlight. The names +#' will be used to distinguish the sets and in the legend. +#' @param use_ranks Show ranks instead of scores. +#' +#' @export +plot_rankings_correlation <- function(ranking_x, + ranking_y, + name_x, + name_y, + gene_sets = NULL, + use_ranks = TRUE) { + if (!requireNamespace("plotly", quietly = TRUE)) { + stop("Please install \"plotly\" to use this function.") + } + + data <- merge(ranking_x, ranking_y, by = "gene") + data <- merge(data, geposan::genes, by.x = "gene", by.y = "id") + + data[, `:=`( + x = if (use_ranks) rank.x else score.x, + y = if (use_ranks) rank.y else score.y + )] + + model <- stats::lm(y ~ x, data) + model_data <- data.table(x = seq(min(data$x), max(data$x), length = 100)) + model_data[, c("y", "lower", "upper") := data.table( + stats::predict(model, model_data, interval = "confidence") + )] + + # Take a random sample to actually plot. + sample_data <- data[!gene %chin% unlist(gene_sets)][sample(.N, 1000)] + + fig <- plotly::plot_ly() |> + plotly::add_markers( + data = sample_data, + x = ~x, + y = ~y, + name = "All genes", + marker = list( + color = base_color(), + size = 5 + ), + hoverinfo = "skip" + ) |> + plotly::add_lines( + data = model_data, + x = ~x, + y = ~y, + line = list(color = base_color()), + showlegend = FALSE, + hoverinfo = "skip" + ) |> + plotly::add_ribbons( + data = model_data, + x = ~x, + ymin = ~lower, + ymax = ~upper, + fillcolor = base_color_transparent(), + line = list(width = 0), + showlegend = FALSE, + hoverinfo = "skip" + ) + + gene_set_index <- 1 + for (gene_set_name in names(gene_sets)) { + gene_set <- gene_sets[[gene_set_name]] + + fig <- fig |> + plotly::add_markers( + data = data[gene %chin% gene_set], + x = ~x, + y = ~y, + name = gene_set_name, + text = ~name, + marker = list( + color = gene_set_color(gene_set_index), + size = 8 + ) + ) + + gene_set_index <- gene_set_index + 1 + } + + fig <- fig |> plotly::layout( + xaxis = list(title = name_x), + yaxis = list(title = name_y) + ) + + if (use_ranks) { + fig <- fig |> plotly::layout( + xaxis = list(autorange = "reversed"), + yaxis = list(autorange = "reversed") + ) + } + + fig +} + #' Plot a ranking as a scatter plot of scores. #' diff --git a/man/plot_rankings_correlation.Rd b/man/plot_rankings_correlation.Rd new file mode 100644 index 0000000..aa7e690 --- /dev/null +++ b/man/plot_rankings_correlation.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plots.R +\name{plot_rankings_correlation} +\alias{plot_rankings_correlation} +\title{Plot a scatter plot to compare two rankings.} +\usage{ +plot_rankings_correlation( + ranking_x, + ranking_y, + name_x, + name_y, + gene_sets = NULL, + use_ranks = TRUE +) +} +\arguments{ +\item{ranking_x}{The ranking to be shown on the X axis.} + +\item{ranking_y}{The ranking to be shown on the Y axis.} + +\item{name_x}{Title of the X axis.} + +\item{name_y}{Title of the Y axis.} + +\item{gene_sets}{A named list of vectors of gene IDs to highlight. The names +will be used to distinguish the sets and in the legend.} + +\item{use_ranks}{Show ranks instead of scores.} +} +\description{ +This function requires the package \code{plotly}. +}