Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SpatialData.plot
Title: SpatialData visualization
Depends: R (>= 4.6), SpatialData
Version: 0.99.5
Version: 0.99.6
Description: Visualization suit for 'SpatialData' (R). Current functionality
includes handling of multiscale 'images', visualizing 'labels', 'points',
and 'shapes'. For the latter, POINT, POLYGON, and MULTIPOLYGON geometries
Expand Down
14 changes: 9 additions & 5 deletions R/plotFrame.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @param i character string or index; the label element to plot.
#' @param assay character string; in case of \code{c} denoting a row name,
#' specifies which \code{assay} data to use (see \code{\link{valTable}}).
#' (ignored when \code{x} is a \code{PointFrame}).
#' (ignored when \code{x} is a \code{SpatialDataPoint}).
#'
#' @examples
#' x <- file.path("extdata", "blobs.zarr")
Expand Down Expand Up @@ -38,18 +38,19 @@ NULL
#' @importFrom ggplot2 aes theme scale_type geom_sf coord_sf
#' @importFrom SpatialData transform
#' @importFrom ggforce geom_circle
#' @importFrom methods is
#' @importFrom utils tail
.plot <- \(x, y, key=NULL, n=Inf, assay=1, i=1, ...) {
if (is(y, "PointFrame")) {
if (is(y, "SpatialDataPoint")) {
if (!is.null(key)) {
fk <- feature_key(y)
y@data <- dplyr::filter(data(y), .data[[fk]] %in% key)
y<- dplyr::filter(y, .data[[fk]] %in% key)
}
}
if (is.finite(n)) {
n <- min(length(y), n)
y <- y[sample(length(y), n)]
if (is(y, "ShapeFrame")) {
if (is(y, "SpatialDataShape")) {
shape(x, i) <- y
} else {
point(x, i) <- y
Expand All @@ -72,11 +73,14 @@ NULL
if (val %in% names(df)) {
if (scale_type(df[[arg]]) == "discrete")
df[[val]] <- factor(df[[arg]])
aes[[arg]] <- aes(.data[[val]])[[1]]
col <- match(arg, c("col", "color", "colour"))
.arg <- ifelse(!is.na(col), "colour", arg)
aes[[.arg]] <- aes(.data[[val]])[[1]]
dot[[arg]] <- NULL
}
}
}

if ("radius" %in% names(df))
df <- st_buffer(df, df$radius)
list(
Expand Down
4 changes: 2 additions & 2 deletions R/plotImage.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' 800 x 800px; use Inf to plot the lowest resolution available.
#' @param ch image channel(s) to be used for plotting (defaults to
#' the first channel(s) available); use \code{channels()} to see
#' which channels are available for a given \code{ImageArray}
#' which channels are available for a given \code{SpatialDataImage}
#' @param c character vector; colors to use for each channel.
#' @param cl list of length-2 numeric vectors (non-negative, increasing);
#' specifies channel-wise contrast limits - defaults to [0, 1] for all
Expand Down Expand Up @@ -128,7 +128,7 @@ NULL
#' @importFrom methods is
#' @noRd
.is_rgb <- \(x) {
if (is(x, "ImageArray") &&
if (is(x, "SpatialDataImage") &&
!is.null(md <- meta(x)))
x <- channels(x)
if (!is.vector(x)) stop("invalid 'x'")
Expand Down
7 changes: 7 additions & 0 deletions inst/NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
changes in version 0.99.6

- renaming of 'SpatialData' classes
- improved visualization of labels
- fixed auto-contrasting for HnE images
- fixed channel-merging for multi-plex images

changes in version 0.99.5

- fix visualization of circles
Expand Down
2 changes: 1 addition & 1 deletion man/plotFrame.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/plotImage.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4,413 changes: 2,209 additions & 2,204 deletions tests/testthat/_snaps/plotSpatialData/overlays.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testthat/test-plotArray.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test_that("plotSpatialData()", {
}

test_that(".guess_scale", {
img <- ImageArray(
img <- SpatialDataImage(
lys <- lapply(
dim <- lapply(c(6, 3), \(.) c(3, rep(., 2))), \(.)
array(sample(seq_len(255), prod(.), replace=TRUE), dim=.)))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-plotImage.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test_that(".is_rgb()", {
expect_false(.is_rgb(c("r", "g", "g")))
expect_true(.is_rgb(. <- c("r", "g", "b")))
expect_true(.is_rgb(rev(.)))
# only works for 'ImageArray'
# only works for 'SpatialDataImage'
expect_true(.is_rgb(image(x, 1)))
expect_error(.is_rgb(label(x, 1)))
})
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-plotShape.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require(sf, quietly=TRUE)
require(ggplot2, quietly=TRUE)
require(SpatialData, quietly=TRUE)

x <- file.path("extdata", "blobs.zarr")
x <- system.file(x, package="SpatialData")
x <- readSpatialData(x, tables=FALSE)
Expand Down Expand Up @@ -40,7 +42,7 @@ test_that("plotShape(),polygons", {
geom <- layer_data(q)$geometry
expect_s3_class(q, "ggplot")
df <- centroids(y)
fd <- st_coordinates(geom)
fd <- st_coordinates(st_centroid(geom))
.f <- \(.) as.matrix(.[,c(1,2)])
expect_equivalent(.f(df), .f(fd))
expect_s3_class(geom, "sfc_POLYGON")
Expand Down
44 changes: 22 additions & 22 deletions tests/testthat/test-plotSpatialData.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
test_that("regression test of overlays", {
x <- system.file("extdata", "blobs.zarr", package="SpatialData") |>
readSpatialData()

p <- plotSpatialData()
# joint
all <- p +
plotImage(x) +
plotLabel(x, a=1/3) +
plotShape(x, 1) +
plotShape(x, 3) +
plotPoint(x, c="genes") +
ggplot2::ggtitle("layered")
# split
one <- list(
p + plotImage(x) + ggplot2::ggtitle("image"),
p + plotLabel(x) + ggplot2::ggtitle("labels"),
p + plotShape(x, 1) + ggplot2::ggtitle("circles"),
p + plotShape(x, 3) + ggplot2::ggtitle("polygons"),
p + plotPoint(x, c="genes") + ggplot2::ggtitle("points"))
fig <- patchwork::wrap_plots(c(list(all), one), nrow=2)

vdiffr::expect_doppelganger("overlays", fig)
zs <- system.file("extdata", "blobs.zarr", package="SpatialData")
x <- readSpatialData(zs)
p <- plotSpatialData()
# joint
all <- p +
plotImage(x) +
plotLabel(x, a=1/3) +
plotShape(x, 1) +
plotShape(x, 3) +
plotPoint(x, col="genes") +
ggplot2::ggtitle("layered")
# split
one <- list(
p + plotImage(x) + ggplot2::ggtitle("image"),
p + plotLabel(x) + ggplot2::ggtitle("labels"),
p + plotShape(x, 1) + ggplot2::ggtitle("circles"),
p + plotShape(x, 3) + ggplot2::ggtitle("polygons"),
p + plotPoint(x, col="genes") + ggplot2::ggtitle("points"))
fig <- patchwork::wrap_plots(c(list(all), one), nrow=2)
vdiffr::expect_doppelganger("overlays", fig)
})
Loading