@@ -83,3 +83,37 @@ test_that("0 breaks -> 3 traces with showlegend=FALSE", {
83
83
computed.showlegend <- sapply(info $ traces , " [[" , " showlegend" )
84
84
expect_identical(as.logical(computed.showlegend ), expected.showlegend )
85
85
})
86
+
87
+ # test of legend position
88
+ test_that(" very long legend items" , {
89
+ long_items <- data.frame (cat1 = sample(x = LETTERS [1 : 10 ],
90
+ size = 100 , replace = TRUE ),
91
+ cat2 = sample(x = c(" AAAAAAAAAAAAAAAAAAAAAAAAAAAAA" ,
92
+ " BBBBBBBBBBBBBBBBBBBBBBBBBBBBB" ,
93
+ " CCCCCCCCCCCCCCCCCCCCCCCCCCCCC" ),
94
+ size = 100 , replace = TRUE ))
95
+ p_long_items <- ggplot(long_items , aes(cat1 , fill = cat2 )) +
96
+ geom_bar(position = " dodge" )
97
+ info <- expect_traces(p_long_items , 3 , " very long legend items" )
98
+ expect_equal(length(info $ layout $ annotations ), 1 )
99
+ expected.names <- levels(long_items $ cat2 )
100
+ expect_identical(info $ layout $ annotations [[1 ]]$ y -
101
+ info $ layout $ legend $ y > 0 , TRUE )
102
+ })
103
+
104
+ # test of legend position
105
+ test_that(" many legend items" , {
106
+ many_items <- data.frame (cat1 = sample(x = paste0(" Group " , LETTERS [1 : 12 ]),
107
+ size = 100 , replace = TRUE ),
108
+ cat2 = sample(x = c(" foo" , " bar" , " baz" ),
109
+ size = 100 , replace = TRUE ))
110
+ p_many_items <- ggplot(many_items , aes(cat2 , fill = cat1 )) +
111
+ geom_bar(position = " dodge" )
112
+ info <- expect_traces(p_many_items , 12 , " many legend items" )
113
+ expect_equal(length(info $ layout $ annotations ), 1 )
114
+ expected.names <- levels(many_items $ cat2 )
115
+ expect_identical(info $ layout $ annotations [[1 ]]$ y > 0.5 , TRUE )
116
+ expect_identical(info $ layout $ annotations [[1 ]]$ y -
117
+ info $ layout $ legend $ y > 0 , TRUE )
118
+ })
119
+
0 commit comments