Skip to content

Commit

Permalink
Fixes Open-Systems-Pharmacology#186 coord_cartesian update both axis …
Browse files Browse the repository at this point in the history
…limits
  • Loading branch information
pchelle committed Nov 3, 2021
1 parent 0df22e1 commit c8666ce
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
10 changes: 6 additions & 4 deletions R/plotconfiguration-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,15 @@ XAxisConfiguration <- R6::R6Class(
public = list(
#' @description Update axis configuration on a `ggplot` object
#' @param plotObject `ggplot` object
#' @param ylim limits of `y` axis to prevent `coord_cartesian` to overwrite its properties
#' @return A `ggplot` object with updated axis properties
updatePlot = function(plotObject) {
updatePlot = function(plotObject, ylim = NULL) {
validateIsOfType(plotObject, "ggplot")
# Update font properties
plotObject <- plotObject + ggplot2::theme(axis.text.x = private$.font$createPlotFont())
# Update limits using coor_cartesian to prevent ggplot to remove data and crash
suppressMessages(
plotObject <- plotObject + ggplot2::coord_cartesian(xlim = private$.limits)
plotObject <- plotObject + ggplot2::coord_cartesian(xlim = private$.limits, ylim = ylim)
)
# Update scales and ticks
if (isIncluded(private$.scale, Scaling$discrete)) {
Expand Down Expand Up @@ -207,13 +208,14 @@ YAxisConfiguration <- R6::R6Class(

#' @description Update axis configuration on a `ggplot` object
#' @param plotObject `ggplot` object
#' @param xlim limits of `x` axis to prevent `coord_cartesian` to overwrite its properties
#' @return A `ggplot` object with updated axis properties
updatePlot = function(plotObject) {
updatePlot = function(plotObject, xlim = NULL) {
validateIsOfType(plotObject, "ggplot")
# Update font properties
plotObject <- plotObject + ggplot2::theme(axis.text.y = private$.font$createPlotFont())
suppressMessages(
plotObject <- plotObject + ggplot2::coord_cartesian(ylim = private$.limits)
plotObject <- plotObject + ggplot2::coord_cartesian(xlim = xlim, ylim = private$.limits)
)
# Update scales and ticks
if (isIncluded(private$.scale, Scaling$discrete)) {
Expand Down
4 changes: 2 additions & 2 deletions R/utilities-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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"), keepIfNull = TRUE))
newPlotObject <- xAxis$updatePlot(newPlotObject)
newPlotObject <- xAxis$updatePlot(newPlotObject, ylim = newPlotObject$plotConfiguration$yAxis$limits)
return(newPlotObject)
}

Expand Down Expand Up @@ -60,7 +60,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"), keepIfNull = TRUE))
newPlotObject <- yAxis$updatePlot(newPlotObject)
newPlotObject <- yAxis$updatePlot(newPlotObject, xlim = newPlotObject$plotConfiguration$xAxis$limits)
return(newPlotObject)
}

Expand Down
4 changes: 3 additions & 1 deletion man/XAxisConfiguration.Rd

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

4 changes: 3 additions & 1 deletion man/YAxisConfiguration.Rd

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

0 comments on commit c8666ce

Please sign in to comment.