Skip to content

Commit 27aeb1b

Browse files
committed
add shape plotting
1 parent 5f76537 commit 27aeb1b

File tree

8 files changed

+235
-21
lines changed

8 files changed

+235
-21
lines changed

NAMESPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export(sd_osn_list)
1818
export(sd_osn_load)
1919
export(sd_plot)
2020
export(sd_plot_image)
21+
export(sd_plot_point)
22+
export(sd_plot_shape)
2123
export(sdio_list)
2224
export(zattrs)
2325
import(geoarrow)
@@ -46,11 +48,14 @@ importFrom(S7,new_generic)
4648
importFrom(S7,new_union)
4749
importFrom(SingleCellExperiment,"int_metadata<-")
4850
importFrom(SingleCellExperiment,int_metadata)
51+
importFrom(SummarizedExperiment,assay)
4952
importFrom(SummarizedExperiment,colData)
5053
importFrom(anndataR,read_zarr)
5154
importFrom(arrow,open_dataset)
5255
importFrom(grDevices,col2rgb)
5356
importFrom(grDevices,rgb)
5457
importFrom(methods,as)
5558
importFrom(methods,is)
59+
importFrom(rlang,.data)
60+
importFrom(sfarrow,read_sf_dataset)
5661
importFrom(utils,.DollarNames)

R/plot-point.R

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#' @name sd_plot_point
2+
#' @title Plot `PointFrame`
3+
#'
4+
#' @param x \code{\link{SpatialData}} object.
5+
#' @param i scalar integer or string;
6+
#' specifies which \code{points} to plot.
7+
#' @param c string; can be a color name or
8+
#' \code{PointFrame} column to color by.
9+
#' @param a assay to use when coloring by \code{tables}.
10+
#' @param ... (optional) aesthetics passed to \code{geom_point}.
11+
#'
12+
#' @return \code{ggplot}
13+
#'
14+
#' @examples
15+
#' pa <- file.path("extdata", "blobs.zarr")
16+
#' pa <- system.file(pa, package="SpatialData")
17+
#' sd <- readSpatialData(pa)
18+
#'
19+
#' sd_plot() + sd_plot_point(sd, c="x")
20+
#' sd_plot() + sd_plot_point(sd, c="pink", size=1)
21+
#'
22+
#' @importFrom SingleCellExperiment int_metadata
23+
#' @importFrom SummarizedExperiment assay
24+
#' @importFrom rlang .data
25+
#' @import ggplot2
26+
#' @export
27+
sd_plot_point <- \(x, i, c, a, ...) {
28+
dot <- list(...)
29+
if (missing(c)) c <- NULL
30+
if (missing(i)) i <- 1
31+
if (is.numeric(i)) i <- names(x)$points[i]
32+
p <- x@points[[i]]
33+
df <- as.data.frame(p@data)
34+
aes <- aes(.data[["x"]], .data[["y"]])
35+
lgd <- "none"
36+
if (!is.null(c)) {
37+
if (.str_is_col(c)) {
38+
dot$colour <- c
39+
} else if (c %in% names(df)) {
40+
aes$colour <- aes(.data[[c]])[[1]]
41+
} else {
42+
stopifnot(length(x@tables) > 0)
43+
id <- int_metadata(t)$zattrs$region
44+
stopifnot(id == i)
45+
t <- x@tables[[1]]
46+
ik <- instance_key(p)
47+
idx <- match(df[[ik]], t[[ik]])
48+
a <- ifelse(missing(a), 1, a)
49+
if (c %in% rownames(t))
50+
t[[c]] <- assay(t, a)[c, ]
51+
df[[c]] <- t[[c]][idx]
52+
aes$colour <- aes(.data[[c]])[[1]]
53+
}
54+
if (c %in% names(df))
55+
lgd <- scale_type(df[[c]])
56+
}
57+
arg <- c(list(data=df, mapping=aes), dot)
58+
list(
59+
do.call(geom_point, arg),
60+
switch(lgd,
61+
discrete=list(
62+
theme(legend.key.size=unit(0, "lines")),
63+
guides(col=guide_legend(override.aes=list(alpha=1, size=2)))
64+
),
65+
continuous=list(
66+
theme(
67+
legend.key.width=unit(0.4, "lines"),
68+
legend.key.height=unit(0.8, "lines")),
69+
scale_color_gradientn(colors=pals::jet())
70+
)
71+
)
72+
)
73+
}

R/plot-shape.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#' @name sd_plot_shape
2+
#' @title Plot `ShapeFrame`
3+
#'
4+
#' @param x \code{\link{SpatialData}} object.
5+
#' @param i scalar integer or string;
6+
#' specifies which \code{shapes} to plot.
7+
#' @param c string; can be a color name,
8+
#' or TRUE to color by shape identifier.
9+
#' @param ... (optional) aesthetics passed to \code{geom_sf}.
10+
#'
11+
#' @return \code{ggplot}
12+
#'
13+
#' @examples
14+
#' pa <- file.path("extdata", "blobs.zarr")
15+
#' pa <- system.file(pa, package="SpatialData")
16+
#' sd <- readSpatialData(pa)
17+
#'
18+
#' sd_plot() + sd_plot_shape(sd, 1) # point
19+
#' sd_plot() + sd_plot_shape(sd, 3) # polygon
20+
#' sd_plot() + sd_plot_shape(sd, 2) # multi-polygon
21+
#'
22+
#' # aesthetics
23+
#' sd_plot() + sd_plot_shape(sd, 3,
24+
#' fill=NA, c="red", linewidth=1)
25+
#'
26+
#' @importFrom sfarrow read_sf_dataset
27+
#' @importFrom rlang .data
28+
#' @import ggplot2
29+
#' @export
30+
sd_plot_shape <- \(x, i=1, c=TRUE, ...) {
31+
if (is.numeric(i))
32+
i <- names(x)$shapes[i]
33+
s <- x@shapes[[i]]
34+
df <- read_sf_dataset(s@data)
35+
aes <- aes()
36+
thm <- list()
37+
dot <- list(...)
38+
if (isTRUE(c)) {
39+
df$.id <- factor(seq(nrow(df)))
40+
aes$colour <- aes(.data$.id)[[1]]
41+
thm <- c(thm, list(guides(col="none")))
42+
} else if (.str_is_col(c)) {
43+
ex <- grepv("col", names(dot))
44+
dot <- dot[setdiff(names(dot), ex)]
45+
dot$colour <- c
46+
}
47+
if (!is.null(df$radius))
48+
aes$size <- aes(.data$radius)[[1]]
49+
arg <- c(list(data=df, mapping=aes), dot)
50+
list(do.call(geom_sf, arg), thm)
51+
}

R/plot-utils.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#' @importFrom grDevices col2rgb
2+
.str_is_col <- \(x) !inherits(tryCatch(error=\(e) e, col2rgb(x)), "error")

R/plot.R

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,34 @@
22
#' @title Plotting `SpatialData`
33
#'
44
#' @aliases sd_plot sd_plot_image
5-
#'
6-
#' @import ggplot2
5+
#'
6+
#' @return \code{ggplot}
7+
#'
78
#' @examples
89
#' pa <- file.path("extdata", "blobs.zarr")
910
#' pa <- system.file(pa, package="SpatialData")
1011
#' sd <- readSpatialData(pa)
1112
#'
12-
#' sd_plot() + sd_plot_image(sd)
13+
#' sd_plot() +
14+
#' sd_plot_image(sd) +
15+
#' sd_plot_point(sd, c="x")
1316
#'
1417
#' pal <- c("cyan", "magenta", "gold")
1518
#' sd_plot() + sd_plot_image(sd, c=pal)
16-
NULL
19+
#'
20+
#' @import ggplot2
21+
#' @export
22+
sd_plot <- \() ggplot() +
23+
#scale_y_reverse() +
24+
coord_equal() +
25+
theme_bw() + theme(
26+
panel.grid=element_blank(),
27+
legend.key=element_blank(),
28+
legend.key.size=unit(0, "lines"),
29+
legend.background=element_blank(),
30+
plot.title=element_text(hjust=0.5),
31+
axis.text=element_text(color="grey"),
32+
axis.ticks=element_line(color="grey"))
1733

1834
# default colors (from ImageJ/Fiji)
1935
.DEFAULT_COLORS <- c("red", "green", "blue", "gray", "cyan", "magenta", "yellow")
@@ -175,17 +191,3 @@ sd_plot_image <- \(x, i=1, k=NULL, c=NULL, ch=NULL, cl=NULL, w=800, h=800) {
175191
scale_x_continuous(limits=w), scale_y_reverse(limits=rev(h)),
176192
annotation_raster(a, w[2],w[1], h[1],h[2], interpolate=FALSE))
177193
}
178-
179-
#' @rdname sd_plot
180-
#' @export
181-
sd_plot <- \() ggplot() +
182-
#scale_y_reverse() +
183-
coord_equal() +
184-
theme_bw() + theme(
185-
panel.grid=element_blank(),
186-
legend.key=element_blank(),
187-
legend.key.size=unit(0, "lines"),
188-
legend.background=element_blank(),
189-
plot.title=element_text(hjust=0.5),
190-
axis.text=element_text(color="grey"),
191-
axis.ticks=element_line(color="grey"))

man/sd_plot.Rd

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sd_plot_point.Rd

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sd_plot_shape.Rd

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)