diff --git a/R/plot_cluster_thresholds.R b/R/plot_cluster_thresholds.R new file mode 100644 index 0000000000000000000000000000000000000000..81bf0587210b65b3ae288f8ace6c8a01f1c5a9eb --- /dev/null +++ b/R/plot_cluster_thresholds.R @@ -0,0 +1,51 @@ +#' Plot cluster counts across similarity thresholds +#' +#' This function creates a plot of cluster thresholds based on a given table of cluster counts +#' and a defined plateau. +#' +#' @param clus_counts_tbl A data frame containing the cluster counts. It should have two columns: +#' - threshold: The threshold values. +#' - cluster_number: The corresponding number of clusters. +#' +#' @param plateaus A named numeric vector containing the plateau information. It should have the following +#' elements: +#' - plateau_start: The starting threshold value of the plateau. +#' - plateau_end: The ending threshold value of the plateau. +#' - cluster_number: The number of clusters within the plateau. +#' +#' @return A plot of cluster thresholds with highlighted plateaus. +#' +#' @examples +#' clus_counts_tbl <- data.frame( +#' threshold = c(0.1, 0.2, 0.3, 0.4, 0.5), +#' cluster_number = c(7, 6, 6, 5, 4) +#' ) +#' +#' plateaus <- c( +#' plateau_start = 0.2, +#' plateau_end = 0.3, +#' cluster_number = 6 +#' ) +#' +#' plot_cluster_thresholds(clus_counts_tbl, plateaus) +#' +#' +#' @export + +plot_cluster_thresholds <- function(clus_counts_tbl, plateaus) { + ggplot(clus_counts_tbl, aes(x = threshold, y = cluster_number)) + + geom_rect(aes(xmin = plateaus['plateau_start'], + xmax = plateaus['plateau_end'], ymin = 0, + ymax = max(cluster_number)), fill = 'lightblue') + + geom_point() + + annotate('text', x = plateaus['plateau_start'], + y = max(clus_counts_tbl$cluster_number)/2, + label = paste('Clusters:', plateaus['cluster_number']), + hjust = 0) + + annotate('text', x = plateaus['plateau_start'], + y = max(clus_counts_tbl$cluster_number)/2.5, + label = paste( + 'Threshold:', plateaus['plateau_start'], + 'to', plateaus['plateau_end']), + hjust = 0) +} diff --git a/R/str_pad_to_max.R b/R/str_pad_to_max.R new file mode 100644 index 0000000000000000000000000000000000000000..c222201a2d5606c53af2f598071842beb0694ecf --- /dev/null +++ b/R/str_pad_to_max.R @@ -0,0 +1,18 @@ +#' Pad string to longest length +#' +#' Pads each element in a character vector to the length of the longest element in the vector. +#' +#' @param vec The character vector to be padded. +#' @param ... Additional arguments to be passed to `str_pad`. +#' +#' @return The character vector with each element padded to the maximum length. +#' +#' @examples +#' str_pad_to_max(c("hello", "world", "foo", "bar", "x")) +#' +#' @importFrom stringr str_pad str_length +#' +#' @export +str_pad_to_max <- function(vec = c(), ...){ + str_pad(vec, max(str_length(vec)), ...) +}