5858# ' @export
5959setMethod ("plotLabel ", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL,
6060 a = 0.5 , pal = c(" red" , " green" ), nan = NA , assay = 1 , z = NULL ) {
61-
6261 if (is.numeric(i )) i <- labelNames(x )[i ]
6362 i <- match.arg(i , labelNames(x ))
6463 y <- label(x , i )
@@ -72,10 +71,12 @@ setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL,
7271 ym <- .get_multiscale_data(y , k )
7372 if (length(dim(ym )) > 2 ) {
7473 if (is.null(z )) {
74+ # max-projection across z-slices
7575 nm <- vapply(axes(y ), \(. ) . $ name , character (1 ))
7676 yx <- match(c(" y" , " x" ), nm )
7777 ym <- apply(ym , yx , max )
7878 } else {
79+ # subset target z-slice
7980 ym <- ym [z ,,]
8081 }
8182 }
@@ -84,19 +85,23 @@ setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL,
8485 # and thus save memory by not plotting all pixels
8586 idx <- BiocGenerics :: which(ym != 0L , arr.ind = TRUE )
8687
87- # offset & multi-scale adjustment
88+ # offset & multi-scale adjustment
89+ ds <- dim(ym )
8890 wh <- .get_wh(y )
89- t <- .get_multiscale_scale(y )
90- tx <- t [length(t )]
91- ty <- t [length(t )- 1L ]
92- .x <- tx * idx [, 2L ]
93- .y <- ty * idx [, 1L ]
94- mx <- dim(ym )[2 ]* tx
95- my <- dim(ym )[1 ]* ty
91+ if (wh $ w [2 ] == tail(dim(y ), 1 ) ||
92+ wh $ h [2 ] == tail(dim(y ), 2 )[1 ]) {
93+ ts <- .get_multiscale_scale(y )
94+ tx <- tail(ts , 1 )
95+ ty <- tail(ts , 2 )[1 ]
96+ } else tx <- ty <- 1
97+ nx <- tail(ds , 1 )
98+ ny <- tail(ds , 2 )[1 ]
99+ sx <- (diff(wh $ w )/ nx )* tx
100+ sy <- (diff(wh $ h )/ ny )* ty
96101 df <- data.frame (
97- x = .x * ( wh $ w [2 ] / mx ) + wh $ w [ 1 ],
98- y = .y * ( wh $ h [2 ] / my ) + wh $ h [ 1 ],
99- z = ym [idx ])
102+ x = wh $ w [1 ] + idx [, 2L ] * sx ,
103+ y = wh $ h [1 ] + idx [, 1L ] * sy ,
104+ z = ym [idx ])
100105
101106 aes <- aes(.data [[" x" ]], .data [[" y" ]])
102107 if (! is.null(c )) {
0 commit comments