Skip to content

Commit

Permalink
Fixes Open-Systems-Pharmacology#151 use coord_cartesian
Browse files Browse the repository at this point in the history
- `coord_trans()` can't be used for the transformation because the method adding the watermark in background. Consequently, log and discrete transformations still use scale_x/y... 
- For the same reason, `coord_cartesian()` is used to set the axis limits
- suppressMessage is needed for the same reason as scale: multiple coord systems or scales lead to ggplot unwanted messages
  • Loading branch information
pchelle committed Jul 26, 2021
1 parent c30a4d4 commit 4657caf
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions R/plotconfiguration-axis.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,23 @@ XAxisConfiguration <- R6::R6Class(
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)
)
# Update scales and ticks
if (isIncluded(private$.scale, Scaling$discrete)) {
suppressMessages(
plotObject <- plotObject +
ggplot2::scale_x_discrete(limits = private$.limits, breaks = private$.ticks, labels = private$.ticklabels)
ggplot2::scale_x_discrete(breaks = private$.ticks, labels = private$.ticklabels)
)
return(plotObject)
}
# Most of ggplot2 scales lead to unwanted warning messages
# `try` should be added in cases of scale breaking because all the ggplot object elements are not yet in place
suppressMessages(
plotObject <- plotObject +
ggplot2::scale_x_continuous(trans = private$.scale, limits = private$.limits, breaks = private$.ticks, labels = private$.ticklabels)
ggplot2::scale_x_continuous(trans = private$.scale, breaks = private$.ticks, labels = private$.ticklabels)
)
return(plotObject)
}
Expand All @@ -207,19 +211,22 @@ YAxisConfiguration <- R6::R6Class(
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)
)
# Update scales and ticks
if (isIncluded(private$.scale, Scaling$discrete)) {
suppressMessages(
plotObject <- plotObject +
ggplot2::scale_y_discrete(limits = private$.limits, breaks = private$.ticks, labels = private$.ticklabels)
ggplot2::scale_y_discrete(breaks = private$.ticks, labels = private$.ticklabels)
)
return(plotObject)
}
# Most of ggplot2 scales lead to unwanted warning messages
# `try` should be added in cases of scale breaking because all the ggplot object elements are not yet in place
suppressMessages(
plotObject <- plotObject +
ggplot2::scale_y_continuous(trans = private$.scale, limits = private$.limits, breaks = private$.ticks, labels = private$.ticklabels)
ggplot2::scale_y_continuous(trans = private$.scale, breaks = private$.ticks, labels = private$.ticklabels)
)
return(plotObject)
}
Expand Down

0 comments on commit 4657caf

Please sign in to comment.