Skip to content

Commit 6d2ca3a

Browse files
committed
plot label draft
1 parent 527c69b commit 6d2ca3a

File tree

6 files changed

+65
-1
lines changed

6 files changed

+65
-1
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export(sd_osn_list)
1818
export(sd_osn_load)
1919
export(sd_plot)
2020
export(sd_plot_image)
21+
export(sd_plot_label)
2122
export(sd_plot_point)
2223
export(sd_plot_shape)
2324
export(sdio_list)

R/plot-image.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#' @importFrom DelayedArray realize
1818
#' @export
1919
sd_plot_image <- \(x, i=1, k=NULL, c=NULL, ch=NULL, cl=NULL, w=800, h=800) {
20-
x <- sd; i <- 1; w=h=800; ch <- cl <- c <- k <- NULL; ch <- 2
20+
#x <- sd; i <- 1; w=h=800; ch <- cl <- c <- k <- NULL; ch <- 2
2121
ia <- x@images[[i]]
2222
ch <- .ch_idx(ia, ch)
2323
if (is.null(k))

R/plot-label.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#' @name sd_plot_label
2+
#' @title Plot `LabelArray`
3+
#'
4+
#' @examples
5+
#' pa <- file.path("extdata", "blobs.zarr")
6+
#' pa <- system.file(pa, package="SpatialData")
7+
#' sd <- readSpatialData(pa)
8+
#'
9+
#' devtools::load_all()
10+
#' sd_plot() + sd_plot_label(sd)
11+
#'
12+
#' @importFrom methods as
13+
#' @importFrom grDevices rgb
14+
#' @importFrom DelayedArray realize
15+
#' @export
16+
sd_plot_label <- \(x, i=1, k=NULL, c=NULL, w=800, h=800) {
17+
#x <- sd; i <- 1; w=h=800; c <- k <- NULL; ch <- 2
18+
la <- x@labels[[i]]
19+
if (is.null(k))
20+
k <- .guess_scale(la, w, h)
21+
a <- data(la, k)
22+
dt <- .get_dt(la)
23+
a <- as(a, "DelayedArray")
24+
dim(a) <- c(1, dim(a))
25+
n <- length(setdiff(unique(c(a)), 0))
26+
a <- do.call(arbind, replicate(n, a))
27+
a <- .norm_ia(realize(a), dt)
28+
a <- .chs2rgb(a, 5, c)
29+
a <- apply(a, c(2, 3), \(.) do.call(rgb, as.list(.)))
30+
w <- c(0, dim(ia)[3])
31+
h <- c(0, dim(ia)[2])
32+
# TODO: coloring by value?
33+
pal <- NULL
34+
lgd <- if (!is.null(pal)) list(
35+
guides(col=guide_legend(override.aes=list(alpha=1, size=2))),
36+
scale_color_identity(NULL, guide="legend", labels=names(pal)),
37+
geom_point(aes(col=.data$foo), data.frame(foo=pal), x=0, y=0, alpha=0))
38+
list(lgd,
39+
scale_x_continuous(limits=w), scale_y_reverse(limits=rev(h)),
40+
annotation_raster(a, w[2],w[1], h[1],h[2], interpolate=FALSE))
41+
}

R/plot.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#'
2020
#' @seealso
2121
#' \code{\link{sd_plot_image}}
22+
#' \code{\link{sd_plot_label}}
2223
#' \code{\link{sd_plot_point}}
2324
#' \code{\link{sd_plot_shape}}
2425
#'

man/sd_plot.Rd

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

man/sd_plot_label.Rd

Lines changed: 20 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)