1
1
context(" bar" )
2
2
3
+ expect_traces <- function (gg , n.traces , name ){
4
+ stopifnot(is.ggplot(gg ))
5
+ stopifnot(is.numeric(n.traces ))
6
+ save_outputs(gg , paste0(" bar-" , name ))
7
+ L <- gg2list(gg )
8
+ is.trace <- names(L ) == " "
9
+ all.traces <- L [is.trace ]
10
+ no.data <- sapply(all.traces , function (tr ) {
11
+ is.null(tr [[" x" ]]) && is.null(tr [[" y" ]])
12
+ })
13
+ has.data <- all.traces [! no.data ]
14
+ expect_equal(length(has.data ), n.traces )
15
+ list (traces = has.data , kwargs = L $ kwargs )
16
+ }
17
+
3
18
researchers <-
4
19
data.frame (country = c(" Canada" , " Canada" , " Germany" , " USA" ),
5
20
name = c(" Warren" , " Andreanne" , " Stefan" , " Toby" ),
@@ -10,75 +25,47 @@ gg <- ggplot(researchers, aes(country, papers, fill=field))
10
25
11
26
test_that(" position_dodge is translated to barmode=group" , {
12
27
gg.dodge <- gg + geom_bar(stat = " identity" , position = " dodge" )
13
- L <- gg2list (gg.dodge )
14
- expect_equal(length( L ), 3 )
15
- trace.names <- sapply(L [1 : 2 ], " [[" , " name" )
28
+ info <- expect_traces (gg.dodge , 2 , " dodge " )
29
+ trs <- info $ traces
30
+ trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
16
31
expect_true(all(c(" Math" , " Bio" ) %in% trace.names ))
17
- expect_identical(L $ kwargs $ layout $ barmode , " group" )
32
+ expect_identical(info $ kwargs $ layout $ barmode , " group" )
18
33
# Check x values
19
- expect_identical(as.character(L [[1 ]]$ x [1 ]), " Canada" )
20
- expect_identical(as.character(L [[1 ]]$ x [2 ]), " Germany" )
21
- expect_identical(as.character(L [[2 ]]$ x [1 ]), " Canada" )
22
- expect_identical(as.character(L [[2 ]]$ x [2 ]), " USA" )
23
-
24
- save_outputs(gg.dodge , " bar-dodge" )
34
+ expect_identical(as.character(trs [[1 ]]$ x ), c(" Canada" , " Germany" ))
35
+ expect_identical(as.character(trs [[2 ]]$ x ), c(" Canada" , " USA" ))
25
36
})
26
37
27
38
test_that(" position_stack is translated to barmode=stack" , {
28
39
gg.stack <- gg + geom_bar(stat = " identity" , position = " stack" )
29
- L <- gg2list (gg.stack )
30
- expect_equal(length( L ), 3 )
31
- trace.names <- sapply(L [1 : 2 ], " [[" , " name" )
40
+ info <- expect_traces (gg.stack , 2 , " stack " )
41
+ trs <- info $ traces
42
+ trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
32
43
expect_true(all(c(" Math" , " Bio" ) %in% trace.names ))
33
- expect_identical(L $ kwargs $ layout $ barmode , " stack" )
34
-
35
- save_outputs(gg.stack , " bar-stack" )
44
+ expect_identical(info $ kwargs $ layout $ barmode , " stack" )
36
45
})
37
46
38
- test_that(" position_identity is translated to barmode=overlay " , {
47
+ test_that(" position_identity is translated to barmode=stack " , {
39
48
gg.identity <- gg + geom_bar(stat = " identity" , position = " identity" )
40
- L <- gg2list (gg.identity )
41
- expect_equal(length( L ), 3 )
42
- trace.names <- sapply(L [1 : 2 ], " [[" , " name" )
49
+ info <- expect_traces (gg.identity , 2 , " identity " )
50
+ trs <- info $ traces
51
+ trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
43
52
expect_true(all(c(" Math" , " Bio" ) %in% trace.names ))
44
- expect_identical(L $ kwargs $ layout $ barmode , " overlay" )
45
-
46
- save_outputs(gg.identity , " bar-identity" )
53
+ expect_identical(info $ kwargs $ layout $ barmode , " stack" )
47
54
})
48
55
49
56
test_that(" dates work well with bar charts" , {
50
-
51
57
researchers $ month <- c(" 2012-01-01" , " 2012-01-01" , " 2012-02-01" , " 2012-02-01" )
52
58
researchers $ month <- as.Date(researchers $ month )
53
-
54
59
gd <- ggplot(researchers , aes(month , papers , fill = field )) +
55
60
geom_bar(stat = " identity" )
56
-
57
- L <- gg2list(gd )
58
-
59
- expect_equal(length(L ), 3 ) # 2 traces + layout
60
- expect_identical(L $ kwargs $ layout $ xaxis $ type , " date" )
61
- expect_identical(L [[1 ]]$ x [1 ], " 2012-01-01 00:00:00" )
62
- expect_identical(L [[1 ]]$ x [2 ], " 2012-02-01 00:00:00" )
63
-
64
- save_outputs(gd , " bar-dates" )
61
+ info <- expect_traces(gd , 2 , " dates" )
62
+ trs <- info $ traces
63
+ expect_identical(info $ kwargs $ layout $ xaxis $ type , " date" )
64
+ # plotly likes time in milliseconds
65
+ t <- as.numeric(unique(researchers $ month )) * 24 * 60 * 60 * 1000
66
+ expect_identical(trs [[1 ]]$ x , t )
65
67
})
66
68
67
- expect_traces <- function (gg , n.traces , name ){
68
- stopifnot(is.ggplot(gg ))
69
- stopifnot(is.numeric(n.traces ))
70
- save_outputs(gg , paste0(" bar-" , name ))
71
- L <- gg2list(gg )
72
- is.trace <- names(L ) == " "
73
- all.traces <- L [is.trace ]
74
- no.data <- sapply(all.traces , function (tr ) {
75
- is.null(tr [[" x" ]]) && is.null(tr [[" y" ]])
76
- })
77
- has.data <- all.traces [! no.data ]
78
- expect_equal(length(has.data ), n.traces )
79
- list (traces = has.data , kwargs = L $ kwargs )
80
- }
81
-
82
69
# # http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/
83
70
df <- data.frame (time = factor (c(" Lunch" ," Dinner" ), levels = c(" Lunch" ," Dinner" )),
84
71
total_bill = c(14.89 , 17.23 ))
@@ -182,3 +169,25 @@ test_that("guides(fill=FALSE) does not affect colour legend", {
182
169
expect_true(info $ kwargs $ layout $ showlegend )
183
170
})
184
171
172
+
173
+ base <- ggplot(mtcars , aes(factor (vs ), fill = factor (cyl )))
174
+
175
+ test_that(" geom_bar() stacks counts" , {
176
+ info <- expect_traces(base + geom_bar(), 3 , " position-stack" )
177
+ expect_identical(info $ kwargs $ layout $ barmode , " stack" )
178
+ trs <- info $ traces
179
+ # sum of y values for each trace
180
+ test <- as.numeric(sort(sapply(trs , function (x ) sum(x $ y ))))
181
+ true <- as.numeric(sort(table(mtcars $ cyl )))
182
+ expect_identical(test , true )
183
+ })
184
+
185
+ test_that(" geom_bar(position = 'fill') stacks proportions" , {
186
+ info <- expect_traces(base + geom_bar(position = " fill" ), 3 , " position-fill" )
187
+ expect_identical(info $ kwargs $ layout $ barmode , " stack" )
188
+ trs <- info $ traces
189
+ # sum of y-values *conditioned* on a x-value
190
+ prop <- sum(sapply(sapply(trs , " [[" , " y" ), " [" , 1 ))
191
+ expect_identical(prop , 1 )
192
+ })
193
+
0 commit comments