Skip to content

Commit 9da6de9

Browse files
committed
Merge pull request #122 from ropensci/marianne-geom-hline
Implement conversion for geom_hline
2 parents 52b7ceb + 41e588d commit 9da6de9

10 files changed

+261
-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
)
26.1 KB
Loading
20.8 KB
Loading

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+
})

tests/testthat/test-ggplot-hline.json

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"data": [
3+
{
4+
"x": [
5+
0,
6+
0.5,
7+
1,
8+
1.5,
9+
2,
10+
2.5,
11+
3,
12+
3.5
13+
],
14+
"y": [
15+
0,
16+
0.475,
17+
0.95,
18+
1.4249999999999998,
19+
1.9,
20+
2.375,
21+
2.8499999999999996,
22+
3.3249999999999997
23+
],
24+
"mode": "markers",
25+
"marker": {
26+
"color": "rgb(0,0,0)",
27+
"size": 10,
28+
"symbol": "circle",
29+
"opacity": 1,
30+
"sizeref": 1,
31+
"sizemode": "area"
32+
},
33+
"xaxis": "x1",
34+
"yaxis": "y1",
35+
"showlegend": false,
36+
"type": "scatter"
37+
},
38+
{
39+
"x": [
40+
-0.17500000000000002,
41+
3.675
42+
],
43+
"y": [
44+
1.1,
45+
1.1
46+
],
47+
"mode": "lines",
48+
"line": {
49+
"color": "rgb(0,255,0)",
50+
"width": 3,
51+
"dash": "solid",
52+
"shape": "linear"
53+
},
54+
"xaxis": "x1",
55+
"yaxis": "y1",
56+
"showlegend": false,
57+
"type": "scatter"
58+
}
59+
],
60+
"layout": {
61+
"titlefont": {
62+
"family": ""
63+
},
64+
"showlegend": true,
65+
"xaxis": {
66+
"title": "x1",
67+
"type": "linear",
68+
"showgrid": true,
69+
"showline": false,
70+
"showticklabels": true,
71+
"tickcolor": "rgb(127,127,127)",
72+
"gridcolor": "rgb(255,255,255)"
73+
},
74+
"yaxis": {
75+
"title": "x2",
76+
"type": "linear",
77+
"showgrid": true,
78+
"showline": false,
79+
"showticklabels": true,
80+
"tickcolor": "rgb(127,127,127)",
81+
"gridcolor": "rgb(255,255,255)"
82+
},
83+
"legend": {
84+
"x": 100,
85+
"y": 0.5,
86+
"font": {
87+
"family": ""
88+
},
89+
"bgcolor": "rgb(255,255,255)",
90+
"bordercolor": "transparent"
91+
},
92+
"margin": {
93+
"r": 10
94+
},
95+
"paper_bgcolor": "rgb(255,255,255)",
96+
"plot_bgcolor": "rgb(229,229,229)"
97+
}
98+
}

tests/testthat/test-ggplot-hline.url

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://plot.ly/~TestBot/561
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
"data": [
3+
{
4+
"x": [
5+
4.2
6+
],
7+
"y": [
8+
0.38
9+
],
10+
"mode": "markers",
11+
"name": "Paraguay",
12+
"text": "size: 7",
13+
"marker": {
14+
"color": "rgb(248,118,109)",
15+
"size": [
16+
2.5
17+
],
18+
"symbol": "circle",
19+
"line": {
20+
"width": 0
21+
},
22+
"opacity": 1,
23+
"sizeref": 1,
24+
"sizemode": "area"
25+
},
26+
"xaxis": "x1",
27+
"yaxis": "y1",
28+
"type": "scatter"
29+
},
30+
{
31+
"x": [
32+
1.75
33+
],
34+
"y": [
35+
1.67
36+
],
37+
"mode": "markers",
38+
"name": "Peru",
39+
"text": "size: 31",
40+
"marker": {
41+
"color": "rgb(0,186,56)",
42+
"size": [
43+
15.26595744680851
44+
],
45+
"symbol": "circle",
46+
"line": {
47+
"width": 0
48+
},
49+
"opacity": 1,
50+
"sizeref": 1,
51+
"sizemode": "area"
52+
},
53+
"xaxis": "x1",
54+
"yaxis": "y1",
55+
"type": "scatter"
56+
},
57+
{
58+
"x": [
59+
1.33
60+
],
61+
"y": [
62+
0.43
63+
],
64+
"mode": "markers",
65+
"name": "Philippines",
66+
"text": "size: 101",
67+
"marker": {
68+
"color": "rgb(97,156,255)",
69+
"size": [
70+
52.5
71+
],
72+
"symbol": "circle",
73+
"line": {
74+
"width": 0
75+
},
76+
"opacity": 1,
77+
"sizeref": 1,
78+
"sizemode": "area"
79+
},
80+
"xaxis": "x1",
81+
"yaxis": "y1",
82+
"type": "scatter"
83+
}
84+
],
85+
"layout": {
86+
"titlefont": {
87+
"family": ""
88+
},
89+
"showlegend": true,
90+
"xaxis": {
91+
"title": "edu",
92+
"type": "linear",
93+
"showgrid": true,
94+
"showline": false,
95+
"showticklabels": true,
96+
"tickcolor": "rgb(127,127,127)",
97+
"gridcolor": "rgb(255,255,255)"
98+
},
99+
"yaxis": {
100+
"title": "illn",
101+
"type": "linear",
102+
"showgrid": true,
103+
"showline": false,
104+
"showticklabels": true,
105+
"tickcolor": "rgb(127,127,127)",
106+
"gridcolor": "rgb(255,255,255)"
107+
},
108+
"legend": {
109+
"x": 100,
110+
"y": 0.5,
111+
"font": {
112+
"family": ""
113+
},
114+
"bgcolor": "rgb(255,255,255)",
115+
"bordercolor": "transparent"
116+
},
117+
"margin": {
118+
"r": 10
119+
},
120+
"paper_bgcolor": "rgb(255,255,255)",
121+
"plot_bgcolor": "rgb(229,229,229)"
122+
}
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://plot.ly/~TestBot/559

0 commit comments

Comments
 (0)