ubigen/R/plots.R

124 lines
3.1 KiB
R
Raw Normal View History

2022-05-30 20:08:54 +02:00
#' Create a plot showing an overview over the provided ranking.
#'
#' @param ranked_data The ranking to visualize.
2022-05-30 21:27:18 +02:00
#' @param highlighted_genes Genes that will be marked.
2022-05-30 20:08:54 +02:00
#' @param sample_proportion Proportion of rows to use as the shown sample.
#'
#' @return A `plotly` figure.
#' @noRd
2022-05-30 21:27:18 +02:00
overview_plot <- function(ranked_data,
highlighted_genes = NULL,
sample_proportion = 0.05) {
figure <- plotly::plot_ly() |>
2022-05-30 20:08:54 +02:00
plotly::add_lines(
data = ranked_data[sample(
nrow(ranked_data),
sample_proportion * nrow(ranked_data)
)],
x = ~rank,
2022-05-30 21:27:18 +02:00
y = ~score,
hoverinfo = "skip"
2022-05-30 20:08:54 +02:00
) |>
plotly::layout(
xaxis = list(title = "Ranks"),
yaxis = list(title = "Score")
)
2022-05-30 21:27:18 +02:00
2022-05-30 21:59:40 +02:00
if (length(highlighted_genes) > 0) {
2022-05-30 21:27:18 +02:00
figure <- figure |>
plotly::add_markers(
data = ranked_data[gene %chin% highlighted_genes],
x = ~rank,
y = ~score,
text = ~ glue::glue(
"<b>{hgnc_name}</b><br>",
"Score: {round(score, digits = 2)}<br>",
"Rank: {rank}<br>",
"Percentile: {round(percentile * 100, digits = 2)}%"
),
hoverinfo = "text",
showlegend = FALSE
)
}
figure
2022-05-30 20:08:54 +02:00
}
2022-05-30 21:59:40 +02:00
#' Create a plot comparing some genes with the overall ranking.
#'
#' @param ranked_data The ranking to visualize.
#' @param highlighted_genes Genes that will be compared.
#'
#' @return A `plotly` figure.
#' @noRd
box_plot <- function(ranked_data, highlighted_genes) {
data <- data.table::copy(ranked_data)
data[, group := data.table::fifelse(
gene %chin% highlighted_genes,
"Your genes",
"Other genes"
)]
plotly::plot_ly() |>
plotly::add_boxplot(
data = data,
x = ~score,
y = ~group,
boxpoints = FALSE
) |> plotly::layout(
xaxis = list(title = "Score"),
yaxis = list(title = "")
)
}
2022-05-30 20:08:54 +02:00
#' Create plot showing the distribution of scores using `plotly`.
#'
#' @param ranked_data Data on genes with precomputed ranks.
2022-05-30 21:27:18 +02:00
#' @param highlighted_genes Genes that will be marked.
2022-05-30 20:08:54 +02:00
#' @param ranks How may ranks the x-axis should include. If this parameter is
#' `NULL`, all ranks will be shown.
#'
#' @return A `plotly` figure for rendering.
#' @noRd
2022-05-30 21:27:18 +02:00
scores_plot <- function(ranked_data, highlighted_genes = NULL, ranks = 1000) {
2022-05-30 20:08:54 +02:00
data <- if (is.null(ranks)) {
ranked_data
} else {
ranked_data[1:ranks]
}
ranks_label <- if (is.null(ranks)) {
"Ranks"
} else {
glue::glue("Ranks (1 to {ranks})")
}
2022-05-30 21:27:18 +02:00
data[, group := data.table::fifelse(
gene %chin% highlighted_genes,
"Your genes",
"All genes"
)]
2022-05-30 20:08:54 +02:00
plotly::plot_ly() |>
plotly::add_markers(
data = data,
x = ~rank,
y = ~score,
2022-05-30 21:27:18 +02:00
name = ~group,
text = ~ glue::glue(
"<b>{hgnc_name}</b><br>",
"Score: {round(score, digits = 2)}<br>",
"Rank: {rank}<br>",
"Percentile: {round(percentile * 100, digits = 2)}%"
),
hoverinfo = "text",
showlegend = FALSE
2022-05-30 20:08:54 +02:00
) |>
plotly::layout(
xaxis = list(title = ranks_label),
yaxis = list(title = "Score"),
clickmode = "event+select",
dragmode = "select"
)
}