3232# ' opacity of points in the plot.
3333# ' @param colorByCorrelation Logical scalar, indicating whether the correlation
3434# ' panels should be colored according to the correlation value.
35+ # ' @param corrColorRange Numeric vector of length 2, providing the lower and
36+ # ' upper limits of the color scale when coloring by correlation. Both
37+ # ' values should be positive; the same range is used for negative
38+ # ' correlations. If \code{NULL} (the default), the range is inferred from
39+ # ' the data.
40+ # ' @param addIdentityLine Logical scalar, indicating whether the identity line
41+ # ' should be added (only used if \code{pointsType = "points"}).
3542# '
3643# ' @importFrom GGally eval_data_col ggpairs wrap
3744# ' @importFrom ggplot2 ggplot annotate theme_void ylim stat_density2d
3845# ' scale_fill_continuous geom_point theme_bw theme element_blank aes
39- # ' geom_histogram scale_x_continuous scale_y_continuous
46+ # ' geom_histogram scale_x_continuous scale_y_continuous geom_abline
4047# ' @importFrom stats cor
4148# ' @importFrom SummarizedExperiment assayNames assay
4249# ' @importFrom grDevices hcl.colors rgb colorRamp
@@ -46,7 +53,8 @@ plotPairs <- function(se, selAssay = "counts", doLog = TRUE, pseudocount = 1,
4653 corMethod = " pearson" , histBreaks = 40 ,
4754 pointsType = " points" , corSizeMult = 5 ,
4855 corSizeAdd = 2 , pointSize = 0.1 , pointAlpha = 0.3 ,
49- colorByCorrelation = TRUE ) {
56+ colorByCorrelation = TRUE , corrColorRange = NULL ,
57+ addIdentityLine = FALSE ) {
5058
5159 .assertVector(x = se , type = " SummarizedExperiment" )
5260 .assertScalar(x = selAssay , type = " character" ,
@@ -63,6 +71,12 @@ plotPairs <- function(se, selAssay = "counts", doLog = TRUE, pseudocount = 1,
6371 .assertScalar(x = pointSize , type = " numeric" , rngExcl = c(0 , Inf ))
6472 .assertScalar(x = pointAlpha , type = " numeric" , rngExcl = c(0 , Inf ))
6573 .assertScalar(x = colorByCorrelation , type = " logical" )
74+ .assertVector(x = corrColorRange , type = " numeric" , rngIncl = c(0 , 1 ),
75+ len = 2 , allowNULL = TRUE )
76+ if (! is.null(corrColorRange )) {
77+ stopifnot(corrColorRange [2 ] > = corrColorRange [1 ])
78+ }
79+ .assertScalar(x = addIdentityLine , type = " logical" )
6680
6781 # # ----------------------------------------------------------------------- ##
6882 # # Define shared theme elements
@@ -131,12 +145,22 @@ plotPairs <- function(se, selAssay = "counts", doLog = TRUE, pseudocount = 1,
131145 }
132146
133147 # # Define function to create scatter plot (for use with ggpairs)
134- plotpoints <- function (data , mapping , ... ) {
135- ggplot2 :: ggplot(data = data , mapping = mapping ) +
136- ggplot2 :: geom_point(alpha = pointAlpha , size = pointSize ) +
137- ggtheme
148+ if (addIdentityLine ) {
149+ plotpoints <- function (data , mapping , ... ) {
150+ ggplot2 :: ggplot(data = data , mapping = mapping ) +
151+ ggplot2 :: geom_abline(slope = 1 , intercept = 0 ,
152+ linetype = " dashed" , color = " grey" ) +
153+ ggplot2 :: geom_point(alpha = pointAlpha , size = pointSize ) +
154+ ggtheme
155+ }
156+ } else {
157+ plotpoints <- function (data , mapping , ... ) {
158+ ggplot2 :: ggplot(data = data , mapping = mapping ) +
159+ ggplot2 :: geom_point(alpha = pointAlpha , size = pointSize ) +
160+ ggtheme
161+ }
138162 }
139-
163+
140164 # # Define the function to use for the plots
141165 if (pointsType == " smoothscatter" ) {
142166 lower <- list (continuous = smoothscat )
@@ -176,7 +200,11 @@ plotPairs <- function(se, selAssay = "counts", doLog = TRUE, pseudocount = 1,
176200
177201 # # Calculate correlations and get range
178202 allCors <- stats :: cor(mat , method = corMethod )
179- corRange <- range(abs(allCors [upper.tri(allCors )]))
203+ if (! is.null(corrColorRange )) {
204+ corRange <- corrColorRange
205+ } else {
206+ corRange <- range(abs(allCors [upper.tri(allCors )]))
207+ }
180208 if (length(unique(corRange )) == 1 ) {
181209 # # Having a range with width 0 leads to problems in the rescaling of the correlations
182210 corRange <- c(0 , 1 )
0 commit comments