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
3532plotEISA <- 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}
0 commit comments