Skip to content

Commit 9927d13

Browse files
authored
Remove warning when creating secondary axis with no minor breaks (#4439)
1 parent 46cc970 commit 9927d13

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
* Fix a bug in `stat_summary_bin()` where one more than the requested number of
1313
bins would be created (@thomasp85, #3824)
1414

15+
* Fix issue in `sec_axis()` that would throw warnings in the absence of any
16+
secondary breaks (@thomasp85, #4368)
17+
1518
* Fix a bug in `geom_abline()` that resulted in `intercept` not being subjected
1619
to the transformation of the y scale (@thomasp85, #3741)
1720

R/axis-secondary.R

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -213,30 +213,38 @@ AxisSecondary <- ggproto("AxisSecondary", NULL,
213213
range_info <- temp_scale$break_info()
214214

215215
# Map the break values back to their correct position on the primary scale
216-
old_val <- lapply(range_info$major_source, function(x) which.min(abs(full_range - x)))
217-
old_val <- old_range[unlist(old_val)]
218-
old_val_trans <- scale$trans$transform(old_val)
216+
if (!is.null(range_info$major_source)) {
217+
old_val <- lapply(range_info$major_source, function(x) which.min(abs(full_range - x)))
218+
old_val <- old_range[unlist(old_val)]
219+
old_val_trans <- scale$trans$transform(old_val)
219220

220-
old_val_minor <- lapply(range_info$minor_source, function(x) which.min(abs(full_range - x)))
221-
old_val_minor <- old_range[unlist(old_val_minor)]
222-
old_val_minor_trans <- scale$trans$transform(old_val_minor)
221+
# rescale values from 0 to 1
222+
range_info$major[] <- round(
223+
rescale(
224+
scale$map(old_val_trans, range(old_val_trans)),
225+
from = range
226+
),
227+
digits = 3
228+
)
229+
} else {
230+
old_val_trans <- NULL
231+
}
223232

224-
# rescale values from 0 to 1
225-
range_info$major[] <- round(
226-
rescale(
227-
scale$map(old_val_trans, range(old_val_trans)),
228-
from = range
229-
),
230-
digits = 3
231-
)
233+
if (!is.null(range_info$minor_source)) {
234+
old_val_minor <- lapply(range_info$minor_source, function(x) which.min(abs(full_range - x)))
235+
old_val_minor <- old_range[unlist(old_val_minor)]
236+
old_val_minor_trans <- scale$trans$transform(old_val_minor)
232237

233-
range_info$minor[] <- round(
234-
rescale(
235-
scale$map(old_val_minor_trans, range(old_val_minor_trans)),
236-
from = range
237-
),
238-
digits = 3
239-
)
238+
range_info$minor[] <- round(
239+
rescale(
240+
scale$map(old_val_minor_trans, range(old_val_minor_trans)),
241+
from = range
242+
),
243+
digits = 3
244+
)
245+
} else {
246+
old_val_minor_trans <- NULL
247+
}
240248
}
241249

242250
# The _source values should be in (primary) scale_transformed space,

0 commit comments

Comments
 (0)