Skip to content

Commit

Permalink
Fixes #867 Fixes #866 create appropriate minor and major time ticks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pchelle committed Sep 13, 2022
1 parent d949223 commit 63b5fcb
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 50 deletions.
4 changes: 4 additions & 0 deletions R/qualification-comparison-time-profile.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ plotQualificationComparisonTimeProfile <- function(configurationPlan, settings)
targetUnit = axesProperties$x$unit,
sourceUnit = timeProfilePlan$TimeUnit
)
axesProperties$x <- c(
axesProperties$x,
getTimeTicksFromUnit(axesProperties$x$unit, simulationDuration)
)

plotConfiguration <- getPlotConfigurationFromPlan(timeProfilePlan[["PlotSettings"]])
timeProfilePlot <- tlf::initializePlot(plotConfiguration)
Expand Down
4 changes: 4 additions & 0 deletions R/qualification-time-profile.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ plotQualificationPopulationTimeProfile <- function(simulationAnalysis, observedD
simulationPathResults$data[, "Time"],
axesProperties$x$unit
)
axesProperties$x <- c(
axesProperties$x,
getTimeTicksFromUnit(axesProperties$x$unit, time)
)
outputValues <- ospsuite::toUnit(
simulationQuantity,
simulationPathResults$data[, outputPath],
Expand Down
1 change: 1 addition & 0 deletions R/utilities-absorption.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ plotAbsorptionTimeProfile <- function(data,
metaData = metaData,
dataMapping = timeVsFractionDataMapping
)
plotConfiguration <- updatePlotConfigurationTimeTicks(data, metaData, dataMapping, plotConfiguration)

timeVsFractionPlot <- tlf::addLine(
data = data,
Expand Down
8 changes: 7 additions & 1 deletion R/utilities-configuration-plan.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ updatePlotAxes <- function(plotObject, axesProperties) {
)

try({
plotObject <- tlf::setXAxis(plotObject, scale = axesProperties$x$scale, limits = c(axesProperties$x$min, axesProperties$x$max))
plotObject <- tlf::setXAxis(
plotObject,
scale = axesProperties$x$scale,
limits = c(axesProperties$x$min, axesProperties$x$max),
ticks = axesProperties$x$ticks,
ticklabels = axesProperties$x$ticklabels
)
})
try({
plotObject <- tlf::setYAxis(plotObject, scale = axesProperties$y$scale, limits = c(axesProperties$y$min, axesProperties$y$max))
Expand Down
81 changes: 33 additions & 48 deletions R/utilities-goodness-of-fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -517,28 +517,26 @@ plotMeanTimeProfile <- function(simulatedData,
metaData = NULL,
dataMapping = NULL,
plotConfiguration = NULL) {
plotConfiguration <- plotConfiguration %||%
TimeProfilePlotConfiguration$new(
data = simulatedData,
metaData = metaData,
dataMapping = dataMapping
)
plotConfiguration <- updatePlotConfigurationTimeTicks(simulatedData, metaData, dataMapping, plotConfiguration)

timeProfilePlot <- tlf::addLine(
data = simulatedData,
metaData = metaData,
dataMapping = dataMapping,
plotConfiguration = plotConfiguration %||%
TimeProfilePlotConfiguration$new(
data = simulatedData,
metaData = metaData,
dataMapping = dataMapping
)
plotConfiguration = plotConfiguration
)
if (!isEmpty(observedData)) {
timeProfilePlot <- tlf::addScatter(
data = observedData,
metaData = metaData,
dataMapping = dataMapping,
plotConfiguration = plotConfiguration %||%
TimeProfilePlotConfiguration$new(
data = simulatedData,
metaData = metaData,
dataMapping = dataMapping
),
plotConfiguration = plotConfiguration,
plotObject = timeProfilePlot
)
}
Expand All @@ -547,12 +545,7 @@ plotMeanTimeProfile <- function(simulatedData,
data = lloqData,
metaData = metaData,
dataMapping = dataMapping,
plotConfiguration = plotConfiguration %||%
TimeProfilePlotConfiguration$new(
data = simulatedData,
metaData = metaData,
dataMapping = dataMapping
),
plotConfiguration = plotConfiguration,
plotObject = timeProfilePlot
)
}
Expand Down Expand Up @@ -585,54 +578,42 @@ plotPopulationTimeProfile <- function(simulatedData,
metaData$ymin <- metaData$Concentration
metaData$ymax <- metaData$Concentration

plotConfiguration <- plotConfiguration %||%
TimeProfilePlotConfiguration$new(
data = simulatedData,
metaData = metaData,
dataMapping = TimeProfileDataMapping$new(x = "x", ymin = "ymin", ymax = "ymax")
)
plotConfiguration <- updatePlotConfigurationTimeTicks(simulatedData, metaData, dataMapping, plotConfiguration)

timeProfilePlot <- tlf::addRibbon(
x = simulatedData$Time,
ymin = simulatedData$lowPerc,
ymax = simulatedData$highPerc,
metaData = metaData,
caption = simulatedData$legendRange,
plotConfiguration = plotConfiguration %||%
TimeProfilePlotConfiguration$new(
data = simulatedData,
metaData = metaData,
dataMapping = TimeProfileDataMapping$new(x = "x", ymin = "ymin", ymax = "ymax")
)
plotConfiguration = plotConfiguration
)
timeProfilePlot <- tlf::addLine(
x = simulatedData$Time,
y = simulatedData$median,
caption = simulatedData$legendMedian,
plotConfiguration = plotConfiguration %||%
TimeProfilePlotConfiguration$new(
data = simulatedData,
metaData = metaData,
dataMapping = TimeProfileDataMapping$new(x = "x", ymin = "ymin", ymax = "ymax")
),
plotConfiguration = plotConfiguration,
plotObject = timeProfilePlot
)
timeProfilePlot <- tlf::addLine(
x = simulatedData$Time,
y = simulatedData$mean,
caption = simulatedData$legendMean,
plotConfiguration = plotConfiguration %||%
TimeProfilePlotConfiguration$new(
data = simulatedData,
metaData = metaData,
dataMapping = TimeProfileDataMapping$new(x = "x", ymin = "ymin", ymax = "ymax")
),
plotConfiguration = plotConfiguration,
plotObject = timeProfilePlot
)
if (!isEmpty(observedData)) {
timeProfilePlot <- tlf::addScatter(
data = observedData,
metaData = metaData,
dataMapping = dataMapping,
plotConfiguration = plotConfiguration %||%
TimeProfilePlotConfiguration$new(
data = simulatedData,
metaData = metaData,
dataMapping = TimeProfileDataMapping$new(x = "x", ymin = "ymin", ymax = "ymax")
),
plotConfiguration = plotConfiguration,
plotObject = timeProfilePlot
)
}
Expand All @@ -641,12 +622,7 @@ plotPopulationTimeProfile <- function(simulatedData,
data = lloqData,
metaData = metaData,
dataMapping = dataMapping,
plotConfiguration = plotConfiguration %||%
TimeProfilePlotConfiguration$new(
data = simulatedData,
metaData = metaData,
dataMapping = TimeProfileDataMapping$new(x = "x", ymin = "ymin", ymax = "ymax")
),
plotConfiguration = plotConfiguration,
plotObject = timeProfilePlot
)
}
Expand Down Expand Up @@ -1065,6 +1041,10 @@ getResidualsPlotResults <- function(timeRange, residualsData, metaDataFrame, str
"Residuals" = list(dimension = residualsLegend, unit = "")
)

residualTimeTicks <- getTimeTicksFromUnit(
residualsMetaData$Time$unit,
timeValues = residualsData$Time
)
goodnessOfFitPlots[["resVsTime"]] <- tlf::plotResVsTime(
data = residualsData,
metaData = residualsMetaData,
Expand All @@ -1075,6 +1055,11 @@ getResidualsPlotResults <- function(timeRange, residualsData, metaDataFrame, str
),
plotConfiguration = settings$plotConfigurations[["resVsTime"]]
)
goodnessOfFitPlots[["resVsTime"]] <- tlf::setXAxis(
plotObject = goodnessOfFitPlots[["resVsTime"]],
ticks = residualTimeTicks$ticks,
ticklabels = residualTimeTicks$ticklabels
)
goodnessOfFitCaptions[["resVsTime"]] <- getGoodnessOfFitCaptions(structureSet, "resVsTime", residualScale)

goodnessOfFitPlots[["resHisto"]] <- tlf::plotHistogram(
Expand Down
2 changes: 2 additions & 0 deletions R/utilities-mass-balance.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ plotMassBalanceTimeProfile <- function(data,
metaData = metaData,
dataMapping = timeVsAmountDataMapping
)
plotConfiguration <- updatePlotConfigurationTimeTicks(data, metaData, dataMapping, plotConfiguration)

timeVsAmountPlot <- tlf::initializePlot(plotConfiguration)

Expand Down Expand Up @@ -303,6 +304,7 @@ plotMassBalanceCumulativeTimeProfile <- function(data,
metaData = metaData,
dataMapping = timeVsAmountDataMapping
)
plotConfiguration <- updatePlotConfigurationTimeTicks(data, metaData, dataMapping, plotConfiguration)

timeVsAmountPlot <- tlf::initializePlot(plotConfiguration)

Expand Down
65 changes: 65 additions & 0 deletions R/utilities-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,71 @@ autoAxesTicksFromLimits <- function(limits) {
return(rep(c(1, 2, 5), length(logTicks)) * 10^rep(logTicks, each = 3))
}

#' @title getTimeTicksFromUnit
#' @description Defines auto time ticks from time unit and time values
#' @param unit A time unit as defined in `ospsuite::ospUnits$Time`
#' @param timeValues Numeric values used by the data
#' @return List of `ticks` and their `ticklabels`
#' @keywords internal
getTimeTicksFromUnit <- function(unit, timeValues = NULL) {
if (isEmpty(timeValues)) {
return()
}
minTime <- floor(min(0, as.numeric(timeValues), na.rm = TRUE))
maxTime <- ceiling(max(as.numeric(timeValues), na.rm = TRUE))
# For undefined ticking of units, assume major tick every 10 units (eg. 10 seconds)
majorTickStep <- 10
# For undefined ticking of units, assume minor tick every 1 unit (eg. 1 seconds)
minorTickStep <- 1

if (isIncluded(unit, ospsuite::ospUnits$Time$h)) {
# Major ticks every 6 hours
majorTickStep <- 6
}
if (isIncluded(unit, ospsuite::ospUnits$Time$`day(s)`)) {
# Major ticks every 7 days
majorTickStep <- 7
}
if (isIncluded(unit, ospsuite::ospUnits$Time$`week(s)`)) {
# Major ticks every 4 weeks
majorTickStep <- 4
}
if (isIncluded(unit, ospsuite::ospUnits$Time$`month(s)`)) {
# Major ticks every 6 months
majorTickStep <- 6
}

minorTicks <- seq(minTime, maxTime, minorTickStep)
majorTicks <- seq(minTime, maxTime, majorTickStep)
ticklabels <- as.character(minorTicks)
ticklabels[!(minorTicks %in% majorTicks)] <- ""

timeTicks <- list(
ticks = minorTicks,
ticklabels = ticklabels
)
return(timeTicks)
}

#' @title updatePlotConfigurationTimeTicks
#' @description Update time ticks based on selected time unit in `PlotConfiguration` objects
#' @param data data.frame
#' @param metaData meta data on `data`
#' @param dataMapping `XYGDataMapping` R6 class object from `tlf` library
#' @param plotConfiguration `PlotConfiguration` R6 class object from `tlf` library
#' @return A `PlotConfiguration` object
#' @keywords internal
updatePlotConfigurationTimeTicks <- function(data, metaData, dataMapping, plotConfiguration) {
timeValues <- data[, dataMapping$x]
timeUnit <- metaData[[dataMapping$x]]$unit
timeTicks <- getTimeTicksFromUnit(timeUnit, timeValues)

plotConfiguration$xAxis$ticks <- timeTicks$ticks
plotConfiguration$xAxis$ticklabels <- timeTicks$ticklabels

return(plotConfiguration)
}

#' @title getPlotConfigurationFromPlan
#' @description Get the appropriate `PlotConfiguration` object with scaled dimensions for exporting it
#' @param plotProperties Plot properties from configuration plan
Expand Down
20 changes: 20 additions & 0 deletions man/getTimeTicksFromUnit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/pkDictionaryQualificationOSP.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions man/updatePlotConfigurationTimeTicks.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 63b5fcb

Please sign in to comment.