11context(" bar" )
22
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+
318researchers <-
419 data.frame (country = c(" Canada" , " Canada" , " Germany" , " USA" ),
520 name = c(" Warren" , " Andreanne" , " Stefan" , " Toby" ),
@@ -10,75 +25,46 @@ gg <- ggplot(researchers, aes(country, papers, fill=field))
1025
1126test_that(" position_dodge is translated to barmode=group" , {
1227 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 , 3 , " dodge " )
29+ trs <- info $ traces
30+ trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
1631 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" )
1833 # 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" ))
2536})
2637
2738test_that(" position_stack is translated to barmode=stack" , {
2839 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 , 3 , " stack " )
41+ trs <- info $ traces
42+ trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
3243 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" )
3645})
3746
3847test_that(" position_identity is translated to barmode=overlay" , {
3948 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 , 3 , " identity " )
50+ trs <- info $ traces
51+ trace.names <- sapply(trs [1 : 2 ], " [[" , " name" )
4352 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 , " overlay" )
4754})
4855
4956test_that(" dates work well with bar charts" , {
50-
5157 researchers $ month <- c(" 2012-01-01" , " 2012-01-01" , " 2012-02-01" , " 2012-02-01" )
5258 researchers $ month <- as.Date(researchers $ month )
53-
5459 gd <- ggplot(researchers , aes(month , papers , fill = field )) +
5560 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 , 3 , " dates" )
62+ trs <- info $ traces
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" )
6566})
6667
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-
8268# # http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/
8369df <- data.frame (time = factor (c(" Lunch" ," Dinner" ), levels = c(" Lunch" ," Dinner" )),
8470 total_bill = c(14.89 , 17.23 ))
@@ -182,3 +168,23 @@ test_that("guides(fill=FALSE) does not affect colour legend", {
182168 expect_true(info $ kwargs $ layout $ showlegend )
183169})
184170
171+
172+ base <- ggplot(mtcars , aes(factor (vs ), fill = factor (cyl )))
173+
174+ test_that(" geom_bar() stacks counts" , {
175+ info <- expect_traces(base + geom_bar(), 3 , " position-stack" )
176+ expect_identical(info $ kwargs $ layout $ barmode , " stack" )
177+ trs <- info $ traces
178+ test <- colSums(t(sapply(trs , " [[" , " y" )), na.rm = TRUE )
179+ true <- as.numeric(colSums(with(mtcars , table(cyl , vs ))))
180+ expect_identical(test , true )
181+ })
182+
183+ test_that(" geom_bar(position = 'fill') stacks proportions" , {
184+ info <- expect_traces(base + geom_bar(position = " fill" ), 3 , " position-fill" )
185+ expect_identical(info $ kwargs $ layout $ barmode , " stack" )
186+ trs <- info $ traces
187+ props <- colSums(t(sapply(trs , " [[" , " y" )), na.rm = TRUE )
188+ expect_identical(props , c(1 , 1 ))
189+ })
190+
0 commit comments