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