diff --git a/R/aaa-utilities.R b/R/aaa-utilities.R index 6e15cdff..749d9746 100644 --- a/R/aaa-utilities.R +++ b/R/aaa-utilities.R @@ -33,7 +33,7 @@ parseVariableFromObject <- function(objectName, variableName, keepIfNull = FALSE #' @return An expression to `eval()` #' @keywords internal parseValueToObject <- function(objectName, value) { - if (isOfLength(value, 0)) { + if (isEmpty(value)) { return(parse(text = paste0(objectName, " <- NULL"))) } if (isOfType(value, "character")) { @@ -113,6 +113,8 @@ parseAddScatterLayer <- function() { shape = mapLabels$shape ), size = getAestheticValues(n = 1, selectionKey = plotConfiguration$points$size, position = 0, aesthetic = "size"), + alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$points$alpha, aesthetic = "alpha"), + na.rm = TRUE, show.legend = TRUE ) }) @@ -120,11 +122,12 @@ parseAddScatterLayer <- function() { #' @title parseAddLineLayer #' @description Create an expression that adds scatter plot layer +#' TODO: create a vignette explaining how argument `lines` in dataMapping is related to this #' @param type one of "horizontal", "vertical" or "diagonal" #' Note that for "diagonal", geom_abline is used. #' `value` of intercept is taken as is for linear scale but corresponds to the log of `value` for log scale. -#' For instance, intercept = c(-1, 0, 1) actually means that the line will go through c(0.1, 1, 10) for x=1 -#' because log10(c(-1, 0, 1)) = c(0.1, 1, 10). +#' For instance, intercept = c(-1, 0, 1) with log scale actually means that the line will go through c(0.1, 1, 10) +#' because c(-1, 0, 1) = log10(c(0.1, 1, 10)). #' @param value value of xintercept or yintercept #' @param position line position for aesthetic properties #' @return An expression to `eval()` @@ -140,6 +143,7 @@ parseAddLineLayer <- function(type, value, position) { ), "color=getAestheticValues(n=1,selectionKey=plotConfiguration$lines$color,position=", position, ',aesthetic="color"),', "linetype=getAestheticValues(n=1,selectionKey=plotConfiguration$lines$linetype,position=", position, ',aesthetic="linetype"),', + "alpha=getAestheticValues(n=1,selectionKey=plotConfiguration$lines$alpha,position=", position, ',aesthetic="alpha"),', "size=getAestheticValues(n=1,selectionKey=plotConfiguration$lines$size,position=", position, ', aesthetic="size"))' )) } @@ -163,6 +167,8 @@ parseAddUncertaintyLayer <- function() { # Error bar size uses a ratio of 1/4 to match with point size size = getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$size, position = 0, aesthetic = "size"), linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$linetype, aesthetic = "linetype"), + alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, aesthetic = "alpha"), + na.rm = TRUE, show.legend = TRUE ) } diff --git a/R/plot-ddiratio.R b/R/plot-ddiratio.R index b00a9ca7..c4680944 100644 --- a/R/plot-ddiratio.R +++ b/R/plot-ddiratio.R @@ -42,12 +42,12 @@ plotDDIRatio <- function(data, if(residualsVsObserved){ lineOrientation <- "ddiHorizontal" } - # Include horizontal lines + # Include diagonal or horizontal lines depending on the plot type for (lineIndex in seq_along(dataMapping$lines)) { # position correspond to the number of layer lines already added eval(parseAddLineLayer(lineOrientation, dataMapping$lines[[lineIndex]], lineIndex - 1)) } - if (isOfLength(lineIndex, 0)) { + if (isEmpty(lineIndex)) { lineIndex <- 0 } # Add Guest et al. lines to plot @@ -60,6 +60,7 @@ plotDDIRatio <- function(data, na.rm = TRUE, color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"), linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"), + alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"), size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size") ) + ggplot2::geom_path( @@ -68,6 +69,7 @@ plotDDIRatio <- function(data, na.rm = TRUE, color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"), linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"), + alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"), size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size") ) diff --git a/R/plot-obs-vs-pred.R b/R/plot-obs-vs-pred.R index a7a7c19e..da5710aa 100644 --- a/R/plot-obs-vs-pred.R +++ b/R/plot-obs-vs-pred.R @@ -43,10 +43,11 @@ plotObsVsPred <- function(data, plotObject <- plotObject %||% initializePlot(plotConfiguration) + # Add diagonal lines with offset defined in lines of dataMapping for (lineIndex in seq_along(dataMapping$lines)) { eval(parseAddLineLayer("diagonal", dataMapping$lines[[lineIndex]], lineIndex - 1)) } - if (isOfLength(lineIndex, 0)) { + if (isEmpty(lineIndex)) { lineIndex <- 0 } # Add Smoother if defined @@ -57,9 +58,11 @@ plotObsVsPred <- function(data, method = "loess", se = FALSE, formula = "y ~ x", + na.rm = TRUE, mapping = ggplot2::aes_string(x = mapLabels$x, y = mapLabels$y), color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"), linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"), + alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"), size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size") ) } @@ -71,8 +74,10 @@ plotObsVsPred <- function(data, method = "lm", se = FALSE, formula = "y ~ x", + na.rm = TRUE, color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"), linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"), + alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"), size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size") ) } diff --git a/R/plot-res-vs-pred.R b/R/plot-res-vs-pred.R index 0ef30e17..1cbecc03 100644 --- a/R/plot-res-vs-pred.R +++ b/R/plot-res-vs-pred.R @@ -33,10 +33,11 @@ plotResVsPred <- function(data, plotObject <- plotObject %||% initializePlot(plotConfiguration) + # Add horizontal lines with offset defined in lines of dataMapping for (lineIndex in seq_along(dataMapping$lines)) { eval(parseAddLineLayer("horizontal", dataMapping$lines[[lineIndex]], lineIndex - 1)) } - if (isOfLength(lineIndex, 0)) { + if (isEmpty(lineIndex)) { lineIndex <- 0 } # Add Smoother if defined @@ -47,9 +48,11 @@ plotResVsPred <- function(data, method = "loess", se = FALSE, formula = "y ~ x", + na.rm = TRUE, mapping = ggplot2::aes_string(x = mapLabels$x, y = mapLabels$y), color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"), linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"), + alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"), size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size") ) } @@ -61,8 +64,10 @@ plotResVsPred <- function(data, method = "lm", se = FALSE, formula = "y ~ x", + na.rm = TRUE, color = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$color, position = lineIndex, aesthetic = "color"), linetype = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$linetype, position = lineIndex, aesthetic = "linetype"), + alpha = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$alpha, position = lineIndex, aesthetic = "alpha"), size = getAestheticValues(n = 1, selectionKey = plotConfiguration$lines$size, position = lineIndex, aesthetic = "size") ) } diff --git a/man/parseAddLineLayer.Rd b/man/parseAddLineLayer.Rd index 73cc2af9..dd9030d9 100644 --- a/man/parseAddLineLayer.Rd +++ b/man/parseAddLineLayer.Rd @@ -10,8 +10,8 @@ parseAddLineLayer(type, value, position) \item{type}{one of "horizontal", "vertical" or "diagonal" Note that for "diagonal", geom_abline is used. \code{value} of intercept is taken as is for linear scale but corresponds to the log of \code{value} for log scale. -For instance, intercept = c(-1, 0, 1) actually means that the line will go through c(0.1, 1, 10) for x=1 -because log10(c(-1, 0, 1)) = c(0.1, 1, 10).} +For instance, intercept = c(-1, 0, 1) with log scale actually means that the line will go through c(0.1, 1, 10) +because c(-1, 0, 1) = log10(c(0.1, 1, 10)).} \item{value}{value of xintercept or yintercept} @@ -22,5 +22,6 @@ An expression to \code{eval()} } \description{ Create an expression that adds scatter plot layer +TODO: create a vignette explaining how argument \code{lines} in dataMapping is related to this } \keyword{internal} diff --git a/tests/testthat/test-no-na-warnings.R b/tests/testthat/test-no-na-warnings.R new file mode 100644 index 00000000..162ac464 --- /dev/null +++ b/tests/testthat/test-no-na-warnings.R @@ -0,0 +1,32 @@ +context("NA data are removed without warnings in scatter plots") + +# Test data that include a NA value +testData <- data.frame( + x = c(1,2,3), + y = c(1,2,NA) +) + +# To prevent copy/paste, expressions are used in the sequel +plotNames <- c("TimeProfile", "ObsVsPred", "ResVsPred", "PKRatio", "DDIRatio") + +# Expression is "DataMapping <- DataMapping$new()" +createDataMapping <- parse(text = paste0(tolower(plotNames), "DataMapping <- ", plotNames, "DataMapping$new(x='x',y='y')")) +eval(createDataMapping) + +# createPlot expression is "plot(data=testData, dataMapping=DataMapping)" +createPlot <- paste0("plot", plotNames, "(data=testData, dataMapping = ", tolower(plotNames), "DataMapping)") +ggplotExpression <- parse(text = paste0('expect_is(', createPlot, ', "ggplot")')) +warningExpression <- parse(text = paste0('expect_silent(', createPlot, ')')) + + +test_that("Plot is produced as a ggplot object", { + expect_is(addScatter(data=testData), "ggplot") + expect_is(addLine(data=testData), "ggplot") + eval(ggplotExpression) +}) + +test_that("No warnings were produced when creating/printing the plot", { + expect_silent(addScatter(data=testData)) + expect_silent(addLine(data=testData)) + eval(warningExpression) +})