-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
* Fixes #362 introduce qq plots * Remove empty y check overkill Co-authored-by: Indrajeet Patil <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#' @title plotQQ | ||
#' @description | ||
#' Producing Histograms | ||
#' | ||
#' @inheritParams addScatter | ||
#' @param dataMapping | ||
#' A `QQDataMapping` object mapping `y` and aesthetic groups to their variable names of `data`. | ||
#' @param plotConfiguration | ||
#' An optional `QQPlotConfiguration` object defining labels, grid, background and watermark. | ||
#' @return A `ggplot` object | ||
#' | ||
#' @export | ||
#' @family molecule plots | ||
#' @examples | ||
#' # Produce QQ plot of normally distributed data | ||
#' plotQQ(y = rnorm(100)) | ||
#' | ||
#' # Produce QQ plot of normally distributed data split by group | ||
#' qqData <- data.frame( | ||
#' residuals = c(rnorm(100), rnorm(100)), | ||
#' groups = c(rep("Group A", 100), rep("Group B", 100)) | ||
#' ) | ||
#' plotQQ( | ||
#' data = qqData, | ||
#' dataMapping = QQDataMapping$new(y = "residuals", group = "groups") | ||
#' ) | ||
#' | ||
plotQQ <- function(data = NULL, | ||
metaData = NULL, | ||
y = NULL, | ||
dataMapping = NULL, | ||
plotConfiguration = NULL, | ||
plotObject = NULL) { | ||
#----- Validation and formatting of input arguments ----- | ||
if (is.null(data)) { | ||
validateIsNumeric(y) | ||
data <- data.frame(y = y) | ||
dataMapping <- dataMapping %||% QQDataMapping$new( | ||
y = "y", | ||
data = data | ||
) | ||
} | ||
validateIsNotEmpty(data) | ||
validateIsOfType(data, "data.frame") | ||
dataMapping <- .setDataMapping(dataMapping, QQDataMapping, data) | ||
|
||
plotConfiguration <- .setPlotConfiguration( | ||
plotConfiguration, QQPlotConfiguration, | ||
data, metaData, dataMapping | ||
) | ||
plotObject <- .setPlotObject(plotObject, plotConfiguration) | ||
|
||
mapData <- dataMapping$checkMapData(data) | ||
mapLabels <- .getAesStringMapping(dataMapping) | ||
|
||
#----- Build layers of molecule plot ----- | ||
# 1- Line layer | ||
aestheticValues <- .getAestheticValuesFromConfiguration( | ||
n = 1, | ||
plotConfigurationProperty = plotObject$plotConfiguration$lines, | ||
propertyNames = c("color", "size", "alpha", "linetype") | ||
) | ||
|
||
plotObject <- plotObject + | ||
ggplot2::geom_qq_line( | ||
data = mapData, | ||
mapping = ggplot2::aes_string( | ||
sample = mapLabels$y | ||
), | ||
size = aestheticValues$size, | ||
color = aestheticValues$color, | ||
linetype = aestheticValues$linetype, | ||
alpha = aestheticValues$alpha, | ||
na.rm = TRUE | ||
) | ||
|
||
# 2- Scatter point layer | ||
aestheticValues <- .getAestheticValuesFromConfiguration( | ||
n = 1, | ||
plotConfigurationProperty = plotObject$plotConfiguration$points, | ||
propertyNames = c("size", "alpha") | ||
) | ||
|
||
plotObject <- plotObject + | ||
ggplot2::geom_point( | ||
data = mapData, | ||
mapping = ggplot2::aes_string( | ||
x = DefaultDataMappingValues$qqPlot, | ||
y = mapLabels$y, | ||
color = mapLabels$color, | ||
shape = mapLabels$shape | ||
), | ||
size = aestheticValues$size, | ||
alpha = aestheticValues$alpha, | ||
na.rm = TRUE | ||
) | ||
|
||
#----- Update properties using ggplot2::scale functions ----- | ||
plotObject <- .updateAesProperties( | ||
plotObject, | ||
plotConfigurationProperty = "points", | ||
propertyNames = c("color", "shape"), | ||
data = mapData, | ||
mapLabels = mapLabels | ||
) | ||
plotObject <- .updateAxes(plotObject) | ||
return(plotObject) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#' @title QQDataMapping | ||
#' @description R6 class for mapping `x`, `y` and `GroupMapping` variables to `data` | ||
#' @export | ||
#' @family DataMapping classes | ||
QQDataMapping <- R6::R6Class( | ||
"QQDataMapping", | ||
inherit = XYGDataMapping, | ||
public = list( | ||
#' @description Check that `data` variables include map variables | ||
#' @param data data.frame to check | ||
#' @param metaData list containing information on `data` | ||
#' @return A data.frame with map and `defaultAes` variables. | ||
#' Dummy variable `defaultAes` is necessary to allow further modification of plots. | ||
checkMapData = function(data, metaData = NULL) { | ||
validateIsOfType(data, "data.frame") | ||
mapData <- super$checkMapData(data, metaData) | ||
if (isEmpty(self$x)) { | ||
# Get theoritical quantiles for normal distribution | ||
xQuantiles <- stats::qqnorm(data[, self$y], plot.it = FALSE)$x | ||
mapData[, DefaultDataMappingValues$qqPlot] <- xQuantiles | ||
} | ||
self$data <- mapData | ||
return(mapData) | ||
} | ||
) | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.