@@ -3,29 +3,70 @@ context("sec-axis")
3
3
x <- exp(seq(log(0.001 ), log(1000 ), length.out = 100 ))
4
4
foo <- data.frame (
5
5
x = x ,
6
- y = x / ( 1 + x )
6
+ y = x / ( 1 + x )
7
7
)
8
8
9
9
test_that(" dup_axis() works" , {
10
10
p <- ggplot(foo , aes(x , y )) +
11
11
geom_point() +
12
- scale_x_continuous(name = " Unit A" ,
13
- sec.axis = dup_axis())
12
+ scale_x_continuous(
13
+ name = " Unit A" ,
14
+ sec.axis = dup_axis()
15
+ )
14
16
scale <- layer_scales(p )$ x
15
17
expect_equal(scale $ sec_name(), scale $ name )
16
18
breaks <- scale $ break_info()
17
19
expect_equal(breaks $ minor , breaks $ sec.minor )
18
20
expect_equal(breaks $ major_source , breaks $ sec.major_source )
19
21
})
20
22
23
+ test_that(" sec_axis() breaks work for log-transformed scales" , {
24
+ df <- data.frame (
25
+ x = c(" A" , " B" , " C" ),
26
+ y = c(10 , 100 , 1000 )
27
+ )
28
+
29
+ # dup_axis()
30
+ p <- ggplot(data = df , aes(x , y )) +
31
+ geom_point() +
32
+ scale_y_log10(sec.axis = dup_axis())
33
+
34
+ scale <- layer_scales(p )$ y
35
+ breaks <- scale $ break_info()
36
+
37
+ expect_equal(breaks $ major_source , breaks $ sec.major_source )
38
+
39
+ # sec_axis() with transform
40
+ p <- ggplot(data = df , aes(x , y )) +
41
+ geom_point() +
42
+ scale_y_log10(sec.axis = sec_axis(~ . * 100 ))
43
+
44
+ scale <- layer_scales(p )$ y
45
+ breaks <- scale $ break_info()
46
+
47
+ expect_equal(breaks $ major_source , breaks $ sec.major_source - 2 )
48
+
49
+ # sec_axis() with transform and breaks
50
+ custom_breaks <- c(10 , 20 , 40 , 200 , 400 , 800 )
51
+ p <- ggplot(data = df , aes(x , y )) +
52
+ geom_point() +
53
+ scale_y_log10(breaks = custom_breaks , sec.axis = sec_axis(~ . * 100 ))
54
+
55
+ scale <- layer_scales(p )$ y
56
+ breaks <- scale $ break_info()
57
+
58
+ expect_equal(breaks $ major_source , log(custom_breaks , base = 10 ))
59
+ expect_equal(log_breaks()(df $ y ) * 100 , 10 ^ (breaks $ sec.major_source ))
60
+ })
61
+
21
62
test_that(" custom breaks work" , {
22
63
custom_breaks <- c(0.01 , 0.1 , 1 , 10 , 100 )
23
64
p <- ggplot(foo , aes(x , y )) +
24
65
geom_point() +
25
66
scale_x_continuous(
26
67
name = " Unit A" ,
27
68
sec.axis = sec_axis(
28
- trans = y ~ . ,
69
+ trans = y ~ . ,
29
70
breaks = custom_breaks
30
71
)
31
72
)
@@ -39,11 +80,15 @@ test_that("sec axis works with skewed transform", {
39
80
" sec_axis, skewed transform" ,
40
81
ggplot(foo , aes(x , y )) +
41
82
geom_point() +
42
- scale_x_continuous(name = " Unit A" , trans = ' log' ,
43
- breaks = c(0.001 , 0.01 , 0.1 , 1 , 10 , 100 , 1000 ),
44
- sec.axis = sec_axis(~ . * 100 , name = " Unit B" ,
45
- labels = derive(),
46
- breaks = derive()))
83
+ scale_x_continuous(
84
+ name = " Unit A" , trans = " log" ,
85
+ breaks = c(0.001 , 0.01 , 0.1 , 1 , 10 , 100 , 1000 ),
86
+ sec.axis = sec_axis(~ . * 100 ,
87
+ name = " Unit B" ,
88
+ labels = derive(),
89
+ breaks = derive()
90
+ )
91
+ )
47
92
)
48
93
})
49
94
0 commit comments