Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… observed and simulated time profiles (Open-Systems-Pharmacology#393)
  • Loading branch information
pchelle authored and Yuri05 committed Jan 27, 2023
1 parent b564383 commit 6ea1ef1
Show file tree
Hide file tree
Showing 19 changed files with 437 additions and 199 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ Collate:
'plot-grid.R'
'plot-histogram.R'
'plot-obs-vs-pred.R'
'plot-observed-time-profile.R'
'plot-pkratio.R'
'plot-qq.R'
'plot-res-vs-pred.R'
'plot-simulated-time-profile.R'
'plot-timeprofile.R'
'plot-tornado.R'
'plotconfiguration-axis.R'
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ export(plotDDIRatio)
export(plotGrid)
export(plotHistogram)
export(plotObsVsPred)
export(plotObservedTimeProfile)
export(plotPKRatio)
export(plotQQ)
export(plotResVsPred)
export(plotResVsTime)
export(plotSimulatedTimeProfile)
export(plotTimeProfile)
export(plotTornado)
export(runPlotMaker)
Expand Down
65 changes: 65 additions & 0 deletions R/plot-observed-time-profile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#' @title plotObservedTimeProfile
#' @description
#' Producing Time Profile plots for observed data
#'
#' @inheritParams addScatter
#' @param dataMapping
#' An `ObservedDataMapping` object mapping `x`, `y`, `ymin`, `ymax` and aesthetic groups to their variable names of `observedData`.
#' @param plotConfiguration
#' An optional `TimeProfilePlotConfiguration` object defining labels, grid, background and watermark.
#' @return A `ggplot` object
#'
#' @export
#' @family molecule plots
#' @examples
#' # Produce a Time profile plot with observed data
#' obsData <- data.frame(x = c(1, 2, 1, 2, 3), y = c(5, 0.2, 2, 3, 4))
#' plotObservedTimeProfile(
#' data = obsData,
#' dataMapping = ObservedDataMapping$new(x = "x", y = "y")
#' )
plotObservedTimeProfile <- function(data = NULL,
metaData = NULL,
dataMapping = NULL,
plotConfiguration = NULL,
plotObject = NULL) {
#----- Validation and formatting of input arguments -----
validateIsNotEmpty(data)
validateIsOfType(data, "data.frame")
dataMapping <- .setDataMapping(dataMapping, ObservedDataMapping, data)
plotConfiguration <- .setPlotConfiguration(
plotConfiguration, TimeProfilePlotConfiguration,
data, metaData, dataMapping
)
plotObject <- .setPlotObject(plotObject, plotConfiguration)

mapData <- dataMapping$checkMapData(data)
mapLabels <- .getAesStringMapping(dataMapping)

#----- Build layers of molecule plot -----
# 1- Error bars if available
if (!any(isEmpty(dataMapping$ymin), isEmpty(dataMapping$ymax))) {
plotObject <- .addErrorbarLayer(
plotObject,
data = mapData,
mapLabels = mapLabels
)
}
# 2- Scatter points
plotObject <- .addScatterLayer(
plotObject,
data = mapData,
mapLabels = mapLabels
)

#----- Update properties using ggplot2::scale functions -----
plotObject <- .updateAesProperties(
plotObject,
plotConfigurationProperty = "points",
propertyNames = c("color", "shape"),
data = mapData,
mapLabels = mapLabels
)
plotObject <- .updateAxes(plotObject)
return(plotObject)
}
112 changes: 112 additions & 0 deletions R/plot-simulated-time-profile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#' @title plotSimulatedTimeProfile
#' @description
#' Producing Time Profile plots
#'
#' @inheritParams addScatter
#' @param dataMapping
#' A `TimeProfileDataMapping` object mapping `x`, `y`, `ymin`, `ymax` and aesthetic groups to their variable names of `data`.
#' @param plotConfiguration
#' An optional `TimeProfilePlotConfiguration` object defining labels, grid, background and watermark.
#' @return A `ggplot` object
#'
#' @export
#' @family molecule plots
#' @examples
#' # Produce a Time profile plot with simulated data
#' simTime <- seq(1, 10, 0.1)
#' simData <- data.frame(
#' x = simTime,
#' y = 10 * exp(-simTime),
#' ymin = 8 * exp(-simTime),
#' ymax = 12 * exp(-simTime)
#' )
#'
#' plotSimulatedTimeProfile(
#' data = simData,
#' dataMapping = TimeProfileDataMapping$new(x = "x", y = "y", ymin = "ymin", ymax = "ymax")
#' )
plotSimulatedTimeProfile <- function(data = NULL,
metaData = NULL,
dataMapping = NULL,
plotConfiguration = NULL,
plotObject = NULL) {
#----- Validation and formatting of input arguments -----
validateIsNotEmpty(data)
validateIsOfType(data, "data.frame")
dataMapping <- .setDataMapping(dataMapping, TimeProfileDataMapping, data)
plotConfiguration <- .setPlotConfiguration(
plotConfiguration, TimeProfilePlotConfiguration,
data, metaData, dataMapping
)
plotObject <- .setPlotObject(plotObject, plotConfiguration)

mapData <- dataMapping$checkMapData(data)
mapLabels <- .getAesStringMapping(dataMapping)

#----- Build layers of molecule plot -----
# 1- Ribbons if available
if (!any(isEmpty(dataMapping$ymin), isEmpty(dataMapping$ymax))) {
aestheticValues <- .getAestheticValuesFromConfiguration(
n = 1,
position = 0,
plotConfigurationProperty = plotObject$plotConfiguration$ribbons,
propertyNames = c("alpha")
)
plotObject <- plotObject +
ggplot2::geom_ribbon(
data = mapData,
mapping = ggplot2::aes_string(
x = mapLabels$x,
ymin = mapLabels$ymin,
ymax = mapLabels$ymax,
fill = mapLabels$fill,
group = mapLabels$linetype
),
alpha = aestheticValues$alpha,
na.rm = TRUE,
show.legend = TRUE
)
}

# 2- Lines
if (!isEmpty(dataMapping$y)) {
aestheticValues <- .getAestheticValuesFromConfiguration(
n = 1,
position = 0,
plotConfigurationProperty = plotObject$plotConfiguration$lines,
propertyNames = c("alpha", "size")
)
plotObject <- plotObject +
ggplot2::geom_path(
data = mapData,
mapping = ggplot2::aes_string(
x = mapLabels$x,
y = mapLabels$y,
color = mapLabels$color,
linetype = mapLabels$linetype
),
size = aestheticValues$size,
alpha = aestheticValues$alpha,
na.rm = TRUE,
show.legend = TRUE,
)
}

#----- Update properties using ggplot2::scale functions -----
plotObject <- .updateAesProperties(
plotObject,
plotConfigurationProperty = "ribbons",
propertyNames = "fill",
data = mapData,
mapLabels = mapLabels
)
plotObject <- .updateAesProperties(
plotObject,
plotConfigurationProperty = "lines",
propertyNames = c("color", "linetype"),
data = mapData,
mapLabels = mapLabels
)
plotObject <- .updateAxes(plotObject)
return(plotObject)
}
Loading

0 comments on commit 6ea1ef1

Please sign in to comment.