1
1
context(" cookbook lines" )
2
2
3
- expect_traces_shapes <- function (gg , n.traces , n.shapes , name ){
3
+ expect_traces_shapes <- function (gg , n.traces , n.shapes , name ) {
4
4
stopifnot(is.ggplot(gg ))
5
5
stopifnot(is.numeric(n.traces ))
6
6
stopifnot(is.numeric(n.shapes ))
7
7
save_outputs(gg , paste0(" cookbook-lines-" , name ))
8
8
L <- gg2list(gg )
9
- is.trace <- names(L ) == " "
10
- all.traces <- L [is.trace ]
9
+ all.traces <- L $ data
11
10
no.data <- sapply(all.traces , function (tr ) {
12
11
is.null(tr [[" x" ]]) && is.null(tr [[" y" ]])
13
12
})
14
13
has.data <- all.traces [! no.data ]
15
14
expect_equal(length(has.data ), n.traces )
16
- shapes <- L $ kwargs $ layout $ shapes
15
+ shapes <- L $ layout $ shapes
17
16
expect_equal(length(shapes ), n.shapes )
18
- list (traces = has.data ,
19
- shapes = shapes ,
20
- kwargs = L $ kwargs )
17
+ list (traces = has.data , shapes = shapes , layout = L $ layout )
21
18
}
22
19
23
- expect_shape <- function (s , ... ){
20
+ expect_shape <- function (s , ... ) {
24
21
expected.list <- list (... )
25
- for (key in names(expected.list )){
22
+ for (key in names(expected.list )) {
26
23
value <- expected.list [[key ]]
27
24
expect_identical(s [[key ]], value )
28
25
}
29
26
}
30
27
31
28
# Some sample data
32
- df <- read.table(header = T , text = "
29
+ df <- read.table(header = T , text = "
33
30
cond result
34
31
control 10
35
32
treatment 11.5
36
33
" )
37
34
38
35
# Basic bar plot
39
- bp <- ggplot(df , aes(x = cond , y = result )) +
40
- geom_bar(position = " dodge" , stat = " identity" )
41
-
42
- # # info <- gg2list(bp)
43
- # # info$kwargs$layout$shapes <-
44
- # # list(list(xref="paper",
45
- # # x0=0,
46
- # # x1=1,
47
- # # yref="y1",
48
- # # y0=10,
49
- # # y1=10))
50
- # # sendJSON(info)
36
+ bp <- ggplot(df , aes(x = cond , y = result )) +
37
+ geom_bar(position = " dodge" , stat = " identity" )
51
38
52
39
test_that(" geom_bar -> 1 trace" , {
53
40
info <- expect_traces_shapes(bp , 1 , 0 , " basic-bar" )
54
41
})
55
42
56
43
# Add a horizontal line
57
- temp <- bp + geom_hline(aes(yintercept = 12 ))
44
+ temp <- bp + geom_hline(aes(yintercept = 12 ))
58
45
test_that(" bar + hline = 2 traces" , {
59
46
info <- expect_traces_shapes(temp , 2 , 0 , " basic-horizontal-line" )
60
- # # expect_shape(info$shapes[[1]],
61
- # # xref="paper", x0=0, x1=1,
62
- # # yref="y1", y0=12, y1=12)
63
47
})
64
48
65
49
# Make the line red and dashed
@@ -82,14 +66,15 @@ bp <- ggplot(df, aes(x=cond, y=result)) +
82
66
geom_bar(position = position_dodge(), stat = " identity" )
83
67
84
68
bp.err <- bp +
85
- geom_errorbar(aes(y = hline , ymax = hline , ymin = hline ), colour = " #AA0000" )
69
+ geom_errorbar(aes(y = hline , ymax = hline , ymin = hline ),
70
+ colour = " #AA0000" )
86
71
test_that(" Draw with separate lines for each bar" , {
87
72
expect_traces_shapes(bp.err , 2 , 0 , " bar-error-wide" )
88
73
})
89
74
90
75
bp.err.narrow <- bp +
91
- geom_errorbar(width = 0.5 , aes(y = hline , ymax = hline , ymin = hline ),
92
- colour = " #AA0000" )
76
+ geom_errorbar(width = 0.5 , aes(y = hline , ymax = hline , ymin = hline ),
77
+ colour = " #AA0000" )
93
78
test_that(" Make the lines narrower" , {
94
79
info <- expect_traces_shapes(bp.err.narrow , 2 , 0 , " bar-error-narrow" )
95
80
})
@@ -103,8 +88,8 @@ df.hlines <- data.frame(cond=c("control","treatment"), hline=c(9,12))
103
88
# treatment 12
104
89
105
90
bp.err.diff <- bp +
106
- geom_errorbar(data = df.hlines , aes(y = hline , ymax = hline , ymin = hline ),
107
- colour = " #AA0000" )
91
+ geom_errorbar(data = df.hlines , aes(y = hline , ymax = hline , ymin = hline ),
92
+ colour = " #AA0000" )
108
93
test_that(" The bar graph are from df, but the lines are from df.hlines" , {
109
94
info <- expect_traces_shapes(bp.err.diff , 2 , 0 , " bar-error-diff" )
110
95
})
@@ -116,13 +101,14 @@ treatment A 11.5 12
116
101
control B 12 9
117
102
treatment B 14 12
118
103
" )
119
- bp <- ggplot(df , aes(x = cond , y = result , fill = group )) +
120
- geom_bar(position = position_dodge(), stat = " identity" )
104
+ bp <- ggplot(df , aes(x = cond , y = result , fill = group )) +
105
+ geom_bar(position = position_dodge(), stat = " identity" )
121
106
test_that(" bar dodged colored -> 1 trace" , {
122
107
info <- expect_traces_shapes(bp , 2 , 0 , " bar-dodge-color" )
123
108
})
124
109
bp.err <-
125
- bp + geom_errorbar(aes(y = hline , ymax = hline , ymin = hline ), linetype = " dashed" )
110
+ bp + geom_errorbar(aes(y = hline , ymax = hline , ymin = hline ),
111
+ linetype = " dashed" )
126
112
test_that(" The error bars get plotted over one another" , {
127
113
# there are four but it looks like two.
128
114
info <- expect_traces_shapes(bp.err , 3 , 0 , " bar-dodge-color-error" )
@@ -131,18 +117,18 @@ test_that("The error bars get plotted over one another", {
131
117
expect_equal(length(unique(err.y )), 2 )
132
118
})
133
119
134
- df <- read.table(header = T , text = "
120
+ df <- read.table(header = TRUE , text = "
135
121
cond group result hline
136
122
control A 10 11
137
123
treatment A 11.5 12
138
124
control B 12 12.5
139
125
treatment B 14 15
140
126
" )
141
- bp <- ggplot(df , aes(x = cond , y = result , fill = group )) +
142
- geom_bar(position = position_dodge(), stat = " identity" )
127
+ bp <- ggplot(df , aes(x = cond , y = result , fill = group )) +
128
+ geom_bar(position = position_dodge(), stat = " identity" )
143
129
bp.err4 <- bp +
144
- geom_errorbar(aes(y = hline , ymax = hline , ymin = hline ),
145
- linetype = " dashed" , position = position_dodge())
130
+ geom_errorbar(aes(y = hline , ymax = hline , ymin = hline ),
131
+ linetype = " dashed" , position = position_dodge())
146
132
test_that(" 4 error bars" , {
147
133
info <- expect_traces_shapes(bp.err4 , 3 , 0 , " bar-dodge-color-err4" )
148
134
tr <- info $ traces [[3 ]]
@@ -152,7 +138,7 @@ test_that("4 error bars", {
152
138
expect_equal(length(unique(tr $ x )), 2 )
153
139
})
154
140
155
- df <- read.table(header = T , text = "
141
+ df <- read.table(header = T , text = "
156
142
cond xval yval
157
143
control 11.5 10.8
158
144
control 9.3 12.9
@@ -175,7 +161,7 @@ df <- read.table(header=T, text="
175
161
treatment 11.5 9.8
176
162
treatment 12.0 10.6
177
163
" )
178
- sp <- ggplot(df , aes(x = xval , y = yval , colour = cond )) + geom_point()
164
+ sp <- ggplot(df , aes(x = xval , y = yval , colour = cond )) + geom_point()
179
165
test_that(" basic scatterplot" , {
180
166
info <- expect_traces_shapes(sp , 2 , 0 , " scatter-basic" )
181
167
})
@@ -186,12 +172,12 @@ test_that("Add a horizontal line", {
186
172
})
187
173
188
174
temp <- sp +
189
- geom_hline(aes(yintercept = 10 )) +
190
- geom_vline(aes(xintercept = 11.5 ),
191
- colour = " #BB0000" , linetype = " dashed" )
175
+ geom_hline(aes(yintercept = 10 )) +
176
+ geom_vline(aes(xintercept = 11.5 ),
177
+ colour = " #BB0000" , linetype = " dashed" )
192
178
test_that(" Add a red dashed vertical line" , {
193
179
info <- expect_traces_shapes(temp , 4 , 0 , " scatter-hline-vline" )
194
- expect_true(info $ kwargs $ layout $ showlegend )
180
+ expect_true(info $ layout $ showlegend )
195
181
mode <- sapply(info $ traces , " [[" , " mode" )
196
182
line.traces <- info $ traces [mode == " lines" ]
197
183
expect_equal(length(line.traces ), 2 )
@@ -206,7 +192,7 @@ temp <- sp + geom_hline(aes(yintercept=10)) +
206
192
geom_line(stat = " vline" , xintercept = " mean" )
207
193
test_that(" Add colored lines for the mean xval of each group" , {
208
194
info <- expect_traces_shapes(temp , 5 , 0 , " scatter-hline-vline-stat" )
209
- expect_true(info $ kwargs $ layout $ showlegend )
195
+ expect_true(info $ layout $ showlegend )
210
196
mode <- sapply(info $ traces , " [[" , " mode" )
211
197
line.traces <- info $ traces [mode == " lines" ]
212
198
expect_equal(length(line.traces ), 3 )
@@ -236,23 +222,23 @@ test_that("scatter facet -> 2 traces", {
236
222
temp <- spf + geom_hline(aes(yintercept = 10 ))
237
223
test_that(" geom_hline -> 2 more traces" , {
238
224
info <- expect_traces_shapes(temp , 4 , 0 , " scatter-facet-hline" )
239
- expect_true(info $ kwargs $ layout $ showlegend )
225
+ expect_true(info $ layout $ showlegend )
240
226
has.name <- sapply(info $ traces , function (tr )is.character(tr $ name ))
241
227
named.traces <- info $ traces [has.name ]
242
228
expect_equal(length(named.traces ), 2 )
243
229
})
244
230
245
- df.vlines <- data.frame (cond = levels(df $ cond ), xval = c(10 ,11.5 ))
231
+ df.vlines <- data.frame (cond = levels(df $ cond ), xval = c(10 ,11.5 ))
246
232
# cond xval
247
233
# control 10.0
248
234
# treatment 11.5
249
235
250
236
spf.vline <-
251
237
spf +
252
- geom_hline(aes(yintercept = 10 )) +
253
- geom_vline(aes(xintercept = xval ),
254
- data = df.vlines ,
255
- colour = " #990000" , linetype = " dashed" )
238
+ geom_hline(aes(yintercept = 10 )) +
239
+ geom_vline(aes(xintercept = xval ),
240
+ data = df.vlines ,
241
+ colour = " #990000" , linetype = " dashed" )
256
242
test_that(" geom_vline -> 2 more traces" , {
257
243
info <- expect_traces_shapes(spf.vline , 6 , 0 , " scatter-facet-hline-vline" )
258
244
})
0 commit comments