Skip to content

Commit 2363611

Browse files
committed
Refactor guide_axis
1 parent ad72bda commit 2363611

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

R/guides-axis.r

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,12 @@
44
# @param labels at ticks
55
# @param position of axis (top, bottom, left or right)
66
# @param range of data values
7-
guide_axis <- function(at, labels, position="right", theme) {
8-
position <- match.arg(position, c("top", "bottom", "right", "left"))
9-
10-
# Quick fix for conflicts #297 and #118
11-
# Previously, at = NA if there is no breaks (breaks = NA).
12-
# Fix for oob bug changed so that at = numeric(0) if there is no breaks.
13-
# Temporally, at is set as NA if there is no breaks.
14-
# see also SHA: f332070fca77399a84ea7a116e8c63f6990abaf6, SHA: 2ae13ad0a856c24cab6a69b523da0936ef7a94d8
15-
if (length(at) == 0) at <- NA
7+
guide_axis <- function(at, labels, position = "right", theme) {
8+
if (length(at) == 0)
9+
return(zeroGrob())
1610

1711
at <- unit(at, "native")
18-
length <- theme$axis.ticks.length
19-
label_pos <- length
20-
21-
nticks <- length(at)
12+
position <- match.arg(position, c("top", "bottom", "right", "left"))
2213

2314
zero <- unit(0, "npc")
2415
one <- unit(1, "npc")
@@ -31,12 +22,12 @@ guide_axis <- function(at, labels, position="right", theme) {
3122
label_x <- switch(position,
3223
top = ,
3324
bottom = at,
34-
right = label_pos,
35-
left = one - label_pos
25+
right = theme$axis.ticks.length,
26+
left = one - theme$axis.ticks.length
3627
)
3728
label_y <- switch(position,
38-
top = label_pos,
39-
bottom = one - label_pos,
29+
top = theme$axis.ticks.length,
30+
bottom = one - theme$axis.ticks.length,
4031
right = ,
4132
left = at
4233
)
@@ -62,21 +53,23 @@ guide_axis <- function(at, labels, position="right", theme) {
6253
left = element_render(theme, "axis.line.y", c(1, 1), c(0, 1), id.lengths = 2)
6354
)
6455

56+
nticks <- length(at)
57+
6558
ticks <- switch(position,
6659
top = element_render(theme, "axis.ticks.x",
6760
x = rep(at, each = 2),
68-
y = rep(unit.c(zero, length), nticks),
61+
y = rep(unit.c(zero, theme$axis.ticks.length), nticks),
6962
id.lengths = rep(2, nticks)),
7063
bottom = element_render(theme, "axis.ticks.x",
7164
x = rep(at, each = 2),
72-
y = rep(unit.c(one - length, one), nticks),
65+
y = rep(unit.c(one - theme$axis.ticks.length, one), nticks),
7366
id.lengths = rep(2, nticks)),
7467
right = element_render(theme, "axis.ticks.y",
75-
x = rep(unit.c(zero, length), nticks),
68+
x = rep(unit.c(zero, theme$axis.ticks.length), nticks),
7669
y = rep(at, each = 2),
7770
id.lengths = rep(2, nticks)),
7871
left = element_render(theme, "axis.ticks.y",
79-
x = rep(unit.c(one - length, one), nticks),
72+
x = rep(unit.c(one - theme$axis.ticks.length, one), nticks),
8073
y = rep(at, each = 2),
8174
id.lengths = rep(2, nticks))
8275
)
@@ -86,19 +79,23 @@ guide_axis <- function(at, labels, position="right", theme) {
8679
top = gtable_col("axis",
8780
grobs = list(labels, ticks),
8881
width = one,
89-
heights = unit.c(grobHeight(labels), label_pos)),
82+
heights = unit.c(grobHeight(labels), theme$axis.ticks.length)
83+
),
9084
bottom = gtable_col("axis",
9185
grobs = list(ticks, labels),
9286
width = one,
93-
heights = unit.c(label_pos, grobHeight(labels))),
87+
heights = unit.c(theme$axis.ticks.length, grobHeight(labels))
88+
),
9489
right = gtable_row("axis",
9590
grobs = list(ticks, labels),
96-
widths = unit.c(label_pos, grobWidth(labels)),
97-
height = one),
91+
widths = unit.c(theme$axis.ticks.length, grobWidth(labels)),
92+
height = one
93+
),
9894
left = gtable_row("axis",
9995
grobs = list(labels, ticks),
100-
widths = unit.c(grobWidth(labels), label_pos),
101-
height = one)
96+
widths = unit.c(grobWidth(labels), theme$axis.ticks.length),
97+
height = one
98+
)
10299
)
103100

104101
# Viewport for justifying the axis grob

0 commit comments

Comments
 (0)