@@ -14,3 +14,150 @@ 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
+ # expressions mixed with character labels
64
+ b <- ggplot_build(
65
+ plot +
66
+ scale_x_continuous(
67
+ breaks = c(1000 , 2000 , 3000 ),
68
+ labels = c(" A" , " B" , " C" )
69
+ ) +
70
+ scale_y_continuous(
71
+ breaks = c(1000 , 1500 , 2000 ),
72
+ labels = parse(text = c(" 10^3" , " 1.5 %*% 10^3" , " 2 %*% 10^3" ))
73
+ )
74
+ )
75
+ graticule <- b $ layout $ panel_params [[1 ]]$ graticule
76
+ expect_identical(
77
+ graticule [graticule $ type == " E" , ]$ degree_label ,
78
+ as.list(c(" A" , " B" , " C" ))
79
+ )
80
+ parsed <- vector(" list" , 3 )
81
+ parsed [1 : 3 ] <- parse(text = c(" 10^3" , " 1.5 %*% 10^3" , " 2 %*% 10^3" ))
82
+ expect_identical(
83
+ graticule [graticule $ type == " N" , ]$ degree_label ,
84
+ parsed
85
+ )
86
+
87
+ # parsing via coord_sf()
88
+ b <- ggplot_build(
89
+ plot +
90
+ scale_x_continuous(
91
+ breaks = c(1000 , 2000 , 3000 ),
92
+ labels = c(" 10^3" , " 2 %*% 10^3" , " 3 %*% 10^3" )
93
+ ) +
94
+ scale_y_continuous(
95
+ breaks = c(1000 , 1500 , 2000 ),
96
+ labels = c(" 10^3" , " 1.5 %*% 10^3" , " 2 %*% 10^3" )
97
+ ) +
98
+ coord_sf(parse_E_labels = TRUE , parse_N_labels = FALSE )
99
+ )
100
+ graticule <- b $ layout $ panel_params [[1 ]]$ graticule
101
+ parsed <- vector(" list" , 3 )
102
+ parsed [1 : 3 ] <- parse(text = c(" 10^3" , " 2 %*% 10^3" , " 3 %*% 10^3" ))
103
+ expect_identical(
104
+ graticule [graticule $ type == " E" , ]$ degree_label ,
105
+ parsed
106
+ )
107
+ expect_identical(
108
+ graticule [graticule $ type == " N" , ]$ degree_label ,
109
+ as.list(c(" 10^3" , " 1.5 %*% 10^3" , " 2 %*% 10^3" ))
110
+ )
111
+
112
+ b <- ggplot_build(
113
+ plot +
114
+ scale_x_continuous(
115
+ breaks = c(1000 , 2000 , 3000 ),
116
+ labels = c(" 10^3" , " 2 %*% 10^3" , " 3 %*% 10^3" )
117
+ ) +
118
+ scale_y_continuous(
119
+ breaks = c(1000 , 1500 , 2000 ),
120
+ labels = c(" 10^3" , " 1.5 %*% 10^3" , " 2 %*% 10^3" )
121
+ ) +
122
+ coord_sf(parse_E_labels = FALSE , parse_N_labels = TRUE )
123
+ )
124
+ graticule <- b $ layout $ panel_params [[1 ]]$ graticule
125
+ expect_identical(
126
+ graticule [graticule $ type == " E" , ]$ degree_label ,
127
+ as.list(c(" 10^3" , " 2 %*% 10^3" , " 3 %*% 10^3" ))
128
+ )
129
+ parsed <- vector(" list" , 3 )
130
+ parsed [1 : 3 ] <- parse(text = c(" 10^3" , " 1.5 %*% 10^3" , " 2 %*% 10^3" ))
131
+ expect_identical(
132
+ graticule [graticule $ type == " N" , ]$ degree_label ,
133
+ parsed
134
+ )
135
+
136
+ # autoparsing of degree labels
137
+ data <- sf :: st_sfc(
138
+ sf :: st_polygon(list (matrix (1e1 * c(1 , 2 , 3 , 1 , 1 , 3 , 2 , 1 ), ncol = 2 ))),
139
+ crs = 4326 # basic long-lat crs
140
+ )
141
+
142
+ plot <- ggplot(data ) + geom_sf()
143
+
144
+ b <- ggplot_build(
145
+ plot +
146
+ scale_x_continuous(breaks = c(10 , 20 , 30 )) +
147
+ scale_y_continuous(breaks = c(10 , 15 , 20 ))
148
+ )
149
+ graticule <- b $ layout $ panel_params [[1 ]]$ graticule
150
+ parsed <- vector(" list" , 3 )
151
+ parsed [1 : 3 ] <- parse(text = c(" 10*degree*E" , " 20*degree*E" , " 30*degree*E" ))
152
+ expect_identical(
153
+ graticule [graticule $ type == " E" , ]$ degree_label ,
154
+ parsed
155
+ )
156
+ parsed [1 : 3 ] <- parse(text = c(" 10*degree*N" , " 15*degree*N" , " 20*degree*N" ))
157
+ expect_identical(
158
+ graticule [graticule $ type == " N" , ]$ degree_label ,
159
+ parsed
160
+ )
161
+
162
+ })
163
+
0 commit comments