11context(" position_stack" )
22
3- test_that(" data is sorted prior to stacking" , {
3+ test_that(" data keeps its order after stacking" , {
44 df <- data_frame(
55 x = rep(c(1 : 10 ), 3 ),
66 var = rep(c(" a" , " b" , " c" ), 10 ),
@@ -9,7 +9,8 @@ test_that("data is sorted prior to stacking", {
99 p <- ggplot(df , aes(x = x , y = y , fill = var )) +
1010 geom_area(position = " stack" )
1111 dat <- layer_data(p )
12- expect_true(all(dat $ group == 3 : 1 ))
12+ expect_true(all(dat $ group == rep(1 : 3 , each = 10 )))
13+ expect_true(all(dat $ x == df $ x ))
1314})
1415
1516test_that(" negative and positive values are handled separately" , {
@@ -21,8 +22,8 @@ test_that("negative and positive values are handled separately", {
2122 p <- ggplot(df , aes(x , y , fill = factor (g ))) + geom_col()
2223 dat <- layer_data(p )
2324
24- expect_equal(dat $ ymin [dat $ x == 1 ], c(0 , - 1 , 1 ))
25- expect_equal(dat $ ymax [dat $ x == 1 ], c(1 , 0 , 2 ))
25+ expect_equal(dat $ ymin [dat $ x == 1 ], c(1 , - 1 , 0 ))
26+ expect_equal(dat $ ymax [dat $ x == 1 ], c(2 , 0 , 1 ))
2627
2728 expect_equal(dat $ ymin [dat $ x == 2 ], c(0 , - 3 ))
2829 expect_equal(dat $ ymax [dat $ x == 2 ], c(2 , 0 ))
@@ -49,12 +50,25 @@ test_that("data with no extent is stacked correctly", {
4950 p0 <- base + geom_text(aes(label = y ), position = position_stack(vjust = 0 ))
5051 p1 <- base + geom_text(aes(label = y ), position = position_stack(vjust = 1 ))
5152
52- expect_equal(layer_data(p0 )$ y , c(- 75 , - 115 ))
53- expect_equal(layer_data(p1 )$ y , c(0 , - 75 ))
53+ expect_equal(layer_data(p0 )$ y , c(- 115 , - 75 ))
54+ expect_equal(layer_data(p1 )$ y , c(- 75 , 0 ))
5455})
5556
5657test_that(" position_stack() can stack correctly when ymax is NA" , {
5758 df <- data_frame(x = c(1 , 1 ), y = c(1 , 1 ))
5859 p <- ggplot(df , aes(x , y , ymax = NA_real_ )) + geom_point(position = " stack" )
5960 expect_equal(layer_data(p )$ y , c(1 , 2 ))
6061})
62+
63+ # Visual tests ------------------------------------------------------------
64+
65+ test_that(" Stacking produces the expected output" , {
66+ data <- data_frame(
67+ x = rep(1 : 4 , each = 2 ),
68+ category = rep(c(" A" ," B" ), 4 ),
69+ value = c(0 , 0 , 2 , 1 , 3 , 6 , - 4 , 3 )
70+ )
71+ p <- ggplot(data , aes(x = x , y = value , fill = category )) +
72+ geom_area(stat = " identity" )
73+ expect_doppelganger(" Area stacking" , p )
74+ })
0 commit comments