Skip to content

Commit f79712f

Browse files
authored
Draw axis line when length(breaks) == 0 (#3257)
1 parent 8359806 commit f79712f

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ core developer team.
107107

108108
* `scale_shape_identity()` now works correctly with `guide = "legend"`
109109
(@malcolmbarrett, #3029)
110+
111+
* `scale_continuous` will now draw axis line even if the length of breaks is 0
112+
(@thomasp85, #3257)
110113

111114
* `stat_bin()` will now error when the number of bins exceeds 1e6 to avoid
112115
accidentally freezing the user session (@thomasp85).

R/guides-axis.r

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,28 @@
55
# @param position of axis (top, bottom, left or right)
66
# @param range of data values
77
guide_axis <- function(at, labels, position = "right", theme) {
8-
if (length(at) == 0)
9-
return(zeroGrob())
10-
11-
at <- unit(at, "native")
8+
line <- switch(position,
9+
top = element_render(theme, "axis.line.x.top", c(0, 1), c(0, 0), id.lengths = 2),
10+
bottom = element_render(theme, "axis.line.x.bottom", c(0, 1), c(1, 1), id.lengths = 2),
11+
right = element_render(theme, "axis.line.y.right", c(0, 0), c(0, 1), id.lengths = 2),
12+
left = element_render(theme, "axis.line.y.left", c(1, 1), c(0, 1), id.lengths = 2)
13+
)
1214
position <- match.arg(position, c("top", "bottom", "right", "left"))
1315

1416
zero <- unit(0, "npc")
1517
one <- unit(1, "npc")
1618

19+
if (length(at) == 0) {
20+
vertical <- position %in% c("left", "right")
21+
return(absoluteGrob(
22+
gList(line),
23+
width = if (vertical) zero else one,
24+
height = if (vertical) one else zero
25+
))
26+
}
27+
28+
at <- unit(at, "native")
29+
1730
theme$axis.ticks.length.x.bottom <- with(
1831
theme,
1932
axis.ticks.length.x.bottom %||%
@@ -71,12 +84,7 @@ guide_axis <- function(at, labels, position = "right", theme) {
7184
right = ,
7285
left = element_render(theme, label_render, labels, y = label_y, margin_x = TRUE))
7386

74-
line <- switch(position,
75-
top = element_render(theme, "axis.line.x.top", c(0, 1), c(0, 0), id.lengths = 2),
76-
bottom = element_render(theme, "axis.line.x.bottom", c(0, 1), c(1, 1), id.lengths = 2),
77-
right = element_render(theme, "axis.line.y.right", c(0, 0), c(0, 1), id.lengths = 2),
78-
left = element_render(theme, "axis.line.y.left", c(1, 1), c(0, 1), id.lengths = 2)
79-
)
87+
8088

8189
nticks <- length(at)
8290

0 commit comments

Comments
 (0)