plots: Improve hoverinfo in correlation plot

This commit is contained in:
Elias Projahn 2022-08-17 16:17:22 +02:00
parent 0440d5931c
commit c8b94fade8
2 changed files with 71 additions and 35 deletions

View file

@ -215,6 +215,7 @@ plot_rankings <- function(rankings, gene_sets) {
#' @param gene_sets A named list of vectors of gene IDs to highlight. The names #' @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. #' will be used to distinguish the sets and in the legend.
#' @param use_ranks Show ranks instead of scores. #' @param use_ranks Show ranks instead of scores.
#' @param use_sample Limit genes outside of the gene sets to a random sample.
#' #'
#' @export #' @export
plot_rankings_correlation <- function(ranking_x, plot_rankings_correlation <- function(ranking_x,
@ -222,7 +223,8 @@ plot_rankings_correlation <- function(ranking_x,
name_x, name_x,
name_y, name_y,
gene_sets = NULL, gene_sets = NULL,
use_ranks = TRUE) { use_ranks = TRUE,
use_sample = TRUE) {
if (!requireNamespace("plotly", quietly = TRUE)) { if (!requireNamespace("plotly", quietly = TRUE)) {
stop("Please install \"plotly\" to use this function.") stop("Please install \"plotly\" to use this function.")
} }
@ -241,12 +243,11 @@ plot_rankings_correlation <- function(ranking_x,
stats::predict(model, model_data, interval = "confidence") stats::predict(model, model_data, interval = "confidence")
)] )]
# Take a random sample to actually plot. fig <- plotly::plot_ly()
sample_data <- data[!gene %chin% unlist(gene_sets)][sample(.N, 1000)]
fig <- plotly::plot_ly() |> if (use_sample) {
plotly::add_markers( fig <- fig |> plotly::add_markers(
data = sample_data, data = data[!gene %chin% unlist(gene_sets)][sample(.N, 1000)],
x = ~x, x = ~x,
y = ~y, y = ~y,
name = "All genes", name = "All genes",
@ -255,7 +256,30 @@ plot_rankings_correlation <- function(ranking_x,
size = 5 size = 5
), ),
hoverinfo = "skip" hoverinfo = "skip"
) |> )
} else {
fig <- fig |> plotly::add_markers(
data = data,
x = ~x,
y = ~y,
name = "All genes",
marker = list(
color = base_color(),
size = 5
),
text = ~ glue::glue(
"<b>{name}</b>",
"<br>",
"{name_x}: {round(x, digits = 2)} ",
"({round(percentile.x * 100, digits = 2)}%)<br>",
"{name_y}: {round(y, digits = 2)} ",
"({round(percentile.y * 100, digits = 2)}%)"
),
hoverinfo = "text"
)
}
fig <- fig |>
plotly::add_lines( plotly::add_lines(
data = model_data, data = model_data,
x = ~x, x = ~x,
@ -276,6 +300,7 @@ plot_rankings_correlation <- function(ranking_x,
) )
gene_set_index <- 1 gene_set_index <- 1
for (gene_set_name in names(gene_sets)) { for (gene_set_name in names(gene_sets)) {
gene_set <- gene_sets[[gene_set_name]] gene_set <- gene_sets[[gene_set_name]]
@ -285,11 +310,19 @@ plot_rankings_correlation <- function(ranking_x,
x = ~x, x = ~x,
y = ~y, y = ~y,
name = gene_set_name, name = gene_set_name,
text = ~name,
marker = list( marker = list(
color = gene_set_color(gene_set_index), color = gene_set_color(gene_set_index),
size = 8 size = 8
) ),
text = ~ glue::glue(
"<b>{name}</b>",
"<br>",
"{name_x}: {round(x, digits = 2)} ",
"({round(percentile.x * 100, digits = 2)}%)<br>",
"{name_y}: {round(y, digits = 2)} ",
"({round(percentile.y * 100, digits = 2)}%)"
),
hoverinfo = "text"
) )
gene_set_index <- gene_set_index + 1 gene_set_index <- gene_set_index + 1

View file

@ -10,7 +10,8 @@ plot_rankings_correlation(
name_x, name_x,
name_y, name_y,
gene_sets = NULL, gene_sets = NULL,
use_ranks = TRUE use_ranks = TRUE,
use_sample = TRUE
) )
} }
\arguments{ \arguments{
@ -26,6 +27,8 @@ plot_rankings_correlation(
will be used to distinguish the sets and in the legend.} will be used to distinguish the sets and in the legend.}
\item{use_ranks}{Show ranks instead of scores.} \item{use_ranks}{Show ranks instead of scores.}
\item{use_sample}{Limit genes outside of the gene sets to a random sample.}
} }
\description{ \description{
This function requires the package \code{plotly}. This function requires the package \code{plotly}.