@@ -14,3 +14,149 @@ test_that("multiplication works", {
14
14
skip(" sf tests are currently unstable" )
15
15
expect_doppelganger(" sf-polygons" , plot )
16
16
})
17
+
18
+
19
+ test_that(" axis labels can be set manually" , {
20
+ skip_if_not_installed(" sf" )
21
+
22
+ plot <- ggplot(sf :: st_polygon(list (matrix (1e3 * c(1 , 2 , 3 , 1 , 1 , 3 , 2 , 1 ), ncol = 2 )))) +
23
+ geom_sf()
24
+
25
+ # autogenerated labels
26
+ b <- ggplot_build(
27
+ plot +
28
+ scale_x_continuous(breaks = c(1000 , 2000 , 3000 )) +
29
+ scale_y_continuous(breaks = c(1000 , 1500 , 2000 ))
30
+ )
31
+ graticule <- b $ layout $ panel_params [[1 ]]$ graticule
32
+ expect_identical(
33
+ graticule [graticule $ type == " E" , ]$ degree_label ,
34
+ c(" 1000" , " 2000" , " 3000" )
35
+ )
36
+ expect_identical(
37
+ graticule [graticule $ type == " N" , ]$ degree_label ,
38
+ c(" 1000" , " 1500" , " 2000" )
39
+ )
40
+
41
+ # character labels
42
+ b <- ggplot_build(
43
+ plot +
44
+ scale_x_continuous(
45
+ breaks = c(1000 , 2000 , 3000 ),
46
+ labels = c(" A" , " B" , " C" )
47
+ ) +
48
+ scale_y_continuous(
49
+ breaks = c(1000 , 1500 , 2000 ),
50
+ labels = c(" D" , " E" , " F" )
51
+ )
52
+ )
53
+ graticule <- b $ layout $ panel_params [[1 ]]$ graticule
54
+ expect_identical(
55
+ graticule [graticule $ type == " E" , ]$ degree_label ,
56
+ c(" A" , " B" , " C" )
57
+ )
58
+ expect_identical(
59
+ graticule [graticule $ type == " N" , ]$ degree_label ,
60
+ c(" D" , " E" , " F" )
61
+ )
62
+
63
+ # factors are treated like character labels
64
+ # and are not parsed
65
+ b <- ggplot_build(
66
+ plot +
67
+ scale_x_continuous(
68
+ breaks = c(1000 , 2000 , 3000 ),
69
+ labels = factor (c(" A" , " B" , " C" ))
70
+ ) +
71
+ scale_y_continuous(
72
+ breaks = c(1000 , 1500 , 2000 ),
73
+ labels = factor (c(" 1 * degree * N" , " 1.5 * degree * N" , " 2 * degree * N" ))
74
+ )
75
+ )
76
+ graticule <- b $ layout $ panel_params [[1 ]]$ graticule
77
+ expect_identical(
78
+ graticule [graticule $ type == " E" , ]$ degree_label ,
79
+ c(" A" , " B" , " C" )
80
+ )
81
+ expect_identical(
82
+ graticule [graticule $ type == " N" , ]$ degree_label ,
83
+ c(" 1 * degree * N" , " 1.5 * degree * N" , " 2 * degree * N" )
84
+ )
85
+
86
+
87
+ # expressions mixed with character labels
88
+ b <- ggplot_build(
89
+ plot +
90
+ scale_x_continuous(
91
+ breaks = c(1000 , 2000 , 3000 ),
92
+ labels = c(" A" , " B" , " C" )
93
+ ) +
94
+ scale_y_continuous(
95
+ breaks = c(1000 , 1500 , 2000 ),
96
+ labels = parse(text = c(" 10^3" , " 1.5 %*% 10^3" , " 2 %*% 10^3" ))
97
+ )
98
+ )
99
+ graticule <- b $ layout $ panel_params [[1 ]]$ graticule
100
+ expect_identical(
101
+ graticule [graticule $ type == " E" , ]$ degree_label ,
102
+ as.list(c(" A" , " B" , " C" ))
103
+ )
104
+ parsed <- vector(" list" , 3 )
105
+ parsed [1 : 3 ] <- parse(text = c(" 10^3" , " 1.5 %*% 10^3" , " 2 %*% 10^3" ))
106
+ expect_identical(
107
+ graticule [graticule $ type == " N" , ]$ degree_label ,
108
+ parsed
109
+ )
110
+
111
+ # reverse x and y from previous test
112
+ b <- ggplot_build(
113
+ plot +
114
+ scale_y_continuous(
115
+ breaks = c(1000 , 2000 , 3000 ),
116
+ labels = c(" A" , " B" , " C" )
117
+ ) +
118
+ scale_x_continuous(
119
+ breaks = c(1000 , 1500 , 2000 ),
120
+ labels = parse(text = c(" 10^3" , " 1.5 %*% 10^3" , " 2 %*% 10^3" ))
121
+ )
122
+ )
123
+ graticule <- b $ layout $ panel_params [[1 ]]$ graticule
124
+ expect_identical(
125
+ graticule [graticule $ type == " N" , ]$ degree_label ,
126
+ as.list(c(" A" , " B" , " C" ))
127
+ )
128
+ parsed <- vector(" list" , 3 )
129
+ parsed [1 : 3 ] <- parse(text = c(" 10^3" , " 1.5 %*% 10^3" , " 2 %*% 10^3" ))
130
+ expect_identical(
131
+ graticule [graticule $ type == " E" , ]$ degree_label ,
132
+ parsed
133
+ )
134
+
135
+ # autoparsing of degree labels
136
+ data <- sf :: st_sfc(
137
+ sf :: st_polygon(list (matrix (1e1 * c(1 , 2 , 3 , 1 , 1 , 3 , 2 , 1 ), ncol = 2 ))),
138
+ crs = 4326 # basic long-lat crs
139
+ )
140
+
141
+ plot <- ggplot(data ) + geom_sf()
142
+
143
+ b <- ggplot_build(
144
+ plot +
145
+ scale_x_continuous(breaks = c(10 , 20 , 30 )) +
146
+ scale_y_continuous(breaks = c(10 , 15 , 20 ))
147
+ )
148
+ graticule <- b $ layout $ panel_params [[1 ]]$ graticule
149
+ parsed <- vector(" list" , 3 )
150
+ parsed [1 : 3 ] <- parse(text = c(" 10*degree*E" , " 20*degree*E" , " 30*degree*E" ))
151
+ expect_identical(
152
+ graticule [graticule $ type == " E" , ]$ degree_label ,
153
+ parsed
154
+ )
155
+ parsed [1 : 3 ] <- parse(text = c(" 10*degree*N" , " 15*degree*N" , " 20*degree*N" ))
156
+ expect_identical(
157
+ graticule [graticule $ type == " N" , ]$ degree_label ,
158
+ parsed
159
+ )
160
+
161
+ })
162
+
0 commit comments