Skip to content

Commit e53047b

Browse files
committed
Merge pull request #131 from ropensci/marianne-geom-vline
Support geom_vline() conversion
2 parents c47df47 + f2b4d96 commit e53047b

File tree

6 files changed

+111
-13
lines changed

6 files changed

+111
-13
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: plotly
22
Type: Package
33
Title: Interactive, publication-quality graphs online.
4-
Version: 0.5.3
4+
Version: 0.5.4
55
Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"),
66
email = "[email protected]"),
77
person("Scott", "Chamberlain", role = "aut",

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.5.4 -- 22 October 2014.
2+
3+
Support conversion of geom_vline().
4+
15
0.5.3 -- 21 October 2014.
26

37
Support conversion of geom_bar() with position_dodge().

R/ggplotly.R

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ gg2list <- function(p){
9898
xrange <- sapply(ggranges, `[[`, "x.range", simplify=FALSE, USE.NAMES=FALSE)
9999
ggxmin <- min(sapply(xrange, min))
100100
ggxmax <- max(sapply(xrange, max))
101+
# Extract y.range
102+
yrange <- sapply(ggranges, `[[`, "y.range", simplify=FALSE, USE.NAMES=FALSE)
103+
ggymin <- min(sapply(yrange, min))
104+
ggymax <- max(sapply(yrange, max))
101105

102106
# Get global size range because we need some of its info in layer2traces
103107
if ("size.name" %in% name.names) {
@@ -168,6 +172,9 @@ gg2list <- function(p){
168172
# Add global x-range info
169173
misc$prestats.data$globxmin <- ggxmin
170174
misc$prestats.data$globxmax <- ggxmax
175+
# Add global y-range info
176+
misc$prestats.data$globymin <- ggymin
177+
misc$prestats.data$globymax <- ggymax
171178

172179
# Add global size info if relevant
173180
if ("size.name" %in% name.names) {

R/trace_generation.R

+29-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ layer2traces <- function(l, d, misc) {
121121
}
122122
## Then split on visual characteristics that will get different
123123
## legend entries.
124-
data.list <- if(basic$geom %in% names(markLegends)){
124+
data.list <- if (basic$geom %in% names(markLegends)) {
125125
mark.names <- markLegends[[basic$geom]]
126126
## However, continuously colored points are an exception: they do
127127
## not need a legend entry, and they can be efficiently rendered
@@ -152,11 +152,24 @@ layer2traces <- function(l, d, misc) {
152152
}
153153
}
154154

155+
# Split hline and vline when multiple
156+
if (g$geom == "hline" || g$geom == "vline") {
157+
if (nrow(g$data) > 1) {
158+
df.list <- split(basic$data, rep(1:nrow(g$data)))
159+
data.list <- lapply(df.list, function(df) {
160+
params <- basic$params
161+
list(data=df,
162+
params=params)
163+
})
164+
}
165+
}
166+
155167
## case of no legend, if either of the two ifs above failed.
156168
if(is.null(data.list)){
157169
data.list <- structure(list(list(data=basic$data, params=basic$params)),
158170
names=basic$params$name)
159171
}
172+
160173
getTrace <- geom2trace[[basic$geom]]
161174
if(is.null(getTrace)){
162175
warning("Conversion not implemented for geom_",
@@ -167,7 +180,8 @@ layer2traces <- function(l, d, misc) {
167180
}
168181
traces <- NULL
169182
names.in.legend <- NULL
170-
for(data.i in seq_along(data.list)){
183+
184+
for (data.i in seq_along(data.list)) {
171185
data.params <- data.list[[data.i]]
172186
data.params$params$stat.type <- l$stat$objname
173187
tr <- do.call(getTrace, data.params)
@@ -303,6 +317,11 @@ toBasic <- list(
303317
g$params$xend <- max(g$prestats.data$globxmax)
304318
g
305319
},
320+
vline=function(g) {
321+
g$params$ystart <- min(g$prestats.data$globymin)
322+
g$params$yend <- max(g$prestats.data$globymax)
323+
g
324+
},
306325
point=function(g) {
307326
if ("size" %in% names(g$data)) {
308327
g$params$sizemin <- min(g$prestats.data$globsizemin)
@@ -527,5 +546,13 @@ geom2trace <- list(
527546
type="scatter",
528547
mode="lines",
529548
line=paramORdefault(params, aes2line, line.defaults))
549+
},
550+
vline=function(data, params) {
551+
list(x=c(data$xintercept, data$xintercept),
552+
y=c(params$ystart, params$yend),
553+
name=params$name,
554+
type="scatter",
555+
mode="lines",
556+
line=paramORdefault(params, aes2line, line.defaults))
530557
}
531558
)

tests/testthat/test-ggplot-hline.R

+28-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
context("Hline")
2-
32
# Horizontal line
43

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)
4+
x1 <- seq(from=0, to=3.5, by=0.5)
5+
x2 <- x1 * 0.95
6+
df <- data.frame("x1"=x1, "x2"=x2)
7+
gg <- ggplot(df) + geom_point(aes(x=x1, y=x2))
8+
9+
test_that("second trace be the hline", {
10+
gg <- gg + geom_hline(yintercept=1.1, colour="green", size=3)
1311

1412
L <- gg2list(gg)
1513

1614
expect_equal(length(L), 3)
15+
expect_equal(L[[2]]$y[1], 1.1)
1716
expect_true(L[[2]]$x[1] <= 0)
1817
expect_true(L[[2]]$x[2] >= 3.5)
1918
expect_identical(L[[2]]$mode, "lines")
@@ -22,4 +21,23 @@ test_that("Second trace be the hline", {
2221
expect_identical(L[[2]]$line$color, "rgb(0,255,0)")
2322

2423
save_outputs(gg, "hline")
25-
})
24+
})
25+
26+
test_that("vector yintercept results in multiple horizontal lines", {
27+
gg <- gg + geom_hline(yintercept=1:3, colour="red", size=3)
28+
29+
L <- gg2list(gg)
30+
31+
expect_equal(length(L), 5)
32+
expect_equal(L[[2]]$y[1], 1)
33+
expect_equal(L[[3]]$y[1], 2)
34+
expect_equal(L[[4]]$y[1], 3)
35+
expect_true(L[[4]]$x[1] <= 0)
36+
expect_true(L[[4]]$x[2] >= 3.325)
37+
expect_identical(L[[3]]$mode, "lines")
38+
expect_identical(L[[3]]$line$shape, "linear")
39+
expect_equal(L[[3]]$line$width, 3)
40+
expect_identical(L[[3]]$line$color, "rgb(255,0,0)")
41+
42+
save_outputs(gg, "hline-multiple")
43+
})

tests/testthat/test-ggplot-vline.R

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
context("Vline")
2+
# Vertical line
3+
4+
x1 <- seq(from=0, to=3.5, by=0.5)
5+
x2 <- x1 * 0.95
6+
df <- data.frame("x1"=x1, "x2"=x2)
7+
gg <- ggplot(df) + geom_point(aes(x=x1, y=x2))
8+
9+
test_that("second trace be the vline", {
10+
gg <- gg + geom_vline(xintercept=1.1, colour="green", size=3)
11+
12+
L <- gg2list(gg)
13+
14+
expect_equal(length(L), 3)
15+
expect_equal(L[[2]]$x[1], 1.1)
16+
expect_true(L[[2]]$y[1] <= 0)
17+
expect_true(L[[2]]$y[2] >= 3.325)
18+
expect_identical(L[[2]]$mode, "lines")
19+
expect_identical(L[[2]]$line$shape, "linear")
20+
expect_equal(L[[2]]$line$width, 3)
21+
expect_identical(L[[2]]$line$color, "rgb(0,255,0)")
22+
23+
save_outputs(gg, "vline")
24+
})
25+
26+
test_that("vector xintercept results in multiple vertical lines", {
27+
gg <- gg + geom_vline(xintercept=1:2, colour="blue", size=3)
28+
29+
L <- gg2list(gg)
30+
31+
expect_equal(length(L), 4)
32+
expect_equal(L[[2]]$x[1], 1)
33+
expect_equal(L[[3]]$x[1], 2)
34+
expect_true(L[[3]]$y[1] <= 0)
35+
expect_true(L[[3]]$y[2] >= 3.325)
36+
expect_identical(L[[3]]$mode, "lines")
37+
expect_identical(L[[3]]$line$shape, "linear")
38+
expect_equal(L[[3]]$line$width, 3)
39+
expect_identical(L[[3]]$line$color, "rgb(0,0,255)")
40+
41+
save_outputs(gg, "vline-multiple")
42+
})

0 commit comments

Comments
 (0)