Skip to content

default_aesthetics in geom_sf does not handle NULL properly #3963

Closed
@wesnm

Description

@wesnm

When plotting a map using a discrete fill scale, I get the error below. The source seems to be default_aesthetics not handling a NULL value for "type" (the line numbers may be off, I was adding some debugging prints to check out internal values):

ggplot(nyt$state.maps[!(STATE %in% c("Alaska", "Hawaii", "Puerto Rico"))]) +
    geom_sf(aes(geometry=GEOMETRY, fill=INFECTIONS))

 Error in if (type == "point") { : argument is of length zero 
18. default_aesthetics(params$legend) at geom-sf.R#113
17. modify_list(default_aesthetics(params$legend), data) at geom-sf.R#113
16. f(...) at ggproto.r#178
15. g$draw_key(g$data[i, ], g$params, key_size) at guide-legend.r#635
14. FUN(X[[i]], ...) 
13. lapply(guide$geoms, function(g) {
    g$draw_key(g$data[i, ], g$params, key_size)
}) at guide-legend.r#634
12. FUN(X[[i]], ...) 
11. lapply(seq_len(nbreak), draw_key) at guide-legend.r#639
10. unlist(lapply(seq_len(nbreak), draw_key), recursive = FALSE) at guide-legend.r#639
9. guide_gengrob.legend(X[[i]], ...) at guides-.r#356
8. FUN(X[[i]], ...) 
7. lapply(gdefs, guide_gengrob, theme) at guides-.r#249
6. guides_gengrob(gdefs, theme) at guides-.r#137
5. build_guides(plot$scales, plot$layers, plot$mapping, position, 
    theme, plot$guides, plot$labels) at plot-build.r#179
4. ggplot_gtable.ggplot_built(data) at plot-build.r#158
3. ggplot_gtable(data) at plot.r#173
2. print.ggplot(x) 
1. (function (x, ...) 
UseMethod("print"))(x) 

Passing a value for legend (or character value for show.legend) works fine:

ggplot(nyt$state.maps[!(STATE %in% c("Alaska", "Hawaii", "Puerto Rico"))]) +
    geom_sf(aes(geometry=GEOMETRY, fill=INFECTIONS_CUT), legend="meh")

You can repeat this using the data from this stack overflow post this stack overflow post and some small changes. Moving the fill aesthetic inside the aes mapping gives the error:

Error:
ggplot2::ggplot() + 
    geom_sf(data = gainsville_df, aes(geometry=Geomtry, fill=`Cluster Group`), alpha=0.2) +
    coord_sf(crs = "+init=epsg:4326")+
    scale_fill_manual(values = c("red", "grey", "seagreen3","gold", "green","orange"), name= "Cluster Group")

No Error:
ggplot2::ggplot() + 
    geom_sf(data = gainsville_df, aes(geometry=Geomtry, fill=`Cluster Group`), alpha=0.2, legend="meh") +
    coord_sf(crs = "+init=epsg:4326")+
    scale_fill_manual(values = c("red", "grey", "seagreen3","gold", "green","orange"), name= "Cluster Group")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions