Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rasterise handles lists #26

Merged
merged 13 commits into from
Feb 26, 2021
Merged

Conversation

teunbrand
Copy link
Contributor

@teunbrand teunbrand commented Feb 26, 2021

Prompted by #25, this PR aims to allow list-input in the rasterise() function.

Regardless of whether the layer is geom_sf() or something else, handling lists is also convenient for rasterising multiple layers at once. With this PR, you can rasterise multiple layers:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.3
library(ggrastr)

ggplot(mtcars, aes(wt, mpg)) +
  rasterise(list(
    geom_point(),
    geom_smooth()
  ))
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

The updated rasterise() should be to find whatever layers are in a nested list, tag these for rasterisation, and ignore other list elements. In the geom_sf() case, this means that only the layer get rasterised, and not the coord. Below a demonstration for a nested list:

world1 <- sf::st_as_sf(maps::map('world', plot = FALSE, fill = TRUE))
ggplot() + rasterise(
  list(
    list(
      geom_sf(data = world1),
      theme(panel.background = element_rect(fill = "skyblue"))
    ),
    list(
      list(
        geom_point(aes(x = rnorm(100, sd = 10), y = rnorm(100, sd = 10)))
      ),
      theme(panel.border = element_rect(fill = NA, colour = "blue"))
    )
  )
)

The function still throws an error when provided a list that doesn't contain any layer elements. Note that I changed the error message as I found it confusing that it complained about the dev argument while the layer argument was at fault.

ggplot() + rasterise(
  list(scale_x_continuous(limits = c(-40, 40)))
)
#> Error: Cannot rasterise an object of class `list`.

Created on 2021-02-26 by the reprex package (v1.0.0)

P.S.
I accidentally branched this from the master branch, which seems ahead of the develop branch. I'm sorry if this drags along unintended changes.

@evanbiederstedt evanbiederstedt self-requested a review February 26, 2021 20:27
@evanbiederstedt
Copy link
Collaborator

Thanks @teunbrand

This does now handle the case mentioned in #25:

library(sf)
library(maps)
world1 <- sf::st_as_sf(maps::map('world', plot = FALSE, fill = TRUE))
library(ggplot2)
library(ggrastr)
ggplot2::ggplot() + ggrastr::rasterise(ggplot2::geom_sf(data = world1))

plot

I accidentally branched this from the master branch, which seems ahead of the develop branch. I'm sorry if this drags along unintended changes.

No problem, the branches were pretty much in sync.

With this PR, you can rasterise multiple layers

I'll add to the vignettes, and mention this. Ditto with geom_sf() example.

Copy link
Collaborator

@evanbiederstedt evanbiederstedt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Added an roxygen2 tag and added to the CHANGELOG.

I'll update the vignettes in the dev->master PR after the other PR is approved, and re-version.

Thanks!

@evanbiederstedt evanbiederstedt merged commit 8e8dbb0 into VPetukhov:develop Feb 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants