Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GiottoVisuals 0.2.8 #89

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
enh: spatPlot3D() for non-categorical data
- working towards feature parity
  • Loading branch information
jiajic committed Nov 14, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 9f998ee5d1168cfc4dba556d22628ea7efb5c939
124 changes: 124 additions & 0 deletions R/aux_visuals.R
Original file line number Diff line number Diff line change
@@ -229,6 +229,130 @@ giotto_point <- function(
}


#' @name giotto_point_3d
#' @param pl plotly plot object
#' @param data dataframe-like object with data to plot
#' @param cell_color column in pl to plot. May be passed as NULL in which case
#' spots will default to `lightblue` in color.
#' @param color_as_factor logical. Whether values should be plotted as
#' categorical. Default is TRUE.
#' @param cell_color_code specific set of color hex codes to use in plotting
#' @param cell_color_gradient character. name of gradient to use
#' @param gradient_limits numeric of length 2. Numerical min and max values to
#' display in gradient scale.
#' @param point_size numeric. Size of points
#' @param data_other dataframe-like object with 'other' data points to plot
#' @param select_cells character. specific cell_IDs to select. Unselected will
#' be treated as 'other'
#' @param show_other_cells logical. whether to plot the 'other' cells.
#' @param other_cell_color character. Color code(s) to apply to 'other' cells.
#' @param other_point_size numeric. Size of points for 'other' cells
#' @param other_cell_alpha numeric. Alpha of 'other' cells
#' @param instrs giottoInstructions
#' @keywords internal
#' @noRd
giotto_point_3d <- function(pl,
data,
cell_color = NULL,
color_as_factor = TRUE,
cell_color_code = NULL,
cell_color_gradient = NULL,
gradient_limits = NULL,
gradient_style = "divergent",
gradient_midpoint = NULL,
point_size = 3,
point_alpha = 1,
data_other = NULL,
select_cells = NULL,
show_other_cells = TRUE,
other_cell_color = "lightgrey",
other_point_size = 0.5,
other_cell_alpha = 3,
instrs
) {
# plotly params list init & static params
# ** toplevel ** #
trace_params <- trace_params_other <- list(
type = "scatter3d",
mode = "markers",
x = ~sdimx,
y = ~sdimy,
z = ~sdimz
)
trace_params$colors <- "lightblue" # default with `cell_color` = NULL
trace_params$opacity <- point_alpha
trace_params_other$name <- "unselected cells"
trace_params_other$opacity <- other_cell_alpha

# ** point aes ** #
marker_params <- marker_params_other <- list()
marker_params$size <- point_size
marker_params_other$size <- other_point_size
marker_params_other$color <- other_cell_color

# finalize data and color
if (!is.null(cell_color)) {
if (!cell_color %in% colnames(data)) {
message(sprintf("`cell_color` '%s' does not exist!", cell_color))
return(pl) # return early
}

# finalize color scale
if (is.null(cell_color_code)) {
if (color_as_factor) { # categorical
number_colors <- length(unique(
data[[cell_color]]
))
cell_color_code <- set_default_color_discrete_cell(
instrs = instrs
)(n = number_colors)
} else { # continuous
cell_color_code <- set_default_color_continuous_cell(
instrs = instrs,
style = gradient_style,
midpoint = gradient_midpoint,
colors = cell_color_gradient
)$palette(seq(0, 1, length.out = 100L))
}
}

# finalize data
if (color_as_factor) { # categorical
data[[cell_color]] <- as.factor(data[[cell_color]])
} else { # continuous
# assign gradient limits if needed
if (!is.null(gradient_limits)) {
checkmate::assert_numeric(gradient_limits, len = 2L)
lower_lim <- gradient_limits[[1L]]
upper_lim <- gradient_limits[[2L]]
data[, (cell_color) :=
scales::oob_squish(get(cell_color), gradient_limits)]
}
}
# apply non-default color settings
trace_params$color <- data[[cell_color]]
trace_params$colors <- cell_color_code
}
# apply finalized data and marker
trace_params$data <- data
trace_params$marker <- marker_params

# other points
if (!is.null(select_cells) && isTRUE(show_other_cells)) {
trace_params$name <- "selected_cells"

trace_params_other$data <- data_other
trace_params_other$marker <- marker_params_other
pl <- do.call(
plotly::add_trace, args = c(list(p = pl), trace_params_other)
)
}

pl <- do.call(plotly::add_trace, args = c(list(p = pl), trace_params))

return(pl)
}




33 changes: 31 additions & 2 deletions R/vis_spatial_gg.R
Original file line number Diff line number Diff line change
@@ -776,6 +776,7 @@ spatPlot2D <- function(gobject,
#' @family spatial visualizations
#' @returns ggplot
#' @examples
#' # 2D Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' g <- GiottoData::loadGiottoMini("visium", verbose = FALSE)
#' spatPlot(g, show_image = TRUE, image_name = "image")
#'
@@ -792,13 +793,39 @@ spatPlot2D <- function(gobject,
#' )
#'
#'
#' # load another dataset with 3D data
#' # 3D Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#' starmap <- GiottoData::loadGiottoMini("starmap", verbose = FALSE)
#'
#' # default is to rescale plot as a 3D cube
#' spatPlot3D(starmap, cell_color = "leiden_clus")
#' # real scaling
#' spatPlot3D(g, cell_color = "leiden_clus", axis_scale = "real")
#' spatPlot3D(starmap, cell_color = "leiden_clus", axis_scale = "real")
#'
#' # plot with selected cell groups
#' spatPlot3D(starmap,
#' cell_color = "cell_types",
#' color_as_factor = TRUE,
#' select_cell_groups = c("cell F", "cell C", "cell A"),
#' other_point_size = 1
#' )
#'
#' # use the "sequential" style gradient default
#' spatPlot3D(starmap,
#' cell_color = "total_expr",
#' color_as_factor = FALSE,
#' point_alpha = 0.5,
#' axis_scale = "real",
#' gradient_style = "sequential"
#' )
#'
#' # specific color gradient
#' spatPlot3D(starmap,
#' cell_color = "total_expr",
#' color_as_factor = FALSE,
#' point_alpha = 0.7,
#' axis_scale = "cube",
#' cell_color_gradient = "mako"
#' )
#' @export
#' @seealso \code{\link{spatPlot3D}}
spatPlot <- function(...) {
@@ -2281,6 +2308,8 @@ spatDimPlot <- function(gobject, ...) {
#' @inheritParams plot_cow_params
#' @inheritParams plot_image_params
#' @inheritParams plot_params
#' @param plot_method character. How to plot the points. Either "ggplot" for
#' the default or "scattermore" for a faster rasterized option
#' @param largeImage_name deprecated
#' @param spat_loc_name name of spatial locations
#' @param sdimx x-axis dimension name (default = 'sdimx')
116 changes: 44 additions & 72 deletions R/vis_spatial_plotly.R
Original file line number Diff line number Diff line change
@@ -702,6 +702,7 @@ dimPlot3D <- function(gobject,
color_as_factor = TRUE,
cell_color = NULL,
cell_color_code = NULL,
cell_color_gradient = NULL,
show_cluster_center = FALSE,
show_center_label = TRUE,
center_point_size = 4,
@@ -1225,13 +1226,19 @@ plotPCA_3D <- function(gobject,
sdimz = NULL,
spat_enr_names = NULL,
point_size = 3,
point_alpha = 1,
cell_color = NULL,
cell_color_code = NULL,
cell_color_gradient = NULL,
color_as_factor = TRUE,
gradient_limits = NULL,
gradient_style = "divergent",
gradient_midpoint = NULL,
select_cell_groups = NULL,
select_cells = NULL,
show_other_cells = TRUE,
other_cell_color = "lightgrey",
other_point_size = 0.5,
other_point_size = 3,
show_network = FALSE,
spatial_network_name = "spatial_network",
network_color = NULL,
@@ -1316,7 +1323,7 @@ plotPCA_3D <- function(gobject,


## create subsets if needed
if (!is.null(select_cells) & !is.null(select_cell_groups)) {
if (!is.null(select_cells) && !is.null(select_cell_groups)) {
message("You have selected both individual cell IDs and a group of
cells")
group_cell_IDs <- cell_locations_metadata[get(cell_color) %in%
@@ -1361,74 +1368,27 @@ plotPCA_3D <- function(gobject,


pl <- plotly::plot_ly()
if (!is.null(cell_color)) {
if (cell_color %in% colnames(cell_locations_metadata_selected)) {
if (is.null(cell_color_code)) {
number_colors <- length(unique(
cell_locations_metadata_selected[[cell_color]]
))
cell_color_code <- set_default_color_discrete_cell(
instrs = instructions(gobject)
)(n = number_colors)
}
cell_locations_metadata_selected[[cell_color]] <- as.factor(
cell_locations_metadata_selected[[cell_color]]
)
pl <- pl %>% plotly::add_trace(
type = "scatter3d", mode = "markers",
data = cell_locations_metadata_selected,
x = ~sdimx, y = ~sdimy, z = ~sdimz,
color = cell_locations_metadata_selected[[cell_color]],
colors = cell_color_code,
marker = list(size = point_size)
)


if (!is.null(select_cells) & show_other_cells) {
pl <- pl %>% plotly::add_trace(
type = "scatter3d", mode = "markers",
data = cell_locations_metadata_other,
name = "unselected cells",
x = ~sdimx,
y = ~sdimy,
z = ~sdimz,
marker = list(
size = other_point_size,
color = other_cell_color
),
opacity = other_cell_alpha
)
}
} else {
message("cell_color does not exist!")
}
} else {
pl <- pl %>% plotly::add_trace(
type = "scatter3d",
data = cell_locations_metadata_selected,
x = ~sdimx,
y = ~sdimy,
z = ~sdimz,
mode = "markers",
marker = list(size = point_size),
colors = "lightblue", name = "selected cells"
)

if (!is.null(select_cells) & show_other_cells) {
pl <- pl %>% plotly::add_trace(
type = "scatter3d",
mode = "markers",
data = cell_locations_metadata_other,
name = "unselected cells",
x = ~sdimx, y = ~sdimy, z = ~sdimz,
marker = list(
size = other_point_size,
color = other_cell_color
),
opacity = other_cell_alpha
)
}
}
## plot points
pl <- giotto_point_3d(pl,
data = cell_locations_metadata_selected,
cell_color = cell_color,
color_as_factor = color_as_factor,
cell_color_code = cell_color_code,
cell_color_gradient = cell_color_gradient,
gradient_limits = gradient_limits,
gradient_style = gradient_style,
gradient_midpoint = gradient_midpoint,
point_size = point_size,
point_alpha = point_alpha,
data_other = cell_locations_metadata_other,
select_cells = select_cells,
show_other_cells = show_other_cells,
other_cell_color = other_cell_color,
other_point_size = other_point_size,
other_cell_alpha = other_cell_alpha,
instrs = instructions(gobject)
)


## plot spatial network
@@ -1499,13 +1459,19 @@ spatPlot3D <- function(gobject,
sdimz = "sdimz",
spat_enr_names = NULL,
point_size = 3,
point_alpha = 1,
cell_color = NULL,
cell_color_code = NULL,
cell_color_gradient = NULL,
color_as_factor = TRUE,
gradient_limits = NULL,
gradient_style = "divergent",
gradient_midpoint = NULL,
select_cell_groups = NULL,
select_cells = NULL,
show_other_cells = TRUE,
other_cell_color = "lightgrey",
other_point_size = 0.5,
other_point_size = 3,
other_cell_alpha = 0.5,
show_network = FALSE,
spatial_network_name = "Delaunay_network",
@@ -1528,7 +1494,7 @@ spatPlot3D <- function(gobject,
save_param = list(),
default_save_name = "spat3D") {
if (is.null(sdimz)) {
message("create 2D plot")
vmsg(.is_debug = TRUE, "create 2D plot")

pl <- .spatPlot_2d_plotly(
gobject = gobject,
@@ -1561,7 +1527,7 @@ spatPlot3D <- function(gobject,
show_plot = FALSE
)
} else {
message("create 3D plot")
vmsg(.is_debug = TRUE, "create 3D plot")
pl <- .spatPlot_3d_plotly(
gobject = gobject,
spat_unit = spat_unit,
@@ -1570,8 +1536,14 @@ spatPlot3D <- function(gobject,
sdimy = sdimy,
sdimz = sdimz,
point_size = point_size,
point_alpha = point_alpha,
cell_color = cell_color,
color_as_factor = color_as_factor,
cell_color_code = cell_color_code,
cell_color_gradient = cell_color_gradient,
gradient_limits = gradient_limits,
gradient_style = gradient_style,
gradient_midpoint = gradient_midpoint,
select_cell_groups = select_cell_groups,
select_cells = select_cells,
show_other_cells = show_other_cells,