dieghernan / tidyterra

tidyverse and ggplot2 methods for terra spatial objects

Home Page:https://dieghernan.github.io/tidyterra/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

geom_spatvector() causes grid.arrange() to fail

Shrubner opened this issue · comments

There is a problem when using grid.arrange() from the gridExtra package (used to arrange ggplot figures side by side) if the ggplot object has a geom_spatvector() within it. When attempting to run the grid.arrange() function, I get this error:

grid.arrange(ggplot1, ggplot2, ncol = 2)

Error in `check_required_aesthetics()`:
! stat_sf requires the following missing aesthetics: geometry
Backtrace:
  1. gridExtra::grid.arrange(ggplot1, ggplot2, ncol = 2)
  2. gridExtra::arrangeGrob(...)
  3. base::lapply(grobs[toconv], ggplot2::ggplotGrob)
  4. ggplot2 FUN(X[[i]], ...)
  7. ggplot2:::ggplot_build.ggplot(x)
     ...
 12. self$stat$compute_layer(data, self$computed_stat_params, layout)
 13. ggplot2 f(..., self = self)
 14. ggproto_parent(Stat, self)$compute_layer(data, params, layout)
 15. ggplot2 f(..., self = self)
 16. ggplot2:::check_required_aesthetics(...)

I have tried another ggplot arranging package called patchwork and get a very similar error:

ggplot1 + ggplot2

Error in `check_required_aesthetics()`:
! stat_sf requires the following missing aesthetics: geometry
Backtrace:
  1. base `<fn>`(x)
  2. patchwork:::print.patchwork(x)
  3. patchwork:::build_patchwork(plot, plot$layout$guides %||% "auto")
  4. base::lapply(x$plots, plot_table, guides = guides)
  6. patchwork:::plot_table.ggplot(X[[i]], ...)
     ...
 15. self$stat$compute_layer(data, self$computed_stat_params, layout)
 16. ggplot2 f(..., self = self)
 17. ggproto_parent(Stat, self)$compute_layer(data, params, layout)
 18. ggplot2 f(..., self = self)
 19. ggplot2:::check_required_aesthetics(...)

Being able to arrange ggplot figures is a very useful tool so it would be great if it could be remedied!

Never mind, I found my mistake immediately after posting, as it always seems to go. I hadn't converted the data argument of my second geom_spatvector to a vector yet. Please delete the thread.