11context(" cookbook lines" )
22
3- expect_traces_shapes <- function (gg , n.traces , n.shapes , name ){
3+ expect_traces_shapes <- function (gg , n.traces , n.shapes , name ) {
44 stopifnot(is.ggplot(gg ))
55 stopifnot(is.numeric(n.traces ))
66 stopifnot(is.numeric(n.shapes ))
77 save_outputs(gg , paste0(" cookbook-lines-" , name ))
88 L <- gg2list(gg )
9- is.trace <- names(L ) == " "
10- all.traces <- L [is.trace ]
9+ all.traces <- L $ data
1110 no.data <- sapply(all.traces , function (tr ) {
1211 is.null(tr [[" x" ]]) && is.null(tr [[" y" ]])
1312 })
1413 has.data <- all.traces [! no.data ]
1514 expect_equal(length(has.data ), n.traces )
16- shapes <- L $ kwargs $ layout $ shapes
15+ shapes <- L $ layout $ shapes
1716 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 )
2118}
2219
23- expect_shape <- function (s , ... ){
20+ expect_shape <- function (s , ... ) {
2421 expected.list <- list (... )
25- for (key in names(expected.list )){
22+ for (key in names(expected.list )) {
2623 value <- expected.list [[key ]]
2724 expect_identical(s [[key ]], value )
2825 }
2926}
3027
3128# Some sample data
32- df <- read.table(header = T , text = "
29+ df <- read.table(header = T , text = "
3330 cond result
3431 control 10
3532treatment 11.5
3633" )
3734
3835# 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" )
5138
5239test_that(" geom_bar -> 1 trace" , {
5340 info <- expect_traces_shapes(bp , 1 , 0 , " basic-bar" )
5441})
5542
5643# Add a horizontal line
57- temp <- bp + geom_hline(aes(yintercept = 12 ))
44+ temp <- bp + geom_hline(aes(yintercept = 12 ))
5845test_that(" bar + hline = 2 traces" , {
5946 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)
6347})
6448
6549# Make the line red and dashed
@@ -82,14 +66,15 @@ bp <- ggplot(df, aes(x=cond, y=result)) +
8266 geom_bar(position = position_dodge(), stat = " identity" )
8367
8468bp.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" )
8671test_that(" Draw with separate lines for each bar" , {
8772 expect_traces_shapes(bp.err , 2 , 0 , " bar-error-wide" )
8873})
8974
9075bp.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" )
9378test_that(" Make the lines narrower" , {
9479 info <- expect_traces_shapes(bp.err.narrow , 2 , 0 , " bar-error-narrow" )
9580})
@@ -103,8 +88,8 @@ df.hlines <- data.frame(cond=c("control","treatment"), hline=c(9,12))
10388# treatment 12
10489
10590bp.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" )
10893test_that(" The bar graph are from df, but the lines are from df.hlines" , {
10994 info <- expect_traces_shapes(bp.err.diff , 2 , 0 , " bar-error-diff" )
11095})
@@ -116,13 +101,14 @@ treatment A 11.5 12
116101 control B 12 9
117102treatment B 14 12
118103" )
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" )
121106test_that(" bar dodged colored -> 1 trace" , {
122107 info <- expect_traces_shapes(bp , 2 , 0 , " bar-dodge-color" )
123108})
124109bp.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" )
126112test_that(" The error bars get plotted over one another" , {
127113 # there are four but it looks like two.
128114 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", {
131117 expect_equal(length(unique(err.y )), 2 )
132118})
133119
134- df <- read.table(header = T , text = "
120+ df <- read.table(header = TRUE , text = "
135121 cond group result hline
136122 control A 10 11
137123treatment A 11.5 12
138124 control B 12 12.5
139125treatment B 14 15
140126" )
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" )
143129bp.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())
146132test_that(" 4 error bars" , {
147133 info <- expect_traces_shapes(bp.err4 , 3 , 0 , " bar-dodge-color-err4" )
148134 tr <- info $ traces [[3 ]]
@@ -152,7 +138,7 @@ test_that("4 error bars", {
152138 expect_equal(length(unique(tr $ x )), 2 )
153139})
154140
155- df <- read.table(header = T , text = "
141+ df <- read.table(header = T , text = "
156142 cond xval yval
157143 control 11.5 10.8
158144 control 9.3 12.9
@@ -175,7 +161,7 @@ df <- read.table(header=T, text="
175161 treatment 11.5 9.8
176162 treatment 12.0 10.6
177163" )
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()
179165test_that(" basic scatterplot" , {
180166 info <- expect_traces_shapes(sp , 2 , 0 , " scatter-basic" )
181167})
@@ -186,12 +172,12 @@ test_that("Add a horizontal line", {
186172})
187173
188174temp <- 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" )
192178test_that(" Add a red dashed vertical line" , {
193179 info <- expect_traces_shapes(temp , 4 , 0 , " scatter-hline-vline" )
194- expect_true(info $ kwargs $ layout $ showlegend )
180+ expect_true(info $ layout $ showlegend )
195181 mode <- sapply(info $ traces , " [[" , " mode" )
196182 line.traces <- info $ traces [mode == " lines" ]
197183 expect_equal(length(line.traces ), 2 )
@@ -206,7 +192,7 @@ temp <- sp + geom_hline(aes(yintercept=10)) +
206192 geom_line(stat = " vline" , xintercept = " mean" )
207193test_that(" Add colored lines for the mean xval of each group" , {
208194 info <- expect_traces_shapes(temp , 5 , 0 , " scatter-hline-vline-stat" )
209- expect_true(info $ kwargs $ layout $ showlegend )
195+ expect_true(info $ layout $ showlegend )
210196 mode <- sapply(info $ traces , " [[" , " mode" )
211197 line.traces <- info $ traces [mode == " lines" ]
212198 expect_equal(length(line.traces ), 3 )
@@ -236,23 +222,23 @@ test_that("scatter facet -> 2 traces", {
236222temp <- spf + geom_hline(aes(yintercept = 10 ))
237223test_that(" geom_hline -> 2 more traces" , {
238224 info <- expect_traces_shapes(temp , 4 , 0 , " scatter-facet-hline" )
239- expect_true(info $ kwargs $ layout $ showlegend )
225+ expect_true(info $ layout $ showlegend )
240226 has.name <- sapply(info $ traces , function (tr )is.character(tr $ name ))
241227 named.traces <- info $ traces [has.name ]
242228 expect_equal(length(named.traces ), 2 )
243229})
244230
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 ))
246232# cond xval
247233# control 10.0
248234# treatment 11.5
249235
250236spf.vline <-
251237 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" )
256242test_that(" geom_vline -> 2 more traces" , {
257243 info <- expect_traces_shapes(spf.vline , 6 , 0 , " scatter-facet-hline-vline" )
258244})
0 commit comments