Skip to content

Commit

Permalink
Fixes Open-Systems-Pharmacology#333 set cap width of errorbars (Open-…
Browse files Browse the repository at this point in the history
…Systems-Pharmacology#344)

* Fixes Open-Systems-Pharmacology#333 set cap width of errorbars

Currently, width is a global setting applied the same way to all error bars

* Absolute cap values are now relative and width renamed to extent

Renaming width to extent to prevent user confusion when used for horizontal errorbars whose caps become vertical

* Fixes Open-Systems-Pharmacology#333 cap extent renamed cap size and use consistent unit in pts

Co-authored-by: Indrajeet Patil <patilindrajeet.science@gmail.com>
  • Loading branch information
pchelle and IndrajeetPatil committed Jul 25, 2022
1 parent cdcdd20 commit 9640f54
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 56 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export(setCaptionVisibility)
export(setDefaultAggregationBins)
export(setDefaultAggregationFunctions)
export(setDefaultAggregationLabels)
export(setDefaultErrorbarCapSize)
export(setDefaultExportParameters)
export(setDefaultLegendPosition)
export(setDefaultLegendTitle)
Expand Down
43 changes: 40 additions & 3 deletions R/aaa-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@
}

#' @title .parseAddUncertaintyLayer
#' @description Create an expression that adds errorbars if uncertainty is included in dataMapping
#' @description Create an expression that adds errorbars
#' `mapLabels` needs to be obtained from `DataMapping` objects
#' @return An expression to `eval()`
#' @keywords internal
.parseAddUncertaintyLayer <- function(direction = "vertical") {
Expand All @@ -165,7 +166,8 @@
"vertical" = "x = mapLabels$x, ymin = mapLabels$ymin, ymax = mapLabels$y,",
"horizontal" = "y = mapLabels$y, xmin = mapLabels$xmin, xmax = mapLabels$x,"
),
"color = mapLabels$color",
"color = mapLabels$color,",
"group = mapLabels$color",
"),",
'size = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$size, position = 0, aesthetic = "size"),',
'linetype = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$linetype, aesthetic = "linetype"),',
Expand All @@ -180,13 +182,48 @@
"vertical" = "x = mapLabels$x, ymin = mapLabels$y, ymax = mapLabels$ymax,",
"horizontal" = "y = mapLabels$y, xmin = mapLabels$x, xmax = mapLabels$xmax,"
),
"color = mapLabels$color",
"color = mapLabels$color,",
"group = mapLabels$color",
"),",
'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 = FALSE",
") + ",
# Add lower cap to error bar
"ggplot2::geom_point(",
"data = mapData,",
"mapping = aes_string(",
switch(direction,
"vertical" = "x = mapLabels$x, y = mapLabels$ymin,",
"horizontal" = "y = mapLabels$y, x = mapLabels$xmin,"
),
"color = mapLabels$color,",
"group = mapLabels$color",
"),",
'size = tlfEnv$defaultErrorbarCapSize,',
'shape = ', switch(direction, "vertical" = '"_"', "horizontal" = '"|"'), ',',
'alpha = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, aesthetic = "alpha"),',
"na.rm = TRUE,",
"show.legend = FALSE",
") + ",
# Add upper cap to error bar
"ggplot2::geom_point(",
"data = mapData,",
"mapping = aes_string(",
switch(direction,
"vertical" = "x = mapLabels$x, y = mapLabels$ymax,",
"horizontal" = "y = mapLabels$y, x = mapLabels$xmax,"
),
"color = mapLabels$color,",
"group = mapLabels$color",
"),",
'size = tlfEnv$defaultErrorbarCapSize,',
'shape = ', switch(direction, "vertical" = '"_"', "horizontal" = '"|"'), ',',
'alpha = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, aesthetic = "alpha"),',
"na.rm = TRUE,",
"show.legend = FALSE",
")"
))
}
101 changes: 57 additions & 44 deletions R/atom-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,9 @@ addRibbon <- function(data = NULL,
#'
#' @inheritParams addRibbon
#' @inheritParams addScatter
#' @param includeCap Logical defining if errorbars include caps at their ends.
#' @param capSize Numeric extent of the error bars caps
#' Caution the value corresponds to the ratio of the mean spacing between plotted error bars.
#' For instance, an `extent` of `1` will fill the caps until the next error bar
#' @return A `ggplot` object
#' @references For examples, see:
#' <https://www.open-systems-pharmacology.org/TLF-Library/articles/atom-plots.html>
Expand Down Expand Up @@ -536,12 +538,12 @@ addRibbon <- function(data = NULL,
#' addErrorbar(data = errorbarData, caption = "My errorbar plot")
#'
#' # Add a errorbar with specific properties
#' addErrorbar(data = errorbarData, color = "blue", size = 0.5, includeCap = TRUE, caption = "My data")
#' addErrorbar(data = errorbarData, color = "blue", size = 0.5, caption = "My data")
#'
#' # Add a errorbar with specific properties
#' p <- addErrorbar(
#' data = errorbarData,
#' color = "blue", size = 0.5, includeCap = TRUE, caption = "My data"
#' color = "blue", size = 0.5, caption = "My data"
#' )
#' addScatter(
#' x = time, y = cos(time),
Expand All @@ -558,15 +560,13 @@ addErrorbar <- function(data = NULL,
color = NULL,
size = NULL,
linetype = NULL,
includeCap = FALSE,
capSize = NULL,
dataMapping = NULL,
plotConfiguration = NULL,
plotObject = NULL) {
validateIsOfType(dataMapping, c("RangeDataMapping", "ObservedDataMapping"), nullAllowed = TRUE)
validateIsOfType(plotConfiguration, PlotConfiguration, nullAllowed = TRUE)
validateIsLogical(includeCap)
# validateIsIncluded(barLinetype, Linetypes, nullAllowed = TRUE)


# If data is not input, creates data and its mapping from x, ymin and ymax input
if (isEmpty(data)) {
data <- as.data.frame(cbind(x = x, ymin = ymin %||% 0, ymax = ymax %||% 0))
Expand Down Expand Up @@ -600,44 +600,57 @@ addErrorbar <- function(data = NULL,
}
mapData$legendLabels <- caption %||% mapData$legendLabels
legendLength <- length(unique(mapData$legendLabels))

# Option caps allows to add an horizontal bar at the edges of the error bars
if (includeCap) {
plotObject <- plotObject +
ggplot2::geom_errorbar(
data = mapData,
mapping = ggplot2::aes_string(
x = mapLabels$x,
ymin = mapLabels$ymin,
ymax = mapLabels$ymax,
group = "legendLabels"
),
na.rm = TRUE,
show.legend = FALSE,
size = size %||% .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$size, aesthetic = "size"),
linetype = linetype %||% .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$linetype, aesthetic = "linetype"),
alpha = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, aesthetic = "alpha"),
color = color %||% .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$color, aesthetic = "color")
)
}
if (!includeCap) {
plotObject <- plotObject +
ggplot2::geom_linerange(
data = mapData,
mapping = ggplot2::aes_string(
x = mapLabels$x,
ymin = mapLabels$ymin,
ymax = mapLabels$ymax,
group = "legendLabels"

eval(.parseVariableToObject("plotObject$plotConfiguration$errorbars", c("color", "size", "linetype"), keepIfNull = TRUE))

plotObject <- plotObject +
ggplot2::geom_linerange(
data = mapData,
mapping = aes_string(
x = mapLabels$x,
ymin = mapLabels$ymin,
ymax = mapLabels$ymax,
color = mapLabels$color,
group = mapLabels$color
),
na.rm = TRUE,
show.legend = FALSE,
size = size %||% .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$size, aesthetic = "size"),
linetype = linetype %||% .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$linetype, aesthetic = "linetype"),
alpha = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, aesthetic = "alpha"),
color = color %||% .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$color, aesthetic = "color")
)
}
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"),
color = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$color, aesthetic = "color"),
na.rm = TRUE,
show.legend = FALSE
) +
ggplot2::geom_point(
data = mapData,
mapping = aes_string(
x = mapLabels$x,
y = mapLabels$ymin,
color = mapLabels$color,
group = mapLabels$color
),
size = capSize %||% tlfEnv$defaultErrorbarCapSize,
shape = "_",
alpha = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, aesthetic = "alpha"),
color = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$color, aesthetic = "color"),
na.rm = TRUE,
show.legend = FALSE
) +
ggplot2::geom_point(
data = mapData,
mapping = aes_string(
x = mapLabels$x,
y = mapLabels$ymax,
color = mapLabels$color,
group = mapLabels$color
),
size = capSize %||% tlfEnv$defaultErrorbarCapSize,
shape = "_",
alpha = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, aesthetic = "alpha"),
color = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$color, aesthetic = "color"),
na.rm = TRUE,
show.legend = FALSE
)

# Try is used to prevent crashes in the final plot due to ggplot2 peculiarities regarding scale functions
eval(.parseUpdateAxes())
return(plotObject)
Expand Down
32 changes: 30 additions & 2 deletions R/plot-timeprofile.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ plotTimeProfile <- function(data = NULL,
x = observedMapLabels$x,
ymin = observedMapLabels$ymin,
ymax = observedMapLabels$y,
color = observedMapLabels$color
color = observedMapLabels$color,
group = observedMapLabels$color
),
size = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$size, position = 0, aesthetic = "size"),
linetype = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$linetype, position = 0, aesthetic = "linetype"),
Expand All @@ -163,12 +164,39 @@ plotTimeProfile <- function(data = NULL,
x = observedMapLabels$x,
ymin = observedMapLabels$y,
ymax = observedMapLabels$ymax,
color = observedMapLabels$color
color = observedMapLabels$color,
group = observedMapLabels$color
),
size = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$size, position = 0, aesthetic = "size"),
linetype = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$linetype, position = 0, aesthetic = "linetype"),
alpha = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, position = 0, aesthetic = "alpha"),
show.legend = FALSE
) +
ggplot2::geom_point(
data = mapObservedData,
mapping = ggplot2::aes_string(
x = observedMapLabels$x,
y = observedMapLabels$ymin,
color = observedMapLabels$color,
group = observedMapLabels$color
),
size = tlfEnv$defaultErrorbarCapSize,
shape = "_",
alpha = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, position = 0, aesthetic = "alpha"),
show.legend = FALSE
) +
ggplot2::geom_point(
data = mapObservedData,
mapping = ggplot2::aes_string(
x = observedMapLabels$x,
y = observedMapLabels$ymax,
color = observedMapLabels$color,
group = observedMapLabels$color
),
size = tlfEnv$defaultErrorbarCapSize,
shape = "_",
alpha = .getAestheticValues(n = 1, selectionKey = plotConfiguration$errorbars$alpha, position = 0, aesthetic = "alpha"),
show.legend = FALSE
)
}
plotObject <- plotObject +
Expand Down
13 changes: 13 additions & 0 deletions R/tlf-env.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,16 @@ setDefaultLogTicks <- function(ticks) {
tlfEnv$logTicks <- ticks
return(invisible())
}

# No cap displayed in the default settings
tlfEnv$defaultErrorbarCapSize <- 0

#' @title setDefaultErrorbarCapSize
#' @description Set default cap size of error bars
#' @param size A numeric defining the size of the error bar caps in pts
#' @export
setDefaultErrorbarCapSize <- function(size) {
validateIsNumeric(size)
tlfEnv$defaultErrorbarCapSize <- size
return(invisible())
}
10 changes: 6 additions & 4 deletions man/addErrorbar.Rd

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

3 changes: 2 additions & 1 deletion man/dot-parseAddUncertaintyLayer.Rd

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

14 changes: 14 additions & 0 deletions man/setDefaultErrorbarCapSize.Rd

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

4 changes: 2 additions & 2 deletions vignettes/atom-plots.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ addErrorbar(
)
```

Among its optional inputs, `addErrorbar` proposes `includeCap` a logical that defines if caps are included at the extremities of the error bars.
Among its optional inputs, `addErrorbar` proposes `capSize` a numeric that defines the size of the extremities of the error bars (caps).

```{r addErrorbar optional inputs}
addErrorbar(
Expand All @@ -353,7 +353,7 @@ addErrorbar(
ymax = cosData$cos,
color = "firebrick",
linetype = Linetypes$solid,
includeCap = TRUE,
capSize = 5,
size = 0.5,
caption = "error bar plot"
)
Expand Down

0 comments on commit 9640f54

Please sign in to comment.