Skip to content

Commit 1966bae

Browse files
committed
Use ggplot2 for plotEISA(), add identity line and set aspect ratio to 1
1 parent 99cab33 commit 1966bae

5 files changed

Lines changed: 81 additions & 88 deletions

File tree

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ Suggests:
4141
withr
4242
VignetteBuilder: knitr
4343
Imports:
44-
graphics,
4544
stats,
4645
GenomicRanges,
4746
S4Vectors,
@@ -51,7 +50,9 @@ Imports:
5150
methods,
5251
SummarizedExperiment,
5352
BiocGenerics,
54-
utils
53+
utils,
54+
ggplot2,
55+
rlang
5556
URL: https://github.com/fmicompbio/eisaR
5657
BugReports: https://github.com/fmicompbio/eisaR/issues
5758
Config/roxygen2/version: 8.0.0

NAMESPACE

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ importFrom(S4Vectors,split)
2626
importFrom(SummarizedExperiment,SummarizedExperiment)
2727
importFrom(SummarizedExperiment,assay)
2828
importFrom(SummarizedExperiment,assayNames)
29-
importFrom(graphics,legend)
30-
importFrom(graphics,plot)
31-
importFrom(graphics,points)
29+
importFrom(ggplot2,aes)
30+
importFrom(ggplot2,coord_fixed)
31+
importFrom(ggplot2,element_blank)
32+
importFrom(ggplot2,geom_abline)
33+
importFrom(ggplot2,geom_point)
34+
importFrom(ggplot2,ggplot)
35+
importFrom(ggplot2,labs)
36+
importFrom(ggplot2,scale_color_manual)
37+
importFrom(ggplot2,theme)
38+
importFrom(ggplot2,theme_bw)
3239
importFrom(limma,nonEstimable)
3340
importFrom(methods,is)
41+
importFrom(rlang,.data)
3442
importFrom(stats,model.matrix)
3543
importFrom(stats,setNames)
3644
importFrom(utils,write.table)

R/plotEISA.R

Lines changed: 55 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,56 @@
11
#' @title Visualize the results from an exon-intron split analysis.
22
#'
33
#' @description \code{plotEISA} takes the return value from \code{\link{runEISA}}
4-
#' and generates a scatterplot of intronic versus exonic changes.
4+
#' and generates a scatterplot of intronic versus exonic changes.
55
#'
6-
#' @author Michael Stadler
6+
#' @author Michael Stadler, Charlotte Soneson
77
#'
8-
#' @param x \code{list} with EISA results, typically the return value from \code{\link{runEISA}}
8+
#' @param x \code{list} with EISA results, typically the return value from
9+
#' \code{\link{runEISA}}
910
#' @param contrast one of \code{"ExIn"} or \code{"none"}. If \code{"ExIn"}
10-
#' (the default), genes that significantly differ between exonic and intronic changes
11-
#' are highlighted. \code{"none"} turns off gene highlighting.
11+
#' (the default), genes that significantly differ between exonic and
12+
#' intronic changes are highlighted. \code{"none"} turns off gene
13+
#' highlighting.
1214
#' @param minLfc \code{NULL} or \code{numeric(1)} with the minimal absolute log2
13-
#' fold change to color a gene. If \code{NULL} (the default), no fold changes
14-
#' are not used to select genes for highlighting.
15+
#' fold change to color a gene. If \code{NULL} (the default), no fold changes
16+
#' are not used to select genes for highlighting.
1517
#' @param maxFDR \code{numeric(1)} with maximal false discovery rate for gene
16-
#' highlighting.
18+
#' highlighting.
1719
#' @param genecolors Vector of length three specifying the colors to use for
18-
#' genes that are significantly up, down or unchanged.
19-
#' @param ... further arguments past to \code{plot()}. Parameters that will be set
20-
#' automatically unless given in the arguments are:\describe{
21-
#' \item{pch}{: plot symbol (default: \code{"."})}
22-
#' \item{cex}{: plot symbol expansion factor (default: \code{2})}
23-
#' \item{col}{: plot symbol color (default: according to \code{contrast} and \code{genecolors})}
24-
#' \item{xlab/ylab}{: axis labels}
25-
#' }
20+
#' genes that are significantly up, down or unchanged.
2621
#'
27-
#' @return `NULL` (invisibly)
22+
#' @return A \code{ggplot} object.
2823
#'
2924
#' @examples
3025
#' # see the help for runEISA() for a full example
3126
#'
32-
#' @importFrom graphics plot legend points
27+
#' @importFrom ggplot2 ggplot aes geom_abline geom_point coord_fixed
28+
#' @importFrom ggplot2 scale_color_manual labs theme_bw theme element_blank
29+
#' @importFrom rlang .data
3330
#'
3431
#' @export
3532
plotEISA <- function(x, contrast = c("ExIn", "none"),
3633
minLfc = NULL, maxFDR = 0.05,
37-
genecolors = c("#E41A1C", "#497AB3", "#222222"), ...) {
34+
genecolors = c("#E41A1C", "#497AB3", "#222222")) {
3835
# check arguments
3936
contrast <- match.arg(contrast)
4037
contrastName <- ifelse("contrastName" %in% names(x),
4138
paste0(" (", x$contrastName, ")"), "")
4239
sigtab <- switch(contrast, ExIn = x$tab.ExIn, none = data.frame())
43-
if (nrow(sigtab) == 0L && contrast != "none")
44-
stop("'x' does not contain the requested statistics and can only ",
45-
"be plotted using contrast = 'none'. Note that at least two ",
46-
"replicates per condition are required to run the statistical ",
47-
"testing.")
48-
if (is.null(minLfc))
40+
if (contrast != "none") {
41+
if (nrow(sigtab) == 0L) {
42+
stop("'x' does not contain the requested statistics and can only ",
43+
"be plotted using contrast = 'none'. Note that at least two ",
44+
"replicates per condition are required to run the statistical ",
45+
"testing.")
46+
}
47+
if (nrow(sigtab) != nrow(x$contrasts)) {
48+
stop("x$tab.ExIn and x$contrasts have different numbers of rows.")
49+
}
50+
}
51+
if (is.null(minLfc)) {
4952
minLfc <- 0.0
53+
}
5054
stopifnot(exprs = {
5155
is.numeric(minLfc)
5256
length(minLfc) == 1L
@@ -58,47 +62,32 @@ plotEISA <- function(x, contrast = c("ExIn", "none"),
5862
length(genecolors) == 3L
5963
})
6064

61-
# identify gene to highlight
62-
if (contrast == "none") {
63-
sig <- rep(FALSE, nrow(x$contrasts))
64-
sigDir <- 0.0
65-
} else {
66-
sig <- abs(sigtab$logFC) >= minLfc & sigtab$FDR <= maxFDR
67-
sigDir <- sign(sigtab$logFC[sig])
68-
message("identified ", sum(sig), " genes to highlight")
69-
}
70-
71-
# set graphical parameters (user-defined colors take precedence)
72-
dotsL <- list(...)
73-
dotsL$x <- x$contrasts[, "Din"]
74-
dotsL$y <- x$contrasts[, "Dex"]
75-
if (!"pch" %in% names(dotsL))
76-
dotsL$pch <- "."
77-
if (!"cex" %in% names(dotsL))
78-
dotsL$cex <- 2L
79-
if (!"col" %in% names(dotsL))
80-
dotsL$col <- ifelse(sig,
81-
ifelse(sigtab$logFC > 0.0,
82-
genecolors[1L], genecolors[2L]),
83-
genecolors[3L])
84-
if (!"xlab" %in% names(dotsL))
85-
dotsL$xlab <- substitute(expression(paste(Delta, "intron", cn)),
86-
list(cn = contrastName))
87-
if (!"ylab" %in% names(dotsL))
88-
dotsL$ylab <- substitute(expression(paste(Delta, "exon", cn)),
89-
list(cn = contrastName))
90-
91-
# Delta I vs. Delta E
92-
do.call(plot, dotsL)
65+
# identify genes to highlight
66+
plotdf <- as.data.frame(x$contrasts)
67+
plotdf$sig <- ""
9368
if (contrast != "none") {
94-
if (length(dotsL$col) == 1L)
95-
dotsL$col <- rep(dotsL$col, length(dotsL$x))
96-
points(dotsL$x[sig], dotsL$y[sig], pch = 20L, col = dotsL$col[sig])
97-
legend(x = "bottomright", bty = "n", pch = 20L, col = genecolors[c(1L, 2L)],
98-
legend = sprintf("%s (%d)",
99-
c("Up", "Down"),
100-
c(sum(sigDir == 1.0), sum(sigDir == -1.0))))
69+
plotdf$sig <- ifelse(
70+
sigtab$logFC >= minLfc & sigtab$FDR <= maxFDR,
71+
paste0("Up (", sum(sigtab$logFC >= minLfc & sigtab$FDR <= maxFDR), ")"),
72+
ifelse(sigtab$logFC <= -minLfc & sigtab$FDR <= maxFDR,
73+
paste0("Down (", sum(sigtab$logFC <= -minLfc & sigtab$FDR <= maxFDR), ")"),
74+
""))
75+
plotdf$sig <- factor(plotdf$sig, levels = rev(sort(unique(plotdf$sig))))
76+
message("identified ", sum(plotdf$sig != ""), " genes to highlight")
10177
}
102-
103-
return(invisible(NULL))
78+
79+
ggplot(plotdf, aes(x = .data$Din, y = .data$Dex)) +
80+
geom_abline(slope = 1, intercept = 0, linetype = "dashed",
81+
color = "grey") +
82+
geom_point(size = 0.1, color = genecolors[3L]) +
83+
geom_point(data = plotdf[plotdf$sig != "", ], aes(color = .data$sig)) +
84+
coord_fixed() +
85+
scale_color_manual(values = genecolors[seq_len(2)], name = "") +
86+
labs(x = substitute(Delta * "intron" ~ x, list(x = contrastName)),
87+
y = substitute(Delta * "exon" ~ x, list(x = contrastName))) +
88+
theme_bw(base_size = 14) +
89+
theme(panel.grid = element_blank(),
90+
legend.position = "inside",
91+
legend.position.inside = c(0.97, 0.03),
92+
legend.justification = c(1, 0))
10493
}

man/plotEISA.Rd

Lines changed: 8 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_plotEISA.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ test_that("plotEISA() runs", {
1313
tf <- tempfile(fileext = ".pdf")
1414
pdf(file = tf)
1515

16-
expect_null(plotEISA(res1, col = "purple"))
17-
expect_null(plotEISA(res1, contrast = "none"))
16+
expect_true(ggplot2::is_ggplot(
17+
plotEISA(res1, genecolors = c("orange", "yellow", "purple"))))
18+
expect_true(ggplot2::is_ggplot(
19+
plotEISA(res1, contrast = "none")))
1820
expect_error(plotEISA(res2))
1921

2022
dev.off()

0 commit comments

Comments
 (0)