Skip to content

Commit

Permalink
add divergent color plotting to plotOutliers. Replaced plotting code …
Browse files Browse the repository at this point in the history
…in plotOutliersPDF with plotOutliers function.
  • Loading branch information
Michael Totty authored and Michael Totty committed Mar 13, 2024
1 parent 29f7a67 commit 72865d2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
37 changes: 24 additions & 13 deletions R/plotOutliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
#' @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".
#' @param low_color A character string indicating the color to be used for the low end
#' of the gradient scale. Default is "white".
#' @param high_color A character string indicating the color to be used for the high end
#' of the gradient scale. Default is "black".
#' @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
#' points. Default is 1.
#'
Expand All @@ -34,20 +32,33 @@
plotOutliers <- function(spe, sample_id = "sample_id",
sample=unique(spe$sample_id)[1], metric="detected",
outliers="local_outliers",
low_color="white", high_color="black", stroke=1) {
colors=c("white","black"), stroke=1) {


spe.subset <- spe[ ,colData(spe)[[sample_id]] == sample]

make_escheR(spe.subset) |>
p <- make_escheR(spe.subset) |>
add_fill(var = metric) |>
add_ground(var = outliers, stroke = stroke) +
scale_color_manual(
name = "", # turn off legend name for ground_truth
values = c(
"TRUE" = "red2",
"FALSE" = "transparent")
) +
scale_fill_gradient(low =low_color,high = high_color)
ggtitle(paste0("Sample: ", sample))

# remove and replace scales (to avoid warnings for re-coloring)
p$scales$scales <- list()
if (length(colors) == 2) {
p <- p + scale_fill_gradient(low=colors[1], high=colors[2]) +
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(
name = "", # turn off legend name for ground
values = c("TRUE" = "red", "FALSE" = "transparent")
) +
scale_y_reverse()
}

print(p)
}
36 changes: 12 additions & 24 deletions R/plotOutliersPDF.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
#' @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".
#' @param low_color A character string indicating the color to be used for the low end
#' of the gradient scale. Default is "white".
#' @param high_color A character string indicating the color to be used for the high end
#' of the gradient scale. Default is "black".
#' @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
#' points. Default is 1.
#' @param width A numeric value indicating the width of the plot. Default
Expand All @@ -33,9 +31,8 @@
#'
#' @export
plotOutliersPDF <- function(spe, sample_id="sample_id", metric="detected",
outliers="local_outliers",low_color="white",
high_color="black", stroke=1, width=5, height=5,
fname
outliers="local_outliers",colors=c("white", "black"),
stroke=1, width=5, height=5,fname
) {

# Get a list of unique sample IDs
Expand All @@ -45,23 +42,14 @@ plotOutliersPDF <- function(spe, sample_id="sample_id", metric="detected",
pdf(width=width, height=height, fname)
for (sample in unique_sample_ids) {

# Subset the data for the current sample
spe_subset <- spe[ ,colData(spe)[[sample_id]] == sample]


p <- make_escheR(spe_subset) |>
add_fill(var = metric) |>
add_ground(var = outliers, stroke = stroke) +
ggtitle(paste0("Sample: ", sample))

# remove and replace scales (to avoid warnings for re-coloring)
p$scales$scales <- list()
p <- p + scale_fill_gradient(low=low_color, high=high_color) +
scale_color_manual(
name = "", # turn off legend name for ground
values = c("TRUE" = "red", "FALSE" = "transparent")
) +
scale_y_reverse()
p <- plotOutliers(spe,
sample_id=sample_id,
sample=sample,
metric=metric,
outliers=outliers,
colors=colors,
stroke=stroke
)

# print
print(p)
Expand Down

0 comments on commit 72865d2

Please sign in to comment.