Skip to content

Commit

Permalink
Fixes Open-Systems-Pharmacology#356 display minor ticks (Open-Systems…
Browse files Browse the repository at this point in the history
…-Pharmacology#357)

* Fixes Open-Systems-Pharmacology#356 display minor ticks

* fix test

Co-authored-by: Indrajeet Patil <patilindrajeet.science@gmail.com>
  • Loading branch information
2 people authored and Yuri05 committed Jan 27, 2023
1 parent a9dc0d7 commit 47a5e69
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 15 deletions.
29 changes: 29 additions & 0 deletions R/plotconfiguration-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ AxisConfiguration <- R6::R6Class(
#' Use enum `Scaling` to access predefined scales.
#' @param ticks numeric vector or function defining where to position axis ticks
#' @param ticklabels character vector or function defining what to print on axis ticks
#' @param minorTicks numeric vector or function defining where to position minor axis ticks
#' @param font `Font` object defining the font of ticklabels
#' @param expand logical defining if data is expanded until axis.
#' If `TRUE`, data is expanded until axis
Expand All @@ -88,6 +89,7 @@ AxisConfiguration <- R6::R6Class(
scale = Scaling$lin,
ticks = NULL,
ticklabels = NULL,
minorTicks = NULL,
font = NULL,
expand = FALSE) {
validateIsNumeric(limits, nullAllowed = TRUE)
Expand All @@ -99,6 +101,7 @@ AxisConfiguration <- R6::R6Class(
private$.scale <- .createPlotScale(scale)
private$.ticks <- .createPlotTicks(ticks)
private$.ticklabels <- .createPlotTickLabels(ticklabels)
private$.minorTicks <- .createPlotTicks(minorTicks)
private$.expand <- expand

# Default axis font will use theme
Expand Down Expand Up @@ -147,6 +150,21 @@ AxisConfiguration <- R6::R6Class(
private$.ticks
)
},

#' @description Get tick values for pretty default log plots
#' @return User defined tick values or tlf default ticks
prettyMinorTicks = function() {
# A waiver is a ggplot2 "flag" object, similar to NULL,
# that indicates the calling function should just use the default value
if (!isOfType(private$.minorTicks, "waiver")) {
return(private$.minorTicks)
}
# Default tick values as a function of scale
if(isIncluded(private$.scale, Scaling$log)){
return(tlfEnv$logMinorTicks)
}
return(private$.minorTicks)
},

#' @description Get tick labels for pretty default log plots
#' @return User defined tick labels or tlf default ticklabels
Expand Down Expand Up @@ -200,6 +218,14 @@ AxisConfiguration <- R6::R6Class(
private$.ticks <- .createPlotTicks(value)
return(invisible())
},
#' @field minorTicks function or values defining where axis minor ticks are placed
minorTicks = function(value) {
if (missing(value)) {
return(private$.minorTicks)
}
private$.minorTicks <- .createPlotTicks(value)
return(invisible())
},
#' @field ticklabels function or values defining the axis tick labels
ticklabels = function(value) {
if (missing(value)) {
Expand Down Expand Up @@ -243,6 +269,7 @@ AxisConfiguration <- R6::R6Class(
.scale = NULL,
.ticks = NULL,
.ticklabels = NULL,
.minorTicks = NULL,
.font = NULL,
.expand = NULL
)
Expand Down Expand Up @@ -286,6 +313,7 @@ XAxisConfiguration <- R6::R6Class(
ggplot2::scale_x_continuous(
trans = self$ggplotScale(),
breaks = self$prettyTicks(),
minor_breaks = self$prettyMinorTicks(),
labels = self$prettyTickLabels(),
expand = self$ggplotExpansion(),
oob = .removeInfiniteValues
Expand Down Expand Up @@ -344,6 +372,7 @@ YAxisConfiguration <- R6::R6Class(
ggplot2::scale_y_continuous(
trans = self$ggplotScale(),
breaks = self$prettyTicks(),
minor_breaks = self$prettyMinorTicks(),
labels = self$prettyTickLabels(),
expand = self$ggplotExpansion(),
oob = .removeInfiniteValues
Expand Down
5 changes: 4 additions & 1 deletion R/plotconfiguration-background.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ BackgroundConfiguration <- R6::R6Class(
axis.line.x = private$.xAxis$createPlotElement(),
axis.line.y = private$.yAxis$createPlotElement(),
panel.grid.major.x = private$.xGrid$createPlotElement(),
panel.grid.major.y = private$.yGrid$createPlotElement()
panel.grid.major.y = private$.yGrid$createPlotElement(),
# Minor grid is same as Major grid but less thick
panel.grid.minor.x = private$.xGrid$createPlotElement(size = as.numeric(private$.xGrid$size)/2),
panel.grid.minor.y = private$.yGrid$createPlotElement(size = as.numeric(private$.yGrid$size)/2)
)
return(plotObject)
}
Expand Down
6 changes: 4 additions & 2 deletions R/tlf-env.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ setDefaultWatermark <- function(watermark = NULL) {
return(invisible())
}

tlfEnv$logTicks <- 10^seq(-6, 6)
tlfEnv$lnTicks <- exp(seq(-6, 6))
tlfEnv$logTicks <- 10^seq(-10, 10)
tlfEnv$lnTicks <- exp(seq(-10, 10))
# Log minor ticks for every integer
tlfEnv$logMinorTicks <- rep(seq(1,9), 21)*rep(10^seq(-10, 10), each = 9)

#' @title setDefaultLogTicks
#' @description Set default values for log ticks
Expand Down
7 changes: 5 additions & 2 deletions R/utilities-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @param limits Optional numeric values of axis limits
#' @param ticks Optional values or function for axis ticks
#' @param ticklabels Optional values or function for axis ticklabels
#' @param minorTicks Optional values or function for axis minor ticks
#' @param font A `Font` object defining font of ticklabels
#' @param expand Logical defining if data is expanded until axis
#' @return A `ggplot` object
Expand All @@ -28,6 +29,7 @@ setXAxis <- function(plotObject,
limits = NULL,
ticks = NULL,
ticklabels = NULL,
minorTicks = NULL,
font = NULL,
expand = NULL) {
validateIsOfType(plotObject, "ggplot")
Expand All @@ -43,7 +45,7 @@ setXAxis <- function(plotObject,

# R6 class not cloned will spread modifications into newPlotObject$plotConfiguration$xAxis
xAxis <- newPlotObject$plotConfiguration$xAxis
eval(.parseVariableToObject("xAxis", c("limits", "scale", "ticks", "ticklabels", "font", "expand"), keepIfNull = TRUE))
eval(.parseVariableToObject("xAxis", c("limits", "scale", "ticks", "ticklabels", "minorTicks", "font", "expand"), keepIfNull = TRUE))
newPlotObject <- xAxis$updatePlot(newPlotObject, ylim = newPlotObject$plotConfiguration$yAxis$limits)
return(newPlotObject)
}
Expand Down Expand Up @@ -72,6 +74,7 @@ setYAxis <- function(plotObject,
limits = NULL,
ticks = NULL,
ticklabels = NULL,
minorTicks = NULL,
font = NULL,
expand = NULL) {
validateIsOfType(plotObject, "ggplot")
Expand All @@ -87,7 +90,7 @@ setYAxis <- function(plotObject,

# R6 class not cloned will spread modifications into newPlotObject$plotConfiguration$yAxis
yAxis <- newPlotObject$plotConfiguration$yAxis
eval(.parseVariableToObject("yAxis", c("limits", "scale", "ticks", "ticklabels", "font", "expand"), keepIfNull = TRUE))
eval(.parseVariableToObject("yAxis", c("limits", "scale", "ticks", "ticklabels", "minorTicks", "font", "expand"), keepIfNull = TRUE))
newPlotObject <- yAxis$updatePlot(newPlotObject, xlim = newPlotObject$plotConfiguration$xAxis$limits)
return(newPlotObject)
}
Expand Down
19 changes: 19 additions & 0 deletions man/AxisConfiguration.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/XAxisConfiguration.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/YAxisConfiguration.Rd

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

3 changes: 3 additions & 0 deletions man/setXAxis.Rd

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

3 changes: 3 additions & 0 deletions man/setYAxis.Rd

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 47a5e69

Please sign in to comment.