diff --git a/R/plotTimeProfile.R b/R/plotTimeProfile.R index 9b4dbc1..8cefb91 100644 --- a/R/plotTimeProfile.R +++ b/R/plotTimeProfile.R @@ -78,6 +78,7 @@ plotTimeProfile <- function(data = NULL, # nolint geomErrorbarAttributes = getDefaultGeomAttributes("Errorbar"), geomLLOQAttributes = getDefaultGeomAttributes("LLOQ"), groupAesthetics = c("colour", "fill", "shape")) { + groupAesthetics <- ggplot2::standardise_aes_names(groupAesthetics) .validatePlotTimeProfileInputs( data = data, @@ -185,7 +186,7 @@ plotTimeProfile <- function(data = NULL, # nolint is.null(listMappedData$mapSimulatedAndObserved) && all(c("colour", "shape") %in% groupAesthetics)) { plotObject <- plotObject + - scale_shape(guide = "none") + guides(shape = "none") } return(plotObject) @@ -375,7 +376,7 @@ plotTimeProfile <- function(data = NULL, # nolint #' @param commonLimits common limits for simulated and observed data #' @param y2ScaleArgs list with arguments for secondary y-axis #' @param requireDualAxis boolean if TRUE secondary axis is needed -#' @param yScaleArgs list with y2scale arguments +#' @param yScaleArgs list with yScale arguments #' #' @return list with #' `simMappedData` adjusted object of class `MappedDataTimeprofile` for simulated data @@ -541,7 +542,7 @@ plotTimeProfile <- function(data = NULL, # nolint plotObject <- plotObject + scale_discrete_manual(aesthetic, values = mapSimulatedAndObserved[[aesthetic]], - guide = guide_legend(order = 1, title = "Simulated") + guide = guide_legend(order = 2, title = "Simulated") ) } } @@ -573,8 +574,12 @@ plotTimeProfile <- function(data = NULL, # nolint # to separate simulated and observed legend entries and start again with default colors if (!is.null(mapSimulatedAndObserved)) { for (aesthetic in groupAesthetics) { + guidesList <- stats::setNames( + list(guide_legend(title = "Simulated", order = 2)), + aesthetic + ) plotObject <- plotObject + - guides(aesthetic = guide_legend(title = "observed", order = 1)) + + guides(!!!guidesList) + ggnewscale::new_scale(new_aes = aesthetic) } } diff --git a/tests/testthat/_snaps/plotTimeProfile/different-observedmapping.svg b/tests/testthat/_snaps/plotTimeProfile/different-observedmapping.svg new file mode 100644 index 0000000..ac53b57 --- /dev/null +++ b/tests/testthat/_snaps/plotTimeProfile/different-observedmapping.svg @@ -0,0 +1,135 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0 +5 +10 +15 + + + + + + + + + +0 +6 +12 +18 +24 +Time [h] +Concentration [mg/l] + +caption + + + + + + + + + + + + +Observed Data 1 +Observed Data 2 +Simulated Data 1 +Simulated Data 2 +different-observedMapping +preliminary analysis + + diff --git a/tests/testthat/test-plotTimeProfile.R b/tests/testthat/test-plotTimeProfile.R index 8ea8458..d16afa8 100644 --- a/tests/testthat/test-plotTimeProfile.R +++ b/tests/testthat/test-plotTimeProfile.R @@ -369,4 +369,52 @@ test_that("plotTimeProfile works with formula as aesthic", { yScale = AxisScales$log )) }) + +test_that("plotTimeProfile works with different observedMapping", { + skip_if_not_installed("vdiffr") + skip_if(getRversion() < "4.1") + + simData <- exampleDataTimeProfile |> + dplyr::filter(SetID %in% c("DataSet1", "DataSet2")) |> + dplyr::filter(Type == "simulated") |> + dplyr::select(c("time", "values", "minValues", "maxValues", "caption")) + + obsData <- exampleDataTimeProfile |> + dplyr::filter(SetID %in% c("DataSet1", "DataSet2")) |> + dplyr::filter(Type == "observed") |> + dplyr::select(c("time", "values", "minValues", "maxValues", "caption")) + + metaData <- attr(exampleDataTimeProfile, "metaData") + + # Test with different observedMapping (different aesthetics for observed data) + fig <- plotTimeProfile( + data = simData, + observedData = obsData, + metaData = metaData, + mapping = aes( + x = time, + y = values, + ymin = minValues, + ymax = maxValues, + groupby = caption + ), + observedMapping = aes( + x = time, + y = values, + ymin = minValues, + ymax = maxValues, + groupby = caption, + shape = caption + ) + ) + + theme( + legend.position = "top" + ) + + vdiffr::expect_doppelganger( + title = "different-observedMapping", + fig = fig + ) +}) + ospsuite.plots::resetDefaults(oldDefaults)