@@ -3,11 +3,9 @@ title: "`SpatialData.plot`"
33date : " `r format(Sys.Date(), '%B %d, %Y')`"
44package : " `r BiocStyle::pkg_ver('SpatialData.plot')`"
55author :
6- - name : Helena Lucia Crowell
7- - name : Louise Deconinck
6+ - name : Helena L. Crowell
87 - name : Artür Manukyan
9- - name : Dario Righelli
10- - name : Estella Dong
8+ - name : Hugo Gruson
119 - name : Vince Carey
1210output :
1311 BiocStyle::html_document
@@ -69,7 +67,7 @@ Internally, multiscale `ImageArray`s are stored as a list of `ZarrArray`, e.g.:
6967
7068``` {r ms-dims}
7169i <- image(x, "blobs_multiscale_image")
72- vapply(i@ data, dim, numeric(3))
70+ vapply(data(i, k=NULL) , dim, numeric(3))
7371```
7472
7573To retrieve a specific scale's ` ZarrArray ` , we can use ` data(., k) ` ,
@@ -82,7 +80,7 @@ wrap_plots(nrow=1, lapply(seq(3), \(.)
8280
8381### Labels
8482
85- ``` {r plotLabel, fig.width=8 , fig.height=3.5}
83+ ``` {r plotLabel, fig.width=9 , fig.height=3.5}
8684i <- "blobs_labels"
8785t <- getTable(x, i)
8886t$id <- sample(letters, ncol(t))
@@ -92,10 +90,9 @@ p <- plotSpatialData()
9290pal_d <- hcl.colors(10, "Spectral")
9391pal_c <- hcl.colors(9, "Inferno")[-9]
9492
95- a <- p + plotLabel(x, i) # simple binary image
96- b <- p + plotLabel(x, i, c = "id", pal=pal_d) # 'colData'
97- c <- p + plotLabel(x, i, c = "channel_1_sum", pal=pal_c) +
98- theme(legend.key.width=unit(1, "lines")) # 'assay'
93+ a <- p + plotLabel(x, i, pal="grey") # binary
94+ b <- p + plotLabel(x, i, c="id", pal=pal_d) # metadata
95+ c <- p + plotLabel(x, i, c="channel_1_sum", pal=pal_c) # assay
9996
10097(a | b | c) +
10198 plot_layout(guides="collect") &
@@ -105,24 +102,17 @@ c <- p + plotLabel(x, i, c = "channel_1_sum", pal=pal_c) +
105102
106103### Points
107104
108- ``` {r plotPoint, eval=FALSE, fig.width=8 , fig.height=2.5 }
105+ ``` {r plotPoint, fig.width=10 , fig.height=3 }
109106i <- "blobs_points"
110- p <- plotSpatialData()
111- # mock up a 'table'
112- f <- list(
113- numbers=\(n) runif(n),
114- letters=\(n) sample(letters, n, TRUE))
115- y <- setTable(x, i, f)
116- # demo. viz. capabilities
117- a <- p + plotPoint(y, i)
118- b <- p + plotPoint(y, i, "letters") # discrete coloring
119- c <- p + plotPoint(y, i, "numbers") # continuous coloring
120- a | b | c
107+ a <- p + plotPoint(x, i)
108+ b <- p + plotPoint(x, i, col="genes") # discrete
109+ c <- p + plotPoint(x, i, col="instance_id") # continuous
110+ (a | b | c)
121111```
122112
123113### Shapes
124114
125- ``` {r plotShape, fig.width=6 , fig.height=2.5 }
115+ ``` {r plotShape, fig.width=8 , fig.height=3 }
126116p <- plotSpatialData()
127117a <- p +
128118 ggtitle("polygons") +
@@ -133,7 +123,7 @@ b <- p +
133123c <- p +
134124 ggtitle("circles") +
135125 plotShape(x, "blobs_circles")
136- wrap_plots(a, b, c)
126+ (a | b | c)
137127```
138128
139129### Layering
@@ -147,15 +137,15 @@ all <- p +
147137 plotShape(x, 1) +
148138 plotShape(x, 3) +
149139 new_scale_color() +
150- plotPoint(x, c ="genes") +
140+ plotPoint(x, col ="genes") +
151141 ggtitle("layered")
152142# split
153143one <- list(
154144 p + plotImage(x) + ggtitle("image"),
155145 p + plotLabel(x) + ggtitle("labels"),
156146 p + plotShape(x, 1) + ggtitle("circles"),
157147 p + plotShape(x, 3) + ggtitle("polygons"),
158- p + plotPoint(x, c ="genes") + ggtitle("points"))
148+ p + plotPoint(x, col ="genes") + ggtitle("points"))
159149wrap_plots(c(list(all), one), nrow=2)
160150```
161151
@@ -176,176 +166,52 @@ There are only `r length(SpatialData::shape(x, "cells"))` cells, but
176166` r format(length(SpatialData::point(x, "single_molecule")), big.mark=",") `
177167molecules, so that we downsample a random subset of 1,000 for visualization:
178168
179- ``` {r merfish-plot}
180- # downsample 1,000 points
181- n <- length(p <- point(x))
182- q <- p[sample(n, 1e3)]
183- (point(x, "1k") <- q)
169+ ``` {r merfish-plot, fig.width=6, fig.height=4}
184170# layered visualization
185171plotSpatialData() +
186172 plotImage(x, c="white") +
187- new_scale_color() +
188- plotPoint(x, i="1k", c="cell_type", size=0.2) +
189- new_scale_color() +
190- plotShape(x, i="anatomical") +
191- scale_color_manual(values=hcl.colors(6, "Spectral"))
192- ```
193-
194- ``` {r include=FALSE}
195- knitr::opts_chunk$set(eval=FALSE)
196- ```
197-
198- ``` {r merfish-box}
199- # bounding-box query
200- qu <- list(xmin=1800, xmax=2400, ymin=5000, ymax=5400)
201- bb <- geom_rect(do.call(aes, qu), data.frame(qu), col="yellow", fill=NA)
202- y <- SpatialData(images=list("."=do.call(query, c(list(x=image(x)), qu))))
203- plotSpatialData() + plotImage(x) + bb | plotSpatialData() + plotImage(y)
204- ```
205-
206- ## VisiumHD
207-
208- Mouse intestine, 1GB; 4 image resolutions and 3 shapes at 2, 8, and 16 $\mu$m.
209-
210- ``` {r visiumhd-read, eval=FALSE}
211- dir.create(td <- tempfile())
212- pa <- MouseIntestineVisHD(target=td)
213- (x <- readSpatialData(pa, images=4, shapes=3))
214- ```
215-
216- ``` {r visiumhd-plot, eval=FALSE}
217- qu <- list(xmin=100, xmax=300, ymin=200, ymax=400)
218- bb <- geom_rect(do.call(aes, qu), data.frame(qu), col="black", fill=NA)
219- y <- SpatialData(images=list("."=do.call(query, c(list(x=image(x)), qu))))
220- plotSpatialData() + plotImage(x) + bb | plotSpatialData() + plotImage(y)
173+ plotPoint(x, n=1e3, col="cell_type", size=0.5) +
174+ scale_color_manual(values=rainbow(8)) +
175+ guides(col=guide_legend(override.aes=list(size=2))) +
176+ plotShape(x, i="anatomical", fill=NA, col="white", linewidth=1)
177+ ```
178+
179+ ``` {r merfish-box, fig.width=7, fig.height=6}
180+ # subset & downsample for speed
181+ y <- x[c("images", "points"), ]
182+ n <- length(point(y))
183+ i <- sample(n, 1e5)
184+ point(y) <- point(y)[i]
185+ # polygon queries
186+ lapply(seq_along(shape(x)), \(s) {
187+ df <- data(shape(x)[s, ])
188+ z <- crop(y, sf::st_as_sf(df))
189+ plotSpatialData() +
190+ plotImage(z) +
191+ plotPoint(z, n=1e3, size=1/3, col="cyan")
192+ }) |> wrap_plots(nrow=2) & theme(axis.text.x=element_text(angle=45, hjust=1))
221193```
222194
223195## MibiTOF
224196
225197Colorectal carcinoma, 25 MB; no shapes, no points.
226198
227199``` {r mibitof-read}
228- dir.create(td <- tempfile())
229- pa <- SpatialData.data:::.unzip_spd_demo(
230- zipname="mibitof.zip",
231- dest=td, source="biocOSN")
232- (x <- readSpatialData(pa))
233- ```
234-
235- ``` {r mibitof-plot, fig.width=10, fig.height=3}
236- pal <- hcl.colors(8, "Spectral")
237- wrap_plots(nrow=1, lapply(seq(3), \(.)
238- plotSpatialData() + plotImage(x, .) +
239- plotLabel(x, ., c = "Cluster", pal=pal))) +
240- plot_layout(guides="collect")
241- ```
242-
243- ## CyCIF (MCMICRO)
244-
245- Small lung adenocarcinoma, 250 MB; 1 image, 2 labels, 2 tables.
246-
247- ``` {r mcmicro-read}
248- dir.create(td <- tempfile())
249- pa <- SpatialData.data:::.unzip_spd_demo(
250- zipname="mcmicro_io.zip",
251- dest=td, source="biocOSN")
252- (x <- readSpatialData(pa))
200+ (x <- ColorectalCarcinomaMIBITOF())
253201```
254202
255- Getting channel names for the image:
256-
257- ``` {r mcmicro-channels}
258- chs <- channels(image(x))
259- ```
260-
261- Plotting with multiple image channels:
262-
263- ``` {r mcmicro-plot}
264- plotSpatialData() + plotImage(x,
265- ch=chs,
266- c=grDevices::palette.colors(length(chs), palette = "Polychrome 36")
267- )
268- ```
269-
270- We can specify contrast limits for each channel via the ` cl ` argument, but if not provided, they will be automatically computed as the 5th and 95th percentiles of the pixel intensities for each channel.
271-
272- ## IMC (Steinbock)
273-
274- 4 different cancers (SCCHN, BCC, NSCLC, CRC), 820 MB; 14 images, 14 labels, 1 table.
275-
276- ``` {r steinbock-read}
277- dir.create(td <- tempfile())
278- pa <- SpatialData.data:::.unzip_spd_demo(
279- zipname="steinbock_io.zip",
280- dest=td, source="biocOSN")
281- x <- readSpatialData(pa)
282- ```
283-
284- ### channels
285-
286- ``` {r steinbock-ch}
287- plotSpatialData() + plotImage(x,
288- i="Patient3_003_image",
289- ch=c(6, 22, 39),
290- c=c("blue", "cyan", "yellow"))
203+ ``` {r mibitof-plot, fig.width=9, fig.height=3.5}
204+ ps <- lapply(imageNames(x), \(i) plotSpatialData() + plotImage(x, i) + ggtitle(i))
205+ wrap_plots(ps, nrow=1)
291206```
292207
293- ### contrasts
294-
295- ``` {r steinbock-cl, fig.width=9, fig.height=3}
296- i <- image(x, "Patient3_003_image")
297- image(x, "crop") <- i[, 200:400, 200:400]
298- lapply(list(c(0.2, 1), c(0, 0.8), c(0, 1.2)), \(.) {
299- plotSpatialData() + plotImage(x,
300- i="crop",
301- ch=c(6, 22, 39),
302- cl=list(1, 1, .),
303- c=c("blue", "cyan", "yellow")) +
304- ggtitle(sprintf("[%s, %s]", .[1], .[2]))
305- }) |> wrap_plots(nrow=1) + plot_layout(guides="collect")
306- ```
307-
308- # Masking
309-
310- Back to blobs...
311-
312- ``` {r read-mask}
313- x <- file.path("extdata", "blobs.zarr")
314- x <- system.file(x, package="SpatialData")
315- x <- readSpatialData(x, tables=FALSE)
316- ```
317-
318- ``` {r plot-mask-one, fig.width=8, fig.height=3.5}
319- i <- "blobs_circles"
320- x <- mask(x, "blobs_points", i)
321- (t <- getTable(x, i))
322- p <- plotSpatialData() +
323- plotPoint(x, c="genes") +
324- scale_color_manual(values=c("tomato", "cornflowerblue")) +
325- new_scale_color()
326- lapply(names(c <- c(a="red", b="blue")), \(.)
327- p + plotShape(x, i, c=paste0("gene_", .)) +
328- scale_color_gradient2(
329- low="grey", high=c[.],
330- limits=c(0, 8), n.breaks=5)) |>
331- wrap_plots() + plot_layout(guides="collect")
332- ```
333-
334- ``` {r plot-mask-two, fig.width=8, fig.height=3.5}
335- # compute channel-wise means
336- i <- "blobs_labels"
337- table(x) <- NULL
338- x <- mask(x, "blobs_image", i, fun=mean)
339- (t <- getTable(x, i))
340- # visualize side-by-side
341- ps <- lapply(paste(seq_len(3)), \(.)
342- plotSpatialData() + plotLabel(x, i, c = .) +
343- ggtitle(paste("channel", ., "sum")))
344- wrap_plots(ps, nrow=1) & theme(
345- legend.position="bottom",
346- legend.title=element_blank(),
347- legend.key.width=unit(1, "lines"),
348- legend.key.height=unit(0.5, "lines"))
208+ ``` {r fig.width=8, fig.height=4}
209+ # bounding-box query
210+ bb <- list(
211+ xmin=0, xmax=800,
212+ ymin=500, ymax=900)
213+ y <- crop(x["images", 1], bb)
214+ plotSpatialData() + plotImage(y)
349215```
350216
351217# Session info
0 commit comments