Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Created December 14, 2024 12:57
Show Gist options
  • Save vjcitn/582e627fca62f2f4eaf42f0c1c87c651 to your computer and use it in GitHub Desktop.
Save vjcitn/582e627fca62f2f4eaf42f0c1c87c651 to your computer and use it in GitHub Desktop.
simple subregion select and crop
library(SpatialData)
library(SpatialData.data)
library(sf)
library(ggplot2)
if (!exists("br")) br = Breast2fov_10x()
brsh = shape(br)
library(SpatialData.plot)
# some points to form a subregion
pick = structure(list(x = c(355.185319678255, 353.624236083554, 510.140679646861,
496.285253053229, 355.185319678255), y = c(-471.421489018199,
-591.83220661942, -601.010859954238, -467.21109758021, -471.421489018199
)), row.names = c(NA, -5L), class = "data.frame")
plotSpatialData() + plotShape(br, c="black") + geom_path(data=pick, aes(x=x,y=abs(y), colour="red")) # full dataset
pick_poly = st_polygon(list(abs(data.matrix(pick))))
brshpoly = st_as_sf(brsh@data)
trim = st_intersects(brshpoly, pick_poly, sparse=FALSE)
sh251 = brshpoly[i=which(trim[,1]),]
par(ask=TRUE)
picked = ShapeFrame(as.data.frame(sh251))
if (!("pick" %in% shapeNames(br))) shapes(br)$pick = picked
plotSpatialData() + plotShape(br, "pick", c="black")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment