plots: Add chromosome plot

This commit is contained in:
Elias Projahn 2021-11-22 14:10:08 +01:00
parent 1058dfaa17
commit 2f3dc80a2f
2 changed files with 52 additions and 0 deletions

View file

@ -6,6 +6,7 @@ export(analyze)
export(compare)
export(optimal_weights)
export(plot_boxplot)
export(plot_chromosomes)
export(plot_positions)
export(plot_scores)
export(preset)

View file

@ -188,3 +188,54 @@ plot_boxplot <- function(ranking, gene_sets = NULL, labels = NULL) {
type = "box"
)
}
#' Show the distribution of scores across chromosomes.
#'
#' This function requires the package `plotly`.
#'
#' @param ranking The ranking to visualize.
#'
#' @seealso ranking()
#'
#' @export
plot_chromosomes <- function(ranking) {
if (!requireNamespace("plotly", quietly = TRUE)) {
stop("Please install \"plotly\" to use this function.")
}
data <- merge(ranking, geposan::genes, by.x = "gene", by.y = "id")
data <- data[, .(score = mean(score)), by = "chromosome"]
# Get an orderable integer from a chromosome name.
chromosome_index <- function(chromosome) {
index <- suppressWarnings(as.integer(chromosome))
ifelse(
!is.na(index),
index,
ifelse(
chromosome == "X",
998,
999
)
)
}
data[, index := chromosome_index(chromosome)]
setorder(data, "index")
plotly::plot_ly(
data = data,
x = ~chromosome,
y = ~score,
type = "bar"
) |>
plotly::layout(
xaxis = list(
title = "Chromosome",
categoryorder = "array",
categoryarray = ~chromosome
),
yaxis = list(title = "Mean score")
)
}