Skip to content

Commit

Permalink
Fixes Open-Systems-Pharmacology#304 add enum helper for ticklabels
Browse files Browse the repository at this point in the history
  • Loading branch information
pchelle committed Jun 13, 2022
1 parent 8a57cdc commit 1c28cc3
Show file tree
Hide file tree
Showing 25 changed files with 223 additions and 21 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export(ThemeAestheticSelections)
export(ThemeBackground)
export(ThemeFont)
export(ThemePlotConfigurations)
export(TickLabelTransforms)
export(TimeProfileDataMapping)
export(TimeProfilePlotConfiguration)
export(TornadoDataMapping)
Expand All @@ -98,6 +99,7 @@ export(exportPlot)
export(exportPlotConfigurationCode)
export(getBoxWhiskerMeasure)
export(getDefaultCaptions)
export(getGreekTickLabels)
export(getGuestValues)
export(getGuestValuesFromDataMapping)
export(getLabelWithUnit)
Expand All @@ -107,6 +109,7 @@ export(getLinesFromFoldDistance)
export(getLnTickLabels)
export(getLogTickLabels)
export(getPKRatioMeasure)
export(getPiTickLabels)
export(getSqrtTickLabels)
export(initializePlot)
export(loadThemeFromJson)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `HorizontalJustification` (horizontal justifications for plot annotation text) (#293)
- `VerticalJustification` (vertical justifications for plot annotation text) (#293)
- `PlotAnnotationTextSize` (default text sizes for plot annotations) (#293)
- `TickLabelTransforms` (predefined tick labeling) (#304)

## Minor improvements and bug fixes

Expand Down
30 changes: 28 additions & 2 deletions R/plotconfiguration-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ createPlotTicks <- function(ticks) {
return(ticks)
}

#' @title createPlotTickLabels
#' @description Translate ticks and ticklabels into a value directly usable by `ggplot2`
#' to give more flexibilty in the next functions
#' @param ticklabels character, numeric or function defining the ticks
#' @return name of the `ggplot2` scale
#' @keywords internal
createPlotTickLabels <- function(ticklabels) {
if (isEmpty(ticklabels)) {
return(waiver())
}
if (isIncluded(ticklabels, TickLabelTransforms)) {
transformedLabels <- switch(ticklabels,
"default" = waiver(),
"none" = NULL,
"identity" = identity,
"log" = getLogTickLabels,
"ln" = getLnTickLabels,
"sqrt" = getSqrtTickLabels,
"greek" = getGreekTickLabels,
"pi" = getPiTickLabels
)
return(transformedLabels)
}
return(ticklabels)
}

#' @title AxisConfiguration
#' @description R6 class defining the configuration of axis
#' @export
Expand Down Expand Up @@ -69,7 +95,7 @@ AxisConfiguration <- R6::R6Class(
scale <- scale %||% Scaling$lin
private$.scale <- createPlotScale(scale)
private$.ticks <- createPlotTicks(ticks)
private$.ticklabels <- createPlotTicks(ticklabels)
private$.ticklabels <- createPlotTickLabels(ticklabels)
private$.expand <- expand

# Default axis font will use theme
Expand Down Expand Up @@ -176,7 +202,7 @@ AxisConfiguration <- R6::R6Class(
if (missing(value)) {
return(private$.ticklabels)
}
private$.ticklabels <- createPlotTicks(value)
private$.ticklabels <- createPlotTickLabels(value)
return(invisible())
},
#' @field font `Font` object defining the font of the ticklabels
Expand Down
55 changes: 55 additions & 0 deletions R/utilities-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,58 @@ getSqrtTickLabels <- function(ticks) {
sqrtValues <- ticks^2
return(parse(text = paste("sqrt(", sqrtValues, ")", sep = "")))
}

#' @title getGreekTickLabels
#' @description Get ticklabels expressions for discrete scale plots with greek letters
#' @param ticks numeric values of the ticks
#' @return Expressions to use in `ticklabels` input parameter of `setXAxis` and `setYAxis` functions
#' @examples
#' ticks <- c(1, 5, 10, 50, 100, 500))
#' getGreekTickLabels(ticks)
#' @export
getGreekTickLabels <- function(ticks) {
# alpha starts at converted integer 945
if(is.numeric(ticks)){
return(sapply(ticks, function(tick){intToUtf8(tick + 944)}))
}
tickLabels <- sapply(1:length(ticks), function(tick){intToUtf8(tick + 944)})
return(tickLabels)
}

#' @title getPiTickLabels
#' @description Get ticklabels expressions for plots with values as ratios of Pi
#' @param ticks numeric values of the ticks
#' @return Expressions to use in `ticklabels` input parameter of `setXAxis` and `setYAxis` functions
#' @examples
#' ticks <- seq(0, 2*pi, pi/2)
#' getPiTickLabels(ticks)
#' @export
getPiTickLabels <- function(ticks) {
# Get fractions of pi from ticks
roundPi <- as.character(ticks %/% pi)
# Remove 1 and -1 from expression
roundPi[roundPi=="1"] <- ""
roundPi[roundPi=="-1"] <- "-"
# Flag when 0 to remove pi from label
roundPi[roundPi=="0"] <- "x"

roundPi <- paste(roundPi, "\u03C0", sep = "")
roundPi[grepl("x", roundPi)] <- ""

# Round to 3 digits to get fraction values
# If fraction is recognized, used fraction format
decPi <- round((ticks %% pi)/pi, 3)
decPi <- sapply(decPi, function(piFraction){
if(piFraction==0){return("")}
if(piFraction==0.167){return(" + \u03C0/6")}
if(piFraction==0.25){return(" + \u03C0/4")}
if(piFraction==0.333){return(" + \u03C0/3")}
if(piFraction==0.5){return(" + \u03C0/2")}
if(piFraction==0.667){return(" + 2\u03C0/3")}
if(piFraction==0.667){return(" + 5\u03C0/6")}
return(paste0("+", piFraction, "\u03C0"))
})
piLabels <- paste(roundPi, decPi, sep = "")
piLabels[piLabels==""] <- "0"
return(piLabels)
}
8 changes: 8 additions & 0 deletions R/utilities-enums.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,11 @@ ExportUnits <- enum(c("cm", "in", "mm", "px"))
#' List of all available formats to export a ggplot object
#' @family enum helpers
ExportFormats <- enum(c("png", "pdf", "eps", "ps", "tex", "jpeg", "tiff", "bmp", "svg", "wmf"))

#' @title TickLabelTransforms
#' @import ospsuite.utils
#' @export
#' @description
#' List of all available tick label transformation names
#' @family enum helpers
TickLabelTransforms <- enum(c("none", "default", "identity", "log", "ln", "sqrt", "greek", "pi"))
1 change: 1 addition & 0 deletions man/AestheticProperties.Rd

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

1 change: 1 addition & 0 deletions man/Alignments.Rd

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

36 changes: 18 additions & 18 deletions man/ExportConfiguration.Rd

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

1 change: 1 addition & 0 deletions man/ExportFormats.Rd

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

1 change: 1 addition & 0 deletions man/ExportUnits.Rd

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

1 change: 1 addition & 0 deletions man/FontFaces.Rd

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

1 change: 1 addition & 0 deletions man/HorizontalJustification.Rd

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

1 change: 1 addition & 0 deletions man/LegendPositions.Rd

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

1 change: 1 addition & 0 deletions man/Linetypes.Rd

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

1 change: 1 addition & 0 deletions man/PlotAnnotationTextSize.Rd

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

1 change: 1 addition & 0 deletions man/Scaling.Rd

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

1 change: 1 addition & 0 deletions man/Shapes.Rd

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

1 change: 1 addition & 0 deletions man/TagPositions.Rd

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

34 changes: 34 additions & 0 deletions man/TickLabelTransforms.Rd

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

1 change: 1 addition & 0 deletions man/VerticalJustification.Rd

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

19 changes: 19 additions & 0 deletions man/createPlotTickLabels.Rd

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

Loading

0 comments on commit 1c28cc3

Please sign in to comment.