Skip to content

Commit

Permalink
updated plotOutliers to include conditional formating for outliers = …
Browse files Browse the repository at this point in the history
…NULL
  • Loading branch information
Michael Totty authored and Michael Totty committed Mar 21, 2024
1 parent c5e5d4f commit 255081c
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions R/plotOutliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' in the plot. This metric should be a column name in `colData(spe)`.
#' @param outliers A character string specifying the column name in `colData(spe)`
#' that indicates whether a data point is considered an outlier. Default is
#' "local_outliers".
#' NULL.
#' @param colors A character vector specifying the colors to be used for the
#' gradient scale. If length is 2, the gradient will be a single color gradient.
#' @param stroke A numeric value specifying the border thickness for outlier
Expand All @@ -33,33 +33,51 @@

plotOutliers <- function(spe, sample_id = "sample_id",
sample=unique(spe$sample_id)[1], metric="detected",
outliers="local_outliers", point_size=1,
outliers=NULL, point_size=1,
colors=c("white","black"), stroke=1) {


# Subset the data to the specified sample
spe.subset <- spe[ ,colData(spe)[[sample_id]] == sample]

# Start building the plot
p <- make_escheR(spe.subset) |>
add_fill(var = metric, point_size = point_size) |>
add_ground(var = outliers, stroke = stroke) +
ggtitle(paste0("Sample: ", sample))
add_fill(var = metric, point_size = point_size)

# remove and replace scales (to avoid warnings for re-coloring)
# Conditionally add outliers if they are not NULL
if (!is.null(outliers)) {
p <- p |> add_ground(var = outliers, stroke = stroke)
}

# Add title to the plot
p <- p + ggtitle(paste0("Sample: ", sample))

# Remove and replace scales to avoid warnings for re-coloring
p$scales$scales <- list()

# Conditionally add color scales based on the length of 'colors'
if (length(colors) == 2) {
p <- p + scale_fill_gradient(low=colors[1], high=colors[2]) +
scale_color_manual(
p <- p + scale_fill_gradient(low = colors[1], high = colors[2]) +
scale_y_reverse()

# If outliers are not NULL, add a manual color scale for them
if (!is.null(outliers)) {
p <- p + scale_color_manual(
name = "", # turn off legend name for ground
values = c("TRUE" = "red", "FALSE" = "transparent")
) +
scale_y_reverse()
)
}
} else if (length(colors) > 2) {
p <- p + scale_fill_gradient2(low=colors[1], mid=colors[2], high=colors[3]) +
scale_color_manual(
p <- p + scale_fill_gradient2(low = colors[1], mid = colors[2], high = colors[3]) +
scale_y_reverse()

# If outliers are not NULL, add a manual color scale for them
if (!is.null(outliers)) {
p <- p + scale_color_manual(
name = "", # turn off legend name for ground
values = c("TRUE" = "red", "FALSE" = "transparent")
) +
scale_y_reverse()
)
}
}

return(p)
Expand Down

0 comments on commit 255081c

Please sign in to comment.