From 33906004b699464ec0ef0fb73a4ebf1edd0cb6f9 Mon Sep 17 00:00:00 2001 From: pchelle Date: Wed, 10 Apr 2024 06:15:18 -0400 Subject: [PATCH] Fixes #1210 x ticks and labels are aligned --- R/utilities-plots.R | 42 ++++++++++++++++++++++++++++++++- R/utilities-pop-pk-parameters.R | 12 +++++----- man/alignXTicks.Rd | 19 +++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 man/alignXTicks.Rd diff --git a/R/utilities-plots.R b/R/utilities-plots.R index 563286da..3cc697dc 100644 --- a/R/utilities-plots.R +++ b/R/utilities-plots.R @@ -711,8 +711,9 @@ getBoxWhiskerPlotConfiguration <- function(plotScale = "log", ) # Remove xlabel plotConfiguration$labels$xlabel$text <- NULL - # Default angle for xticklabels is 45 degrees + # Default angle for x-ticklabels is 45 degrees right aligned plotConfiguration$xAxis$font$angle <- 45 + plotConfiguration$xAxis$font$align <- tlf::Alignments$right # No need for legend for boxplots plotConfiguration$legend$position <- tlf::LegendPositions$none # Color groups @@ -739,6 +740,45 @@ getBoxWhiskerPlotConfiguration <- function(plotScale = "log", return(plotConfiguration) } +#' @title alignXTicks +#' @description +#' Use the `plotConfiguration` of a `plotObject` +#' to check and perform vertical alignment of x-axis tick labels +#' @param plotObject A `ggplot` object +#' @return A `ggplot` object +#' @keywords internal +#' @import ggplot2 +alignXTicks <- function(plotObject) { + # vertical alignment required only for 45 degrees right aligned x-axis labels + requireAlignment <- all( + isIncluded( + plotObject$plotConfiguration$xAxis$font$align, + tlf::Alignments$right + ), + plotObject$plotConfiguration$xAxis$font$angle %in% 45 + ) + if (!requireAlignment) { + return(plotObject) + } + xAxisFont <- plotObject$plotConfiguration$xAxis$font + plotObject <- plotObject + ggplot2::theme( + axis.text.x = ggplot2::element_text( + colour = xAxisFont$color, + size = xAxisFont$size, + face = xAxisFont$fontFace, + family = tlf:::.checkPlotFontFamily(xAxisFont$fontFamily), + hjust = switch(xAxisFont$align, + left = 0, + center = 0.5, + right = 1 + ), + vjust = 1 + ) + ) + return(plotObject) +} + + #' @title getColorFromOutputGroup #' @description Get the appropriate colors from an output group #' @param group A data.frame mapping properties to output groups diff --git a/R/utilities-pop-pk-parameters.R b/R/utilities-pop-pk-parameters.R index ebf58359..46b0feed 100644 --- a/R/utilities-pop-pk-parameters.R +++ b/R/utilities-pop-pk-parameters.R @@ -152,7 +152,7 @@ plotPopulationPKParameters <- function(structureSets, resultID <- defaultFileNames$resultID(length(pkParametersResults) + 1, "pk_parameters", pkParameter$pkParameter, "log") pkParametersResults[[resultID]] <- saveTaskResults( id = resultID, - plot = pkParameterBoxplotLog, + plot = alignXTicks(pkParameterBoxplotLog), plotCaption = captions$plotPKParameters$boxplot( parameterCaption, output$displayName, @@ -179,7 +179,7 @@ plotPopulationPKParameters <- function(structureSets, resultID <- defaultFileNames$resultID(length(pkParametersResults) + 1, "pk_parameters", pkParameter$pkParameter) pkParametersResults[[resultID]] <- saveTaskResults( id = resultID, - plot = pkParameterBoxplot, + plot = alignXTicks(pkParameterBoxplot), plotCaption = captions$plotPKParameters$boxplot( parameterCaption, output$displayName, @@ -404,7 +404,7 @@ plotPopulationPKParameters <- function(structureSets, resultID <- defaultFileNames$resultID(length(pkParametersResults) + 1, "pk_parameters", pkParameter$pkParameter, "log") pkParametersResults[[resultID]] <- saveTaskResults( id = resultID, - plot = boxplotPKRatiosLog, + plot = alignXTicks(boxplotPKRatiosLog), plotCaption = captions$plotPKParameters$relativeChangePlot( parameterCaption, output$displayName, @@ -420,7 +420,7 @@ plotPopulationPKParameters <- function(structureSets, resultID <- defaultFileNames$resultID(length(pkParametersResults) + 1, "pk_parameters", pkParameter$pkParameter) pkParametersResults[[resultID]] <- saveTaskResults( id = resultID, - plot = boxplotPKRatios, + plot = alignXTicks(boxplotPKRatios), plotCaption = captions$plotPKParameters$relativeChangePlot( parameterCaption, output$displayName, @@ -492,7 +492,7 @@ plotPopulationPKParameters <- function(structureSets, resultID <- defaultFileNames$resultID(length(pkParametersResults) + 1, "pk_ratios", pkParameter$pkParameter, "log") pkParametersResults[[resultID]] <- saveTaskResults( id = resultID, - plot = boxplotPKRatiosLog, + plot = alignXTicks(boxplotPKRatiosLog), plotCaption = captions$plotPKParameters$ratioPlot( parameterCaption, output$displayName, @@ -508,7 +508,7 @@ plotPopulationPKParameters <- function(structureSets, resultID <- defaultFileNames$resultID(length(pkParametersResults) + 1, "pk_ratios", pkParameter$pkParameter) pkParametersResults[[resultID]] <- saveTaskResults( id = resultID, - plot = boxplotPKRatios, + plot = alignXTicks(boxplotPKRatios), plotCaption = captions$plotPKParameters$ratioPlot( parameterCaption, output$displayName, diff --git a/man/alignXTicks.Rd b/man/alignXTicks.Rd new file mode 100644 index 00000000..7ea13365 --- /dev/null +++ b/man/alignXTicks.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utilities-plots.R +\name{alignXTicks} +\alias{alignXTicks} +\title{alignXTicks} +\usage{ +alignXTicks(plotObject) +} +\arguments{ +\item{plotObject}{A `ggplot` object} +} +\value{ +A `ggplot` object +} +\description{ +Use the `plotConfiguration` of a `plotObject` +to check and perform vertical alignment of x-axis tick labels +} +\keyword{internal}