diff --git a/NAMESPACE b/NAMESPACE index e729dad..c7d7c77 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(analyze) export(compare) export(optimal_weights) export(plot_boxplot) +export(plot_positions) export(plot_scores) export(preset) export(ranking) diff --git a/R/plots.R b/R/plots.R index 9b2ee0d..d2b055b 100644 --- a/R/plots.R +++ b/R/plots.R @@ -1,3 +1,68 @@ +#' Plot gene positions. +#' +#' This function requires the package `plotly`. +#' +#' @param species_ids IDs of species to show in the plot. +#' @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. +#' @param use_positions Whether to display positions instead of distances. +#' +#' @export +plot_positions <- function(species_ids, + gene_sets, + labels, + use_positions = FALSE) { + if (!requireNamespace("plotly", quietly = TRUE)) { + stop("Please install \"plotly\" to use this function.") + } + + data <- merge( + geposan::distances[gene %chin% unlist(gene_sets) & + species %chin% species_ids], + geposan::genes[, .(id, name)], + by.x = "gene", by.y = "id" + ) + + if (use_positions) { + data[, value := position] + } else { + data[, value := distance] + } + + # Add labels for each gene set. + for (i in seq_along(gene_sets)) { + data[gene %chin% gene_sets[[i]], label := labels[i]] + } + + species <- geposan::species[id %chin% species_ids] + + yaxis_title <- if (use_positions) { + "Position [Bp]" + } else { + "Distance to telomeres [Bp]" + } + + plotly::plot_ly( + data = data, + x = ~species, + y = ~value, + color = ~label, + text = ~name, + type = "scatter", + mode = "markers" + ) |> plotly::layout( + xaxis = list( + title = "Species", + tickvals = species$id, + ticktext = species$name + ), + yaxis = list(title = yaxis_title) + ) +} + + #' Plot a ranking as a scatter plot of scores. #' #' This function requires the package `plotly`. diff --git a/man/plot_positions.Rd b/man/plot_positions.Rd new file mode 100644 index 0000000..b2847c6 --- /dev/null +++ b/man/plot_positions.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plots.R +\name{plot_positions} +\alias{plot_positions} +\title{Plot gene positions.} +\usage{ +plot_positions(species_ids, gene_sets, labels, use_positions = FALSE) +} +\arguments{ +\item{species_ids}{IDs of species to show in the plot.} + +\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.} + +\item{use_positions}{Whether to display positions instead of distances.} +} +\description{ +This function requires the package \code{plotly}. +}