From 6523c99b85fb6b70e975694dc0675ac10656723f Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Wed, 24 Nov 2021 13:21:36 +0100 Subject: [PATCH] Check for range == NULL - happens if no data has been added to plot --- NEWS.md | 3 +++ R/axis-secondary.R | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/NEWS.md b/NEWS.md index f8fc338eda..708ec55d62 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* Fixed a bug throwing errors when trying to render an empty plot with secondary + axes (@thomasp85, #4509) + * Axes are now added correctly in `facet_wrap()` when `as.table = FALSE` (@thomasp85, #4553) diff --git a/R/axis-secondary.R b/R/axis-secondary.R index 28cd8ece2d..e25e21fca9 100644 --- a/R/axis-secondary.R +++ b/R/axis-secondary.R @@ -174,6 +174,12 @@ AxisSecondary <- ggproto("AxisSecondary", NULL, mono_test = function(self, scale){ range <- scale$range$range + + # Check if plot is empty + if (is.null(range)) { + return() + } + along_range <- seq(range[1], range[2], length.out = self$detail) old_range <- scale$trans$inverse(along_range)