-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplotFrame.R
More file actions
102 lines (101 loc) · 3.13 KB
/
Copy pathplotFrame.R
File metadata and controls
102 lines (101 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#' @name plotFrame
#' @aliases plotShape plotPoint
#' @title \code{SpatialData} point/shape viz.
#'
#' @param x \code{SpatialData} object.
#' @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}).
#'
#' @examples
#' x <- file.path("extdata", "blobs.zarr")
#' x <- system.file(x, package="SpatialData")
#' x <- readSpatialData(x)
#'
#' # shapes
#' p <- plotSpatialData()
#' a <- p + plotShape(x, "blobs_polygons")
#' b <- p + plotShape(x, "blobs_multipolygons")
#' c <- p + plotShape(x, "blobs_circles")
#' patchwork::wrap_plots(a, b, c)
#'
#' # layered
#' p +
#' plotShape(x, "blobs_circles", fill="pink") +
#' plotShape(x, "blobs_polygons", colour="red")
#' patchwork::wrap_plots(a, b)
#'
#' # points
#' i <- "blobs_points"
#' p <- plotSpatialData()
#' p + plotPoint(x, i) # simple
#' p + plotPoint(x, i, colour="genes") # discrete
#' p + plotPoint(x, i, colour="instance_id") # continuous
NULL
#' @importFrom sf st_as_sf st_coordinates st_geometry_type st_buffer
#' @importFrom ggplot2 aes theme scale_type geom_sf coord_sf
#' @importFrom SpatialData transform
#' @importFrom ggforce geom_circle
#' @importFrom utils tail
.plot <- \(x, y, key=NULL, n=Inf, assay=1, i=1, ...) {
if (is(y, "PointFrame")) {
if (!is.null(key)) {
fk <- feature_key(y)
y@data <- dplyr::filter(data(y), .data[[fk]] %in% key)
}
}
if (is.finite(n)) {
n <- min(length(y), n)
y <- y[sample(length(y), n)]
if (is(y, "ShapeFrame")) {
shape(x, i) <- y
} else {
point(x, i) <- y
}
}
df <- st_as_sf(data(y))
aes <- aes()
dot <- list(...)
for (arg in names(dot)) {
val <- dot[[arg]]
if (is.character(val)) {
z <- tryCatch(
error=\(e) NULL,
getTable(x, i, val, assay=assay))
if (!is.null(z)) {
fd <- data.frame(z)
names(fd) <- val
df <- cbind(df, fd)
}
if (val %in% names(df)) {
if (scale_type(df[[arg]]) == "discrete")
df[[val]] <- factor(df[[arg]])
aes[[arg]] <- aes(.data[[val]])[[1]]
dot[[arg]] <- NULL
}
}
}
if ("radius" %in% names(df))
df <- st_buffer(df, df$radius)
list(
do.call(geom_sf, c(list(data=df, mapping=aes), c(dot))),
theme(legend.key.size=unit(0.5, "lines")),
coord_sf(expand=FALSE))
}
#' @export
#' @rdname plotFrame
setMethod("plotShape", "SpatialData", \(x, i=1, j=1, assay=1, ...) {
if (is.numeric(i)) i <- shapeNames(x)[i]
y <- shape(x, i)
y <- SpatialData::transform(y, j)
.plot(x, y, assay=assay, i=i, ...)
})
#' @export
#' @rdname plotFrame
setMethod("plotPoint", "SpatialData", \(x, i=1, j=1, ...) {
if (is.numeric(i)) i <- pointNames(x)[i]
y <- point(x, i)
y <- SpatialData::transform(y, j)
.plot(x, y, i=i, ...)
})