Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update plots #122

Open
wants to merge 6 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ importFrom(ggplot2,geom_point)
importFrom(ggplot2,geom_segment)
importFrom(ggplot2,geom_step)
importFrom(ggplot2,geom_vline)
importFrom(ggplot2,geom_line)
importFrom(ggplot2,geom_text)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,guide_legend)
Expand Down
24 changes: 12 additions & 12 deletions R/CorrelationMethods.R
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,10 @@ pairs.DataFrame <- function (x, labels, panel = points, ..., horInd = 1:nc, verI
, tagCountThreshold, applyThresholdBoth
, digits) {
# Select samples
if (all(samples %in% colnames(expr.table))) {
expr.table <- expr.table[,samples]
} else if(samples == "all"){
if(samples == "all"){
samples <- colnames(expr.table)
} else if (all(samples %in% colnames(expr.table))) {
expr.table <- expr.table[,samples]
} else stop("'samples' parameter must be either \"all\" or a character vector of valid sample labels!")
nr.samples <- length(samples)

Expand Down Expand Up @@ -541,15 +541,15 @@ pairs.DataFrame <- function (x, labels, panel = points, ..., horInd = 1:nc, verI
}

pairs( expr.table
, lower.panel = pointsUnique
, upper.panel = panel.cor
, pch = "."
, cex = 4
, log = "xy"
, las = 1
, xaxp = c(1,10,1)
, yaxp = c(1,10,1)
, labels = samples)
, lower.panel = pointsUnique
, upper.panel = panel.cor
, pch = "."
, cex = 4
, log = "xy"
, las = 1
, xaxp = c(1,10,1)
, yaxp = c(1,10,1)
, labels = samples)

# Return a correlation matrix
corr.m <- matrix(1, nr.samples, nr.samples)
Expand Down
73 changes: 35 additions & 38 deletions R/ExportMethods.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ NULL
#' ggplot2::ggtitle("prim6 replicates")
#' tagClustersGR(exampleCAGEexp) |> plotReverseCumulatives()
#'
#' @importFrom ggplot2 aes geom_abline geom_step geom_vline ggplot ggtitle
#' @importFrom ggplot2 guides guide_legend labs
#' @importFrom ggplot2 scale_color_manual scale_x_log10 scale_y_log10
#' @importFrom ggplot2 aes geom_line geom_abline facet_wrap geom_text geom_vline ggplot scale_x_log10 scale_y_log10
#' @importFrom rlang .data
#' @importFrom scales hue_pal
#' @importFrom stats cor median
Expand Down Expand Up @@ -101,10 +99,6 @@ setGeneric( "plotReverseCumulatives",
group <- "group"
}

if (is.null(object@metadata$colData$Colors)) {
object@metadata$colData$Colors <- scales::hue_pal()(length(object))
}

toCumSums <- function(exprValues, sampleName) {
# See benchmarks/sorted-abundance-benchmark.Rmd in the CAGEr's Git repository
exprValues <- exprValues[exprValues != 0]
Expand All @@ -122,14 +116,9 @@ setGeneric( "plotReverseCumulatives",
# Enforce original sample order
DF$plotOrder <- DF$sampleLabels |> ordered(unique(DF$sampleLabels))

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

if(!is.null(fitInRange)) {
fit.coefs.m <- sapply(object, val.range = fitInRange, \(x, val.range)
Expand All @@ -138,20 +127,28 @@ setGeneric( "plotReverseCumulatives",
reference.slope <- min(median(fit.slopes), -1.05)
reference.library.size <- 10^floor(log10(median(sapply(object, sum))))
reference.intercept <- log10(reference.library.size/VGAM::zeta(-1*reference.slope)) # intercept on log10 scale used for plotting with abline
p <- p +
geom_abline(intercept = reference.intercept, slope = reference.slope, color = "grey", linetype = "longdash") +
geom_vline(xintercept = fitInRange, color = "darkgrey", linetype = "dotted") +
scale_color_manual(
values = object@metadata$colData$Colors,
labels = paste0("(", formatC(-fit.slopes, format = "f", digits = 2), ") ", names(fit.slopes))) +
labs(subtitle = paste0("Ref. distribution alpha = ", sprintf("%.2f", -reference.slope), ", T = ", reference.library.size, ".")) +
guides(col = guide_legend(title = "(alpha) sample names"))
} else {
p <- p +
scale_color_manual(values = object@metadata$colData$Colors)
guides(col = guide_legend(title = "Sample names"))

ggplot(intermediate_df) +
aes(x=x, y=y) +
geom_line() +
facet_wrap(. ~sampleLabels) +
scale_x_log10() +
scale_y_log10() +
labs(title="Reference distribution:",
subtitle = paste0("alpha= ", sprintf("%.2f", -1*reference.slope), " T= ", reference.library.size),
x =xlab, y = ylab) +
geom_text(data = intermediate_df,
mapping= aes(
x=10, y= 10,
label = paste0("alpha= ", formatC(-1*fit.slopes[sampleLabels], format = "f", digits = 2)))) +
geom_vline(xintercept=fitInRange, linetype="dotted") +
geom_abline(slope = reference.slope, intercept = reference.intercept,
linetype="longdash", colour="#7F7F7F7F")
} else{
# ToDo: what is the intended behaviour?
warning("reference distribution not fitted")
}
p + facet_wrap(group)

}

#' @rdname plotReverseCumulatives
Expand Down Expand Up @@ -230,8 +227,8 @@ setMethod("plotReverseCumulatives", "GRanges",
#' @family CAGEr plot functions
#' @family CAGEr clusters functions
#'
#' @importFrom ggplot2 ggplot scale_fill_manual geom_histogram facet_wrap
#' @importFrom ggplot2 ggtitle xlab ylab labs
#' @importFrom ggplot2 ggplot aes_string scale_fill_manual geom_histogram facet_wrap
#' @importFrom ggplot2 ggtitle xlab ylab
#'
#' @examples
#'
Expand Down Expand Up @@ -278,17 +275,17 @@ setMethod( "plotInterquantileWidth", "CAGEexp"

binsize <- round(max(iqwidths$iq_width)/2)

ggplot2::ggplot(iqwidths) +
ggplot2::aes_string(x = "iq_width", fill = "sampleName") +
ggplot2::scale_fill_manual(values = names(sampleLabels(object))) +
ggplot2::geom_histogram(bins = binsize) +
ggplot2::facet_wrap("~sampleName") +
ggplot2::ggtitle(paste0(
ggplot(iqwidths) +
aes_string(x = "iq_width") +
scale_fill_manual(values = names(sampleLabels(object))) +
geom_histogram(bins = binsize) +
facet_wrap("~sampleName") +
ggtitle(paste0(
switch(clusters, tagClusters = "Tag Clusters", consensusClusters = "Consenss Clusters"),
" interquantile width (quantile ", qLow, " to ", qUp, ")")) +
ggplot2::xlab("Interquantile width (bp)") +
ggplot2::ylab("Frequency") +
ggplot2::labs(fill = "Sample name")
xlab("Interquantile width (bp)") +
ylab("Frequency")

})

#' @name plotExpressionProfiles
Expand Down
Loading