@@ -25,7 +25,7 @@ gg <- ggplot(researchers, aes(country, papers, fill=field))
25
25
26
26
test_that(" position_dodge is translated to barmode=group" , {
27
27
gg.dodge <- gg + geom_bar(stat = " identity" , position = " dodge" )
28
- info <- expect_traces(gg.dodge , 3 , " dodge" )
28
+ info <- expect_traces(gg.dodge , 2 , " dodge" )
29
29
trs <- info $ traces
30
30
trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
31
31
expect_true(all(c(" Math" , " Bio" ) %in% trace.names ))
@@ -37,7 +37,7 @@ test_that("position_dodge is translated to barmode=group", {
37
37
38
38
test_that(" position_stack is translated to barmode=stack" , {
39
39
gg.stack <- gg + geom_bar(stat = " identity" , position = " stack" )
40
- info <- expect_traces(gg.stack , 3 , " stack" )
40
+ info <- expect_traces(gg.stack , 2 , " stack" )
41
41
trs <- info $ traces
42
42
trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
43
43
expect_true(all(c(" Math" , " Bio" ) %in% trace.names ))
@@ -46,7 +46,7 @@ test_that("position_stack is translated to barmode=stack", {
46
46
47
47
test_that(" position_identity is translated to barmode=overlay" , {
48
48
gg.identity <- gg + geom_bar(stat = " identity" , position = " identity" )
49
- info <- expect_traces(gg.identity , 3 , " identity" )
49
+ info <- expect_traces(gg.identity , 2 , " identity" )
50
50
trs <- info $ traces
51
51
trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
52
52
expect_true(all(c(" Math" , " Bio" ) %in% trace.names ))
@@ -58,11 +58,10 @@ test_that("dates work well with bar charts", {
58
58
researchers $ month <- as.Date(researchers $ month )
59
59
gd <- ggplot(researchers , aes(month , papers , fill = field )) +
60
60
geom_bar(stat = " identity" )
61
- info <- expect_traces(gd , 3 , " dates" )
61
+ info <- expect_traces(gd , 2 , " dates" )
62
62
trs <- info $ traces
63
63
expect_identical(info $ kwargs $ layout $ xaxis $ type , " date" )
64
- expect_identical(trs [[1 ]]$ x [1 ], " 2012-01-01 00:00:00" )
65
- expect_identical(trs [[1 ]]$ x [2 ], " 2012-02-01 00:00:00" )
64
+ expect_identical(trs [[1 ]]$ x , unique(researchers $ month ))
66
65
})
67
66
68
67
# # http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/
@@ -175,16 +174,18 @@ test_that("geom_bar() stacks counts", {
175
174
info <- expect_traces(base + geom_bar(), 3 , " position-stack" )
176
175
expect_identical(info $ kwargs $ layout $ barmode , " stack" )
177
176
trs <- info $ traces
178
- test <- colSums(t(sapply(trs , " [[" , " y" )), na.rm = TRUE )
179
- true <- as.numeric(colSums(with(mtcars , table(cyl , vs ))))
177
+ # sum of y values for each trace
178
+ test <- as.numeric(sort(sapply(trs , function (x ) sum(x $ y ))))
179
+ true <- as.numeric(sort(table(mtcars $ cyl )))
180
180
expect_identical(test , true )
181
181
})
182
182
183
183
test_that(" geom_bar(position = 'fill') stacks proportions" , {
184
184
info <- expect_traces(base + geom_bar(position = " fill" ), 3 , " position-fill" )
185
185
expect_identical(info $ kwargs $ layout $ barmode , " stack" )
186
186
trs <- info $ traces
187
- props <- colSums(t(sapply(trs , " [[" , " y" )), na.rm = TRUE )
188
- expect_identical(props , c(1 , 1 ))
187
+ # sum of y-values *conditioned* on a x-value
188
+ prop <- sum(sapply(sapply(trs , " [[" , " y" ), " [" , 1 ))
189
+ expect_identical(prop , 1 )
189
190
})
190
191
0 commit comments