Skip to content

Bug in geom_sf()? Unable to convert call to string when processing geometry aesthetic at print. #4122

Closed

Description

@rpruim

Summary of issue

An example similar to p2 is used in the {ggformula} package but is now breaking. It appears that when rendering the plot, as_string(as_name()) fails on the geometry aesthetic when it has the form data$variable. Simply storing the variable as a vector "solves" the problem; see p1.

Is this a bug or an intended change in behavior?

ggplot2 examples

suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(sf))
suppressPackageStartupMessages(library(ggformula))
suppressPackageStartupMessages(library(dplyr))
MI <- sf::st_as_sf(maps::map("county", "michigan", plot = FALSE, fill = TRUE))
MIgeom <- MI$geom

# this works
p1 <- 
  ggplot(ggformula::MIpop %>% arrange(county)) + 
  geom_sf(aes(fill = log10(population), geometry = MIgeom), color = "white") 
p1

# this fails
p2 <- 
  ggplot(ggformula::MIpop %>% arrange(county)) + 
  geom_sf(aes(fill = log10(population), geometry = MI$geometry), color = "white") 
p2
#> Error: Can't convert a call to a string

ggformula examples

For completeness, here are the {ggformula} examples that led to this. I see that these are not quite identical (although the same issues arise). See the geometry aesthetics displayed near the end.

p3 <- gf_sf(
  data = MIpop %>% dplyr::arrange(county),
  fill = ~ log10(population), 
  geometry = ~ MIgeom, color = "white")
p3

p4 <- gf_sf(
  data = MIpop %>% dplyr::arrange(county),
  fill = ~ log10(population), 
  geometry = ~ MI$geom, color = "white")
p4
#> Error: Can't convert a call to a string
p1$mapping
#> Aesthetic mapping: 
#> <empty>
p2$mapping
#> Aesthetic mapping: 
#> <empty>
p3$mapping
#> Aesthetic mapping: 
#> * `fill`     -> `log10(population)`
#> * `geometry` -> `MIgeom`
p4$mapping
#> Aesthetic mapping: 
#> * `fill`     -> `log10(population)`
#> * `geometry` -> `MI$geom`

rlang::as_string(rlang::as_name(p3$mapping$geometry))
#> [1] "MIgeom"
rlang::as_string(rlang::as_name(p4$mapping$geometry))
#> Error: Can't convert a call to a string

Created on 2020-07-07 by the reprex package (v0.3.0)

Activity

  1. clauswilke commented on Jul 8, 2020

    @clauswilke
    Member

    None of these examples are idiomatic, so I'd say ggformula should change its examples. Below I demonstrate idiomatic use for both ggplot2 and ggformula.

    suppressPackageStartupMessages(library(ggplot2))
    suppressPackageStartupMessages(library(sf))
    suppressPackageStartupMessages(library(ggformula))
    suppressPackageStartupMessages(library(dplyr))
    
    MI <- st_as_sf(maps::map("county", "michigan", plot = FALSE, fill = TRUE))
    
    data <- ggformula::MIpop %>% 
      arrange(county) %>%
      mutate(geometry = MI$geom) %>%
      st_as_sf()
    
    ggplot(data) + 
      geom_sf(aes(fill = log10(population)), color = "white") 

    gf_sf(
      data = data,
      fill = ~ log10(population), 
      color = "white"
    )

    Created on 2020-07-07 by the reprex package (v0.3.0)

  2. yutannihilation commented on Jul 8, 2020

    @yutannihilation
    Member

    I agree specifying a vector in aes() is not a recommended usage. That said, I feel this should work.

    library(ggplot2)
    
    nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
    ggplot(nc) +
      geom_sf(aes(fill = AREA, geometry = sf::st_centroid(geometry)))
    #> Error: Can't convert a call to a string
  3. rpruim commented on Jul 8, 2020

    @rpruim
    Author

    Thanks for the quick reply.

    I realize there are other ways to do things, but it seems like these things should work. It works fine for things like geom_point(), for example, and it worked until recently in geom_sf() as well. (That ggformula example has been in the ggformula package for quite some time and passed CRAN checks in March.) So I would say that geom_sf() behavior is unexpected, and I wonder if it is also unintended -- perhaps a side effect of rlang changes?

    suppressPackageStartupMessages(library(ggplot2))
    
    ggplot() +
      geom_point(aes(mtcars$disp, mtcars$mpg, color = mtcars$cyl))

    Created on 2020-07-08 by the reprex package (v0.3.0)

  4. yutannihilation commented on Jul 8, 2020

    @yutannihilation
    Member

    Again, while it works, it's not a recommended usage.

  5. added a commit that references this issue on Jul 10, 2020
    dcad61a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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