diff --git a/NAMESPACE b/NAMESPACE index 65258c9f..1809da12 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -78,6 +78,7 @@ export(ThemeAestheticSelections) export(ThemeBackground) export(ThemeFont) export(ThemePlotConfigurations) +export(TickLabelTransforms) export(TimeProfileDataMapping) export(TimeProfilePlotConfiguration) export(TornadoDataMapping) @@ -98,6 +99,7 @@ export(exportPlot) export(exportPlotConfigurationCode) export(getBoxWhiskerMeasure) export(getDefaultCaptions) +export(getGreekTickLabels) export(getGuestValues) export(getGuestValuesFromDataMapping) export(getLabelWithUnit) @@ -107,6 +109,7 @@ export(getLinesFromFoldDistance) export(getLnTickLabels) export(getLogTickLabels) export(getPKRatioMeasure) +export(getPiTickLabels) export(getSqrtTickLabels) export(initializePlot) export(loadThemeFromJson) diff --git a/NEWS.md b/NEWS.md index 9d22620a..f9e8a516 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/plotconfiguration-axis.R b/R/plotconfiguration-axis.R index 07873041..075ad4a1 100644 --- a/R/plotconfiguration-axis.R +++ b/R/plotconfiguration-axis.R @@ -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 @@ -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 @@ -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 diff --git a/R/utilities-axis.R b/R/utilities-axis.R index 15834e35..464272aa 100644 --- a/R/utilities-axis.R +++ b/R/utilities-axis.R @@ -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) +} diff --git a/R/utilities-enums.R b/R/utilities-enums.R index f0a081ca..6846008e 100644 --- a/R/utilities-enums.R +++ b/R/utilities-enums.R @@ -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")) diff --git a/man/AestheticProperties.Rd b/man/AestheticProperties.Rd index 5eaa3adc..67b3a454 100644 --- a/man/AestheticProperties.Rd +++ b/man/AestheticProperties.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/Alignments.Rd b/man/Alignments.Rd index ba85dda5..266ad2d2 100644 --- a/man/Alignments.Rd +++ b/man/Alignments.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/ExportConfiguration.Rd b/man/ExportConfiguration.Rd index bf2169ae..685e9920 100644 --- a/man/ExportConfiguration.Rd +++ b/man/ExportConfiguration.Rd @@ -29,17 +29,17 @@ combined to create the fully qualified file name. Defaults to the working direct \section{Methods}{ \subsection{Public methods}{ \itemize{ -\item \href{#method-ExportConfiguration-new}{\code{ExportConfiguration$new()}} -\item \href{#method-ExportConfiguration-print}{\code{ExportConfiguration$print()}} -\item \href{#method-ExportConfiguration-getFileName}{\code{ExportConfiguration$getFileName()}} -\item \href{#method-ExportConfiguration-savePlot}{\code{ExportConfiguration$savePlot()}} -\item \href{#method-ExportConfiguration-convertPixels}{\code{ExportConfiguration$convertPixels()}} -\item \href{#method-ExportConfiguration-clone}{\code{ExportConfiguration$clone()}} +\item \href{#method-new}{\code{ExportConfiguration$new()}} +\item \href{#method-print}{\code{ExportConfiguration$print()}} +\item \href{#method-getFileName}{\code{ExportConfiguration$getFileName()}} +\item \href{#method-savePlot}{\code{ExportConfiguration$savePlot()}} +\item \href{#method-convertPixels}{\code{ExportConfiguration$convertPixels()}} +\item \href{#method-clone}{\code{ExportConfiguration$clone()}} } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-ExportConfiguration-new}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-new}{}}} \subsection{Method \code{new()}}{ Create a new \code{ExportConfiguration} object \subsection{Usage}{ @@ -79,8 +79,8 @@ A new \code{ExportConfiguration} object } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-ExportConfiguration-print}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-print}{}}} \subsection{Method \code{print()}}{ Print properties of export configuration \subsection{Usage}{ @@ -92,8 +92,8 @@ Export configuration properties } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-ExportConfiguration-getFileName}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-getFileName}{}}} \subsection{Method \code{getFileName()}}{ Print the default exported file name from the export configuration \subsection{Usage}{ @@ -105,8 +105,8 @@ Default file name } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-ExportConfiguration-savePlot}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-savePlot}{}}} \subsection{Method \code{savePlot()}}{ Save/Export a plot \subsection{Usage}{ @@ -127,8 +127,8 @@ The file name of the exported plot } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-ExportConfiguration-convertPixels}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-convertPixels}{}}} \subsection{Method \code{convertPixels()}}{ If unit is in pixels, convert all export dimensions to inches to keep compatibility with older versions of ggplot2 \subsection{Usage}{ @@ -137,8 +137,8 @@ If unit is in pixels, convert all export dimensions to inches to keep compatibil } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-ExportConfiguration-clone}{}}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-clone}{}}} \subsection{Method \code{clone()}}{ The objects of this class are cloneable with this method. \subsection{Usage}{ diff --git a/man/ExportFormats.Rd b/man/ExportFormats.Rd index 3925f8f1..ef95227b 100644 --- a/man/ExportFormats.Rd +++ b/man/ExportFormats.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/ExportUnits.Rd b/man/ExportUnits.Rd index b05382b8..2d3601ac 100644 --- a/man/ExportUnits.Rd +++ b/man/ExportUnits.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/FontFaces.Rd b/man/FontFaces.Rd index 6697a912..004b6ea6 100644 --- a/man/FontFaces.Rd +++ b/man/FontFaces.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/HorizontalJustification.Rd b/man/HorizontalJustification.Rd index c5e43855..6b400166 100644 --- a/man/HorizontalJustification.Rd +++ b/man/HorizontalJustification.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/LegendPositions.Rd b/man/LegendPositions.Rd index 5718ba67..be91460a 100644 --- a/man/LegendPositions.Rd +++ b/man/LegendPositions.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/Linetypes.Rd b/man/Linetypes.Rd index b9c6bc4a..c8971add 100644 --- a/man/Linetypes.Rd +++ b/man/Linetypes.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/PlotAnnotationTextSize.Rd b/man/PlotAnnotationTextSize.Rd index 614b5703..32a829ef 100644 --- a/man/PlotAnnotationTextSize.Rd +++ b/man/PlotAnnotationTextSize.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/Scaling.Rd b/man/Scaling.Rd index 6e54788d..fd958e46 100644 --- a/man/Scaling.Rd +++ b/man/Scaling.Rd @@ -55,6 +55,7 @@ Other enum helpers: \code{\link{PlotAnnotationTextSize}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/Shapes.Rd b/man/Shapes.Rd index a0b552d9..08cf9884 100644 --- a/man/Shapes.Rd +++ b/man/Shapes.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{PlotAnnotationTextSize}}, \code{\link{Scaling}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/TagPositions.Rd b/man/TagPositions.Rd index 8318eacf..b51cf8cd 100644 --- a/man/TagPositions.Rd +++ b/man/TagPositions.Rd @@ -26,6 +26,7 @@ Other enum helpers: \code{\link{PlotAnnotationTextSize}}, \code{\link{Scaling}}, \code{\link{Shapes}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}}, \code{\link{tlfStatFunctions}} } diff --git a/man/TickLabelTransforms.Rd b/man/TickLabelTransforms.Rd new file mode 100644 index 00000000..ea215325 --- /dev/null +++ b/man/TickLabelTransforms.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities-enums.R +\docType{data} +\name{TickLabelTransforms} +\alias{TickLabelTransforms} +\title{TickLabelTransforms} +\format{ +An object of class \code{list} of length 8. +} +\usage{ +TickLabelTransforms +} +\description{ +List of all available tick label transformation names +} +\seealso{ +Other enum helpers: +\code{\link{AestheticProperties}}, +\code{\link{Alignments}}, +\code{\link{ExportFormats}}, +\code{\link{ExportUnits}}, +\code{\link{FontFaces}}, +\code{\link{HorizontalJustification}}, +\code{\link{LegendPositions}}, +\code{\link{Linetypes}}, +\code{\link{PlotAnnotationTextSize}}, +\code{\link{Scaling}}, +\code{\link{Shapes}}, +\code{\link{TagPositions}}, +\code{\link{VerticalJustification}}, +\code{\link{tlfStatFunctions}} +} +\concept{enum helpers} +\keyword{datasets} diff --git a/man/VerticalJustification.Rd b/man/VerticalJustification.Rd index e2931b5f..00b5d6bc 100644 --- a/man/VerticalJustification.Rd +++ b/man/VerticalJustification.Rd @@ -27,6 +27,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{tlfStatFunctions}} } \concept{enum helpers} diff --git a/man/createPlotTickLabels.Rd b/man/createPlotTickLabels.Rd new file mode 100644 index 00000000..b87eb457 --- /dev/null +++ b/man/createPlotTickLabels.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plotconfiguration-axis.R +\name{createPlotTickLabels} +\alias{createPlotTickLabels} +\title{createPlotTickLabels} +\usage{ +createPlotTickLabels(ticklabels) +} +\arguments{ +\item{ticklabels}{character, numeric or function defining the ticks} +} +\value{ +name of the \code{ggplot2} scale +} +\description{ +Translate ticks and ticklabels into a value directly usable by \code{ggplot2} +to give more flexibilty in the next functions +} +\keyword{internal} diff --git a/man/getGreekTickLabels.Rd b/man/getGreekTickLabels.Rd new file mode 100644 index 00000000..630eb87d --- /dev/null +++ b/man/getGreekTickLabels.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities-axis.R +\name{getGreekTickLabels} +\alias{getGreekTickLabels} +\title{getGreekTickLabels} +\usage{ +getGreekTickLabels(ticks) +} +\arguments{ +\item{ticks}{numeric values of the ticks} +} +\value{ +Expressions to use in \code{ticklabels} input parameter of \code{setXAxis} and \code{setYAxis} functions +} +\description{ +Get ticklabels expressions for discrete scale plots with greek letters +} +\examples{ +ticks <- c(1, 5, 10, 50, 100, 500)) +getGreekTickLabels(ticks) +} diff --git a/man/getPiTickLabels.Rd b/man/getPiTickLabels.Rd new file mode 100644 index 00000000..c66b534f --- /dev/null +++ b/man/getPiTickLabels.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities-axis.R +\name{getPiTickLabels} +\alias{getPiTickLabels} +\title{getPiTickLabels} +\usage{ +getPiTickLabels(ticks) +} +\arguments{ +\item{ticks}{numeric values of the ticks} +} +\value{ +Expressions to use in \code{ticklabels} input parameter of \code{setXAxis} and \code{setYAxis} functions +} +\description{ +Get ticklabels expressions for plots with values as ratios of Pi +} +\examples{ +ticks <- seq(0, 2*pi, pi/2) +getPiTickLabels(ticks) +} diff --git a/man/tlfStatFunctions.Rd b/man/tlfStatFunctions.Rd index 23cce8ad..922c6f3b 100644 --- a/man/tlfStatFunctions.Rd +++ b/man/tlfStatFunctions.Rd @@ -28,6 +28,7 @@ Other enum helpers: \code{\link{Scaling}}, \code{\link{Shapes}}, \code{\link{TagPositions}}, +\code{\link{TickLabelTransforms}}, \code{\link{VerticalJustification}} } \concept{enum helpers} diff --git a/vignettes/plot-configuration.Rmd b/vignettes/plot-configuration.Rmd index b94007b8..3bd4a97e 100644 --- a/vignettes/plot-configuration.Rmd +++ b/vignettes/plot-configuration.Rmd @@ -464,7 +464,7 @@ setXAxis(scatter1, limits = c(0.5, 20), scale = Scaling$sqrt) setXAxis(scatter1, limits = c(0, 6 * pi), ticks = seq(0, 6 * pi, pi), - ticklabels = c("0", "pi", paste0(seq(2, 6), "*pi")), + ticklabels = TickLabelTransforms$pi, font = Font$new(color = "dodgerblue") ) ```