diff --git a/DESCRIPTION b/DESCRIPTION index b7f19f1..2e0439c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -67,8 +67,8 @@ biocViews: GeneExpression, Transcriptomics License: Artistic-2.0 -RoxygenNote: 7.3.3 Encoding: UTF-8 VignetteBuilder: knitr URL: https://github.com/HelenaLC/SpatialData.plot BugReports: https://github.com/HelenaLC/SpatialData.plot/issues +Config/roxygen2/version: 8.0.0 diff --git a/NAMESPACE b/NAMESPACE index 0520092..daf80cb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,8 +25,8 @@ importFrom(ggplot2,element_line) importFrom(ggplot2,element_text) importFrom(ggplot2,geom_blank) importFrom(ggplot2,geom_point) -importFrom(ggplot2,geom_raster) importFrom(ggplot2,geom_sf) +importFrom(ggplot2,geom_tile) importFrom(ggplot2,ggplot) importFrom(ggplot2,guide_legend) importFrom(ggplot2,guides) diff --git a/R/plotImage.R b/R/plotImage.R index af979e4..3718db1 100644 --- a/R/plotImage.R +++ b/R/plotImage.R @@ -178,11 +178,8 @@ NULL list(w=df[, 1], h=df[, 2]) } -#' @importFrom ggplot2 guides geom_point -#' geom_blank annotation_raster -#' scale_color_identity -#' scale_x_continuous -#' scale_y_reverse +#' @importFrom ggplot2 guides geom_point geom_blank annotation_raster +#' @importFrom ggplot2 scale_color_identity scale_x_continuous scale_y_reverse .gg_i <- \(x, w, h, pal=NULL) { l <- if (!is.null(names(pal))) list( guides(col=guide_legend(override.aes=list(alpha=1, size=2))), diff --git a/R/plotLabel.R b/R/plotLabel.R index 4718a46..9b7b13e 100644 --- a/R/plotLabel.R +++ b/R/plotLabel.R @@ -18,8 +18,7 @@ #' @param nan character string; color for missing values (hidden by default). #' #' @examples -#' x <- file.path("extdata", "blobs.zarr") -#' x <- system.file(x, package="SpatialData") +#' x <- system.file("extdata", "blobs.zarr", package="SpatialData") #' x <- readSpatialData(x) #' #' i <- "blobs_labels" @@ -49,9 +48,9 @@ NULL #' @importFrom S4Vectors metadata #' @importFrom rlang .data #' @importFrom methods as -#' @importFrom ggplot2 -#' scale_fill_manual scale_fill_gradientn -#' aes geom_raster theme unit guides guide_legend +#' @importFrom ggplot2 scale_fill_manual scale_fill_gradientn +#' @importFrom ggplot2 aes theme unit guides guide_legend geom_tile +#' #' @importFrom SingleCellExperiment colData #' @export setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL, @@ -59,13 +58,17 @@ setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL, if (is.numeric(i)) i <- labelNames(x)[i] i <- match.arg(i, labelNames(x)) y <- label(x, i) - ym <- as.matrix(.get_multiscale_data(label(x, i), k)) - df <- data.frame(x=c(col(ym)), y=c(row(ym)), z=c(ym)) # transformation if (is.numeric(j)) j <- CTname(y)[j] - ts <- CTpath(x, i, j) - df[,c("x", "y")] <- .trans_xy(df[,c("x", "y")], ts) + y <- transform(y, j) + ym <- .get_multiscale_data(y, k) + wh <- .get_wh(y) + + # Keep only indices != 0 since labels might be sparse and thus save memory by not plotting all pixels + idx <- BiocGenerics::which(ym != 0L, arr.ind=TRUE) + # All other SD elements are flipped when plotted. Let's keep the same convention here. + df <- data.frame(x=idx[,2L]+wh$w[1], y=idx[,1L]+wh$h[1], z=ym[idx]) aes <- aes(.data[["x"]], .data[["y"]]) if (!is.null(c)) { stopifnot(length(c) == 1, is.character(c)) @@ -83,24 +86,23 @@ setMethod("plotLabel", "SpatialData", \(x, i=1, j=1, k=NULL, c=NULL, df$z <- getTable(x, i, c, assay=assay)[idx] if (c == ik) df$z <- factor(df$z) aes$fill <- aes(.data[["z"]])[[1]] - switch(scale_type(df$z), + thm <- switch(scale_type(df$z), discrete={ - val <- sort(setdiff(unique(df$z), NA)) + val <- sort(unique(df$z), na.last=NA) pal <- colorRampPalette(pal)(length(val)) - thm <- list( + list( theme(legend.key.size=unit(0.5, "lines")), guides(fill=guide_legend(override.aes=list(alpha=1))), scale_fill_manual(c, values=pal, breaks=val, na.value=nan)) }, - continuous=thm <- list( + continuous=list( theme(legend.key.size=unit(0.5, "lines")), scale_fill_gradientn(c, colors=pal, na.value=nan))) } else { - thm <- guides(fill="none") aes$fill <- aes(.data$z != 0)[[1]] thm <- list( theme(legend.position="none"), scale_fill_manual(NULL, values=pal)) } - list(thm, do.call(geom_raster, list(data=df, mapping=aes, alpha=a))) + list(thm, do.call(geom_tile, list(data=df, mapping=aes, alpha=a))) }) \ No newline at end of file diff --git a/R/trans.R b/R/trans.R deleted file mode 100644 index 0ee4f31..0000000 --- a/R/trans.R +++ /dev/null @@ -1,39 +0,0 @@ -# TODO: deprecate this; all should be handled by SD - -# count occurrences of each coordinate space; -# return most frequent (in order of appearance) -.guess_space <- \(x) { - nms <- lapply(rownames(x), \(l) - lapply(colnames(x)[[l]], \(e) - CTname(x[[l]][[e]]))) - cnt <- table(nms <- unlist(nms)) - cnt <- cnt[unique(nms)] - names(which.max(cnt)) -} - -.trans_xy <- \(xy, ts, rev=FALSE) { - if (rev) ts <- rev(ts) - for (. in seq_along(ts)) { - t <- ts[[.]]$type - d <- ts[[.]]$data - d <- unlist(d) - if (length(d) == 3) - d <- d[-1] - switch(t, - identity={}, - scale={ - op <- ifelse(rev, `/`, `*`) - xy$x <- op(xy$x, d[2]) - xy$y <- op(xy$y, d[1]) - }, - rotate={ - xy <- xy %*% .R(d*pi/180) - }, - translation={ - op <- ifelse(rev, `-`, `+`) - xy$x <- op(xy$x, d[2]) - xy$y <- op(xy$y, d[1]) - }) - } - return(xy) -} diff --git a/man/plotLabel.Rd b/man/plotLabel.Rd index 02bc7cf..3dc738e 100644 --- a/man/plotLabel.Rd +++ b/man/plotLabel.Rd @@ -46,8 +46,7 @@ specifies which \code{assay} data to use (see \code{\link{valTable}}).} \code{SpatialData} label viz. } \examples{ -x <- file.path("extdata", "blobs.zarr") -x <- system.file(x, package="SpatialData") +x <- system.file("extdata", "blobs.zarr", package="SpatialData") x <- readSpatialData(x) i <- "blobs_labels"