86
86
# ' @usage NULL
87
87
# ' @format NULL
88
88
GeomSf <- ggproto(" GeomSf" , Geom ,
89
- required_aes = " geometry" ,
90
- default_aes = aes(
91
- shape = NULL ,
92
- colour = NULL ,
93
- fill = NULL ,
94
- size = NULL ,
95
- linetype = 1 ,
96
- alpha = NA ,
97
- stroke = 0.5
98
- ),
89
+ required_aes = " geometry" ,
90
+ default_aes = aes(
91
+ shape = NULL ,
92
+ colour = NULL ,
93
+ fill = NULL ,
94
+ size = NULL ,
95
+ linetype = 1 ,
96
+ alpha = NA ,
97
+ stroke = 0.5
98
+ ),
99
99
100
- non_missing_aes = c(" size" , " shape" , " colour" ),
101
-
102
100
draw_panel = function (data , panel_params , coord , legend = NULL ,
103
- lineend = " butt" , linejoin = " round" , linemitre = 10 ) {
101
+ lineend = " butt" , linejoin = " round" , linemitre = 10 ,
102
+ na.rm = TRUE ) {
104
103
if (! inherits(coord , " CoordSf" )) {
105
104
stop(" geom_sf() must be used with coord_sf()" , call. = FALSE )
106
105
}
107
106
108
107
# Need to refactor this to generate one grob per geometry type
109
108
coord <- coord $ transform(data , panel_params )
110
- sf_grob(coord , lineend = lineend , linejoin = linejoin , linemitre = linemitre )
109
+ sf_grob(coord , lineend = lineend , linejoin = linejoin , linemitre = linemitre , na.rm = na.rm )
111
110
},
112
111
113
112
draw_key = function (data , params , size ) {
@@ -132,17 +131,36 @@ default_aesthetics <- function(type) {
132
131
}
133
132
}
134
133
135
- sf_grob <- function (x , lineend = " butt" , linejoin = " round" , linemitre = 10 ) {
136
- # Need to extract geometry out of corresponding list column
137
- geometry <- x $ geometry
138
- type <- sf_types [sf :: st_geometry_type(geometry )]
139
- is_point <- type %in% " point"
140
- type_ind <- match(type , c(" point" , " line" , " other" ))
134
+ sf_grob <- function (x , lineend = " butt" , linejoin = " round" , linemitre = 10 , na.rm = TRUE ) {
135
+ type <- sf_types [sf :: st_geometry_type(x $ geometry )]
136
+ is_point <- type == " point"
137
+ is_line <- type == " line"
138
+ is_other <- type == " other"
139
+ is_collection <- type == " collection"
140
+ type_ind <- match(type , c(" point" , " line" , " other" , " collection" ))
141
+ remove <- rep_len(FALSE , nrow(x ))
142
+ remove [is_point ] <- detect_missing(x , c(GeomPoint $ required_aes , GeomPoint $ non_missing_aes ))[is_point ]
143
+ remove [is_line ] <- detect_missing(x , c(GeomPath $ required_aes , GeomPath $ non_missing_aes ))[is_line ]
144
+ remove [is_other ] <- detect_missing(x , c(GeomPolygon $ required_aes , GeomPolygon $ non_missing_aes ))[is_other ]
145
+ if (any(remove )) {
146
+ if (! na.rm ) {
147
+ warning_wrap(
148
+ " Removed " , sum(remove ), " rows containing missing values (geom_sf)."
149
+ )
150
+ }
151
+ x <- x [! remove , , drop = FALSE ]
152
+ type_ind <- type_ind [! remove ]
153
+ is_collection <- is_collection [! remove ]
154
+ }
141
155
defaults <- list (
142
156
GeomPoint $ default_aes ,
143
157
GeomLine $ default_aes ,
144
158
modify_list(GeomPolygon $ default_aes , list (fill = " grey90" , colour = " grey35" ))
145
159
)
160
+ defaults [[4 ]] <- modify_list(
161
+ defaults [[3 ]],
162
+ rename(GeomPoint $ default_aes , c(size = " point_size" , fill = " point_fill" ))
163
+ )
146
164
default_names <- unique(unlist(lapply(defaults , names )))
147
165
defaults <- lapply(setNames(default_names , default_names ), function (n ) {
148
166
unlist(lapply(defaults , function (def ) def [[n ]] %|| % NA ))
@@ -153,16 +171,17 @@ sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10) {
153
171
fill <- x $ fill %|| % defaults $ fill [type_ind ]
154
172
fill <- alpha(fill , alpha )
155
173
size <- x $ size %|| % defaults $ size [type_ind ]
174
+ point_size <- ifelse(is_collection , x $ size %|| % defaults $ point_size [type_ind ], size )
156
175
stroke <- (x $ stroke %|| % defaults $ stroke [1 ]) * .stroke / 2
157
- fontsize <- size * .pt + stroke
176
+ fontsize <- point_size * .pt + stroke
158
177
lwd <- ifelse(is_point , stroke , size * .pt )
159
178
pch <- x $ shape %|| % defaults $ shape [type_ind ]
160
179
lty <- x $ linetype %|| % defaults $ linetype [type_ind ]
161
180
gp <- gpar(
162
181
col = col , fill = fill , fontsize = fontsize , lwd = lwd , lty = lty ,
163
182
lineend = lineend , linejoin = linejoin , linemitre = linemitre
164
183
)
165
- sf :: st_as_grob(geometry , pch = pch , gp = gp )
184
+ sf :: st_as_grob(x $ geometry , pch = pch , gp = gp )
166
185
}
167
186
168
187
# ' @export
@@ -280,7 +299,7 @@ geom_sf_text <- function(mapping = aes(), data = NULL,
280
299
281
300
sf_types <- c(GEOMETRY = " other" , POINT = " point" , LINESTRING = " line" ,
282
301
POLYGON = " other" , MULTIPOINT = " point" , MULTILINESTRING = " line" ,
283
- MULTIPOLYGON = " other" , GEOMETRYCOLLECTION = " other " ,
302
+ MULTIPOLYGON = " other" , GEOMETRYCOLLECTION = " collection " ,
284
303
CIRCULARSTRING = " line" , COMPOUNDCURVE = " other" , CURVEPOLYGON = " other" ,
285
304
MULTICURVE = " other" , MULTISURFACE = " other" , CURVE = " other" ,
286
305
SURFACE = " other" , POLYHEDRALSURFACE = " other" , TIN = " other" ,
0 commit comments