Skip to content

Commit 01b9b64

Browse files
committed
Convert geom_hline the same way geom_abline is handled
1 parent 52b7ceb commit 01b9b64

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

R/trace_generation.R

+13
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ toBasic <- list(
294294
g$params$xend <- max(g$prestats.data$globxmax)
295295
g
296296
},
297+
hline=function(g) {
298+
g$params$xstart <- min(g$prestats.data$globxmin)
299+
g$params$xend <- max(g$prestats.data$globxmax)
300+
g
301+
},
297302
point=function(g) {
298303
if ("size" %in% names(g$data)) {
299304
g$params$sizemin <- min(g$prestats.data$globsizemin)
@@ -510,5 +515,13 @@ geom2trace <- list(
510515
type="scatter",
511516
mode="lines",
512517
line=paramORdefault(params, aes2line, line.defaults))
518+
},
519+
hline=function(data, params) {
520+
list(x=c(params$xstart, params$xend),
521+
y=c(data$yintercept, data$yintercept),
522+
name=params$name,
523+
type="scatter",
524+
mode="lines",
525+
line=paramORdefault(params, aes2line, line.defaults))
513526
}
514527
)

tests/testthat/test-ggplot-hline.R

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
context("Hline")
2+
3+
# Horizontal line
4+
5+
test_that("Second trace be the hline", {
6+
7+
x1 <- seq(from=0, to=3.5, by=0.5)
8+
x2 <- x1 * 0.95
9+
df <- data.frame("x1"=x1, "x2"=x2)
10+
11+
gg <- ggplot(df) + geom_point(aes(x=x1, y=x2)) +
12+
geom_hline(yintercept=1.1, colour="green", size=3)
13+
14+
L <- gg2list(gg)
15+
16+
expect_equal(length(L), 3)
17+
expect_true(L[[2]]$x[1] <= 0)
18+
expect_true(L[[2]]$x[2] >= 3.5)
19+
expect_identical(L[[2]]$mode, "lines")
20+
expect_identical(L[[2]]$line$shape, "linear")
21+
expect_equal(L[[2]]$line$width, 3)
22+
expect_identical(L[[2]]$line$color, "rgb(0,255,0)")
23+
24+
save_outputs(gg, "hline")
25+
})

0 commit comments

Comments
 (0)