Skip to content
Snippets Groups Projects
Commit bbaf3a7c authored by Simeon's avatar Simeon
Browse files

new functions

parent f111274c
Branches
No related tags found
No related merge requests found
#' 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)
}
#' 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)), ...)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment