Skip to content

Instantly share code, notes, and snippets.

@marcosci
Last active May 23, 2022 23:04
Show Gist options
  • Save marcosci/03cc149d3661ae8cc0778cc06ecafb21 to your computer and use it in GitHub Desktop.
Save marcosci/03cc149d3661ae8cc0778cc06ecafb21 to your computer and use it in GitHub Desktop.
ggplot_polygon_size
library(tidyverse)
library(patchwork)
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.3, PROJ 7.2.1; sf_use_s2() is TRUE
poly <- st_polygon(list(cbind(c(0,1,1,0,0), c(0,0,1,1,0))))
poly_grid <- st_sf(st_make_grid(poly, cellsize = 0.01))
poly_grid[1:490, "group"] <- 1
poly_grid[491:3600, "group"] <- 2
poly_grid[3601:3950, "group"] <- 3
poly_grid[3951:8999, "group"] <- 4
poly_grid[9000:10000, "group"] <- 5
plot_list <- map(unique(poly_grid$group), function(x){
poly_grid <- poly_grid %>%
filter(group == x)
ggplot(poly_grid) +
geom_sf(fill="orange", color="grey75", lwd = 0.1) +
theme_void()
})
wrap_plots(plot_list)
@marcosci
Copy link
Author

image

@mcanouil
Copy link

mcanouil commented May 23, 2022

library(ggplot2)
library(patchwork)
library(sf)

poly <- st_polygon(list(cbind(c(0, 1, 1, 0, 0), c(0, 0, 1, 1, 0))))
poly_grid <- st_sf(st_make_grid(poly, cellsize = 0.01))

plot_list <- by(
  data = poly_grid,
  INDICES = cut(
    x = seq_len(nrow(poly_grid)),
    breaks = c(1, 491, 3601, 3951, 9000, nrow(poly_grid)),
    labels = FALSE,
    include.lowest = TRUE
  ),
  FUN = function(df) {
    ggplot(df) +
      geom_sf(fill = "orange", color = "grey75", lwd = 0.1) +
      theme_void()
  }
)

wrap_plots(plot_list, widths = 1, heights = 1)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment