@@ -131,6 +131,63 @@ GeomSf <- ggproto("GeomSf", Geom,
131131 stroke = 0.5
132132 ),
133133
134+ use_defaults = function (self , data , params = list (), modifiers = aes(), default_aes = NULL ) {
135+ data <- ggproto_parent(Geom , self )$ use_defaults(data , params , modifiers , default_aes )
136+ # Early exit for e.g. legend data that don't have geometry columns
137+ if (! " geometry" %in% names(data )) {
138+ return (data )
139+ }
140+
141+ # Devise splitting index for geometry types
142+ type <- sf_types [sf :: st_geometry_type(data $ geometry )]
143+ type <- factor (type , c(" point" , " line" , " other" , " collection" ))
144+ index <- split(seq_len(nrow(data )), type )
145+
146+ # Initialise parts of the data
147+ points <- lines <- others <- collections <- NULL
148+
149+ # Go through every part, applying different defaults
150+ if (length(index $ point ) > 0 ) {
151+ points <- GeomPoint $ use_defaults(
152+ vec_slice(data , index $ point ),
153+ params , modifiers
154+ )
155+ }
156+ if (length(index $ line ) > 0 ) {
157+ lines <- GeomLine $ use_defaults(
158+ vec_slice(data , index $ line ),
159+ params , modifiers
160+ )
161+ }
162+ other_default <- modify_list(
163+ GeomPolygon $ default_aes ,
164+ list (fill = " grey90" , colour = " grey35" , linewidth = 0.2 )
165+ )
166+ if (length(index $ other ) > 0 ) {
167+ others <- GeomPolygon $ use_defaults(
168+ vec_slice(data , index $ other ),
169+ params , modifiers ,
170+ default_aes = other_default
171+ )
172+ }
173+ if (length(index $ collection ) > 0 ) {
174+ modified <- rename(
175+ GeomPoint $ default_aes ,
176+ c(fill = " point_fill" )
177+ )
178+ modified <- modify_list(other_default , modified )
179+ collections <- Geom $ use_defaults(
180+ vec_slice(data , index $ collection ),
181+ params , modifiers ,
182+ default_aes = modified
183+ )
184+ }
185+
186+ # Recombine data in original order
187+ data <- vec_c(points , lines , others , collections )
188+ vec_slice(data , order(unlist(index )))
189+ },
190+
134191 draw_panel = function (self , data , panel_params , coord , legend = NULL ,
135192 lineend = " butt" , linejoin = " round" , linemitre = 10 ,
136193 arrow = NULL , na.rm = TRUE ) {
@@ -189,36 +246,24 @@ sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10,
189246 type_ind <- type_ind [! remove ]
190247 is_collection <- is_collection [! remove ]
191248 }
192- defaults <- list (
193- GeomPoint $ default_aes ,
194- GeomLine $ default_aes ,
195- modify_list(GeomPolygon $ default_aes , list (fill = " grey90" , colour = " grey35" , linewidth = 0.2 ))
196- )
197- defaults [[4 ]] <- modify_list(
198- defaults [[3 ]],
199- rename(GeomPoint $ default_aes , c(size = " point_size" , fill = " point_fill" ))
200- )
201- default_names <- unique0(unlist(lapply(defaults , names )))
202- defaults <- lapply(setNames(default_names , default_names ), function (n ) {
203- unlist(lapply(defaults , function (def ) def [[n ]] %|| % NA ))
204- })
205- alpha <- x $ alpha %|| % defaults $ alpha [type_ind ]
206- col <- x $ colour %|| % defaults $ colour [type_ind ]
249+
250+ alpha <- x $ alpha %|| % NA
251+ fill <- fill_alpha(x $ fill %|| % NA , alpha )
252+ col <- x $ colour %|| % NA
207253 col [is_point | is_line ] <- alpha(col [is_point | is_line ], alpha [is_point | is_line ])
208- fill <- x $ fill %|| % defaults $ fill [type_ind ]
209- fill <- fill_alpha(fill , alpha )
210- size <- x $ size %|| % defaults $ size [type_ind ]
211- linewidth <- x $ linewidth %|| % defaults $ linewidth [type_ind ]
254+
255+ size <- x $ size %|| % 0.5
256+ linewidth <- x $ linewidth %|| % 0.5
212257 point_size <- ifelse(
213258 is_collection ,
214- x $ size % || % defaults $ point_size [ type_ind ] ,
259+ x $ size ,
215260 ifelse(is_point , size , linewidth )
216261 )
217- stroke <- (x $ stroke %|| % defaults $ stroke [ 1 ] ) * .stroke / 2
262+ stroke <- (x $ stroke %|| % 0 ) * .stroke / 2
218263 fontsize <- point_size * .pt + stroke
219264 lwd <- ifelse(is_point , stroke , linewidth * .pt )
220- pch <- x $ shape % || % defaults $ shape [ type_ind ]
221- lty <- x $ linetype % || % defaults $ linetype [ type_ind ]
265+ pch <- x $ shape
266+ lty <- x $ linetype
222267 gp <- gpar(
223268 col = col , fill = fill , fontsize = fontsize , lwd = lwd , lty = lty ,
224269 lineend = lineend , linejoin = linejoin , linemitre = linemitre
0 commit comments