Skip to content

Commit 7446eae

Browse files
committed
adding updated reversecumulatives plot
1 parent f7aaf1d commit 7446eae

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

R/ExportMethods.R

+30-26
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ setGeneric( "plotReverseCumulatives",
8383
.plotReverseCumulatives <-
8484
function( object, values = c("raw", "normalized")
8585
, fitInRange = c(10, 1000)
86-
, group = NULL) {
86+
, group = NULL
87+
, xlim = c(1, 1e5)
88+
, ylim = c(1, 1e6)) {
8789
if (is.null(object@metadata$colData))
8890
stop("Expects a List-like object with a colData DataFrame in its metadata slot.")
8991

@@ -101,11 +103,6 @@ setGeneric( "plotReverseCumulatives",
101103
group <- "group"
102104
}
103105

104-
# no need for colors if a multi-facet plot and many colors
105-
if (is.null(object@metadata$colData$Colors)) {
106-
object@metadata$colData$Colors <- scales::hue_pal()(length(object))
107-
}
108-
109106
toCumSums <- function(exprValues, sampleName) {
110107
# See benchmarks/sorted-abundance-benchmark.Rmd in the CAGEr's Git repository
111108
exprValues <- exprValues[exprValues != 0]
@@ -123,14 +120,9 @@ setGeneric( "plotReverseCumulatives",
123120
# Enforce original sample order
124121
DF$plotOrder <- DF$sampleLabels |> ordered(unique(DF$sampleLabels))
125122

126-
p <- as.data.frame(DF) |>
123+
intermediate_df <- as.data.frame(DF) |>
127124
merge( object@metadata$colData[,c('sampleLabels', group)] |> as.data.frame()
128-
, by="sampleLabels") |> ggplot() +
129-
aes(.data$x, .data$y, col = .data$plotOrder) +
130-
geom_step() +
131-
scale_x_log10() + scale_y_log10() +
132-
xlab(xlab) + ylab(ylab) +
133-
ggtitle("Reverse-cumulative plot")
125+
, by="sampleLabels")
134126

135127
if(!is.null(fitInRange)) {
136128
fit.coefs.m <- sapply(object, val.range = fitInRange, \(x, val.range)
@@ -139,20 +131,32 @@ setGeneric( "plotReverseCumulatives",
139131
reference.slope <- min(median(fit.slopes), -1.05)
140132
reference.library.size <- 10^floor(log10(median(sapply(object, sum))))
141133
reference.intercept <- log10(reference.library.size/VGAM::zeta(-1*reference.slope)) # intercept on log10 scale used for plotting with abline
142-
p <- p +
143-
geom_abline(intercept = reference.intercept, slope = reference.slope, color = "grey", linetype = "longdash") +
144-
geom_vline(xintercept = fitInRange, color = "darkgrey", linetype = "dotted") +
145-
scale_color_manual(
146-
values = object@metadata$colData$Colors,
147-
labels = paste0("(", formatC(-fit.slopes, format = "f", digits = 2), ") ", names(fit.slopes))) +
148-
labs(subtitle = paste0("Ref. distribution alpha = ", sprintf("%.2f", -reference.slope), ", T = ", reference.library.size, ".")) +
149-
guides(col = guide_legend(title = "(alpha) sample names"))
150-
} else {
151-
p <- p +
152-
scale_color_manual(values = object@metadata$colData$Colors)
153-
guides(col = guide_legend(title = "Sample names"))
154134
}
155-
p + facet_wrap(group)
135+
136+
plot_out <- ggplot2::ggplot(intermediate_df) +
137+
ggplot2::aes(x=x, y=y) +
138+
ggplot2::geom_line() +
139+
ggplot2::facet_wrap(. ~sampleLabels) +
140+
xlim(xlim[1], xlim[2]) +
141+
ylim(ylim[1], ylim[2]) +
142+
scale_x_continuous(trans='log10') +
143+
scale_y_continuous(trans='log10') +
144+
labs(title="Reference distribution:",
145+
subtitle = paste0("alpha= ", sprintf("%.2f", -1*reference.slope), " T= ", reference.library.size),
146+
x =xlab, y = ylab) +
147+
ggplot2::geom_text(data = intermediate_df,
148+
mapping= aes(
149+
x=10, y= 10,
150+
label = paste0("alpha= ", formatC(-1*fit.slopes[sampleLabels], format = "f", digits = 2)))) +
151+
ggplot2::geom_vline(xintercept=fitInRange, linetype="dotted") +
152+
ggplot2::geom_abline(slope = reference.slope, intercept = reference.intercept,
153+
linetype="longdash", colour="#7F7F7F7F")
154+
155+
if (TOPLOT){
156+
print(plot_out)
157+
}
158+
159+
return(list(plot_out, reference.slope, reference.library.size, reference.intercept))
156160
}
157161

158162
#' @rdname plotReverseCumulatives

0 commit comments

Comments
 (0)