diff --git a/DESCRIPTION b/DESCRIPTION index f0f9198..69ea85a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: GiottoVisuals Title: Visuals for the Giotto spatial biology analysis ecosystem -Version: 0.1.7 +Version: 0.2.0 Authors@R: c( person("Ruben", "Dries", email = "rubendries@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-7650-7754")), @@ -33,7 +33,7 @@ Imports: data.table, ggplot2 (>= 3.1.1), GiottoUtils (>= 0.1.0.1), - GiottoClass (>= 0.1.0.1), + GiottoClass (>= 0.3.0), ggrepel, igraph (>= 1.2.4.1), methods, @@ -83,6 +83,7 @@ Collate: 'generics.R' 'gg_annotation_raster.R' 'gg_info_layers.R' + 'gg_settings.R' 'globals.R' 'gstop.R' 'mixcolor.R' diff --git a/NAMESPACE b/NAMESPACE index 77c6784..13d0472 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(create_cluster_dendrogram) export(dimCellPlot) export(dimCellPlot2D) export(dimFeatPlot2D) +export(dimFeatPlot3D) export(dimGenePlot3D) export(dimPlot) export(dimPlot2D) @@ -67,12 +68,14 @@ export(spatDeconvPlot) export(spatDimCellPlot) export(spatDimCellPlot2D) export(spatDimFeatPlot2D) +export(spatDimFeatPlot3D) export(spatDimGenePlot3D) export(spatDimPlot) export(spatDimPlot2D) export(spatDimPlot3D) export(spatFeatPlot2D) export(spatFeatPlot2D_single) +export(spatFeatPlot3D) export(spatGenePlot3D) export(spatInSituPlotDensity) export(spatInSituPlotHex) diff --git a/NEWS.md b/NEWS.md index 707fe05..c08120b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,20 @@ +# GiottoVisuals 0.2.0 (2024/05/01) + +## breaking changes + +## changes +- _GiottoClass >= 0.3.0_ is now required. (object no longer has `@largeImages` slot) +- `largeImage_name` arg is deprecated. `image_name` arg should be used instead + +## bug fixes +- removed centroid point that unintentionally gets plotted in polygons + +## enhancements +- `theme_param` arg has been added to some plotting functions that provides access to `ggplot2::theme()` finetuning +- `gg_annotation_raster()` now performs plot extent detection and resampling of images +- resampling args are now globally settable (see `?auto_image_resample`) + + # GiottoVisuals 0.1.7 (2024/03/11) ## Changes @@ -9,7 +26,7 @@ ## bug fixes - fix `spatPlot2D()` and `spatFeatPlot2D()` largeImage plotting when using `group_by` -- fix `edge_alpha` param in `spatFeatPlot2D()` +- fix `edge_alpha` arg in `spatFeatPlot2D()` ## new - `mixRGB()` vectorized additive mixing in RGB space @@ -19,7 +36,7 @@ # GiottoVisuals 0.1.4 (2024/01/25) ## bug fixes -- fix plotting color gradient when using param `point_shape = "no_border"` +- fix plotting color gradient when using arg `point_shape = "no_border"` - fix image NA value [#865](https://github.com/drieslab/Giotto/issues/865) by rbutleriii ## new diff --git a/R/dd.R b/R/dd.R index 69eca0d..0f90fe2 100644 --- a/R/dd.R +++ b/R/dd.R @@ -64,7 +64,7 @@ NULL #' @param show_polygon overlay polygon information (e.g. cell shape) #' @param use_overlap use polygon and feature coordinates overlap results #' @param polygon_feat_type feature type associated with polygon information -#' @param polygon_color color for polygon border +#' @param polygon_color color for polygon border. Set `NA` to remove border #' @param polygon_bg_color color for polygon background #' (overruled by polygon_fill) #' @param polygon_fill character. what to color to fill polgyons by diff --git a/R/gg_annotation_raster.R b/R/gg_annotation_raster.R index f796442..7be19ec 100644 --- a/R/gg_annotation_raster.R +++ b/R/gg_annotation_raster.R @@ -3,6 +3,11 @@ #' @title Append image to ggplot as annotation_raster #' @param ggobj ggplot2 `gg` object #' @param gimage `giottoLargeImage`, `giottoImage` or `list` thereof +#' @param \dots additional params to pass +#' @details +#' No ... params are implemented for `giottoImage`. \cr ... params for +#' `giottoLargeImage` passes to automated resampling params see +#' `?auto_image_resample` for details #' @return `gg` object with images to plot appended as annotation rasters NULL @@ -51,10 +56,22 @@ setMethod( ) #' @rdname gg_annotation_raster +#' @param ext Object that responds to `ext()`. Defines the plot spatial ROI +#' that the image should be sampled for. setMethod( "gg_annotation_raster", signature(ggobj = "gg", gimage = "giottoLargeImage"), - function(ggobj, gimage, ...) { + function(ggobj, gimage, ext = NULL, ...) { + + # apply plot ext + if (!is.null(ext)) { + gimage <- .auto_resample_gimage( + img = gimage, + plot_ext = ext, + ... + ) + } + # get plotting minmax extent <- terra::ext(gimage@raster_object)[seq_len(4)] xmin <- extent[["xmin"]] @@ -110,3 +127,197 @@ setMethod( return(ggobj) } ) + + + + + + +# Internals #### + +# returns the spatial extent needed for the plot +# ... passes to ext() `giotto` method +.guess_plot_extent <- function( + gobject, spat_unit = NULL, spat_loc_name = NULL, ext = NULL, ... + ) { + + if (!is.null(ext)) ext <- ext(ext) # normalize to `SpatExtent` class + # if ext already given, directly return + if (inherits(ext, "SpatExtent")) return(ext) + + # find extent from one of poly, spatlocs, points, in that order of pref + e <- ext( + gobject, + spat_unit = spat_unit, + verbose = FALSE, + name = spat_loc_name, + # `name` only passes to `getSpatialLocations()` if spatlocs are + # present and used to find extent + ... # You can ensure they are used by setting prefer = "spatlocs + ) + + if (is.null(e)) { + stop(wrap_txt( + "No `ext` provided and no spatial locations or polygons discovered. + Cannot determine largeImage resample extent" + )) + } + return(e) +} + + +#' @name auto_image_resample +#' @title Optimized image resampling +#' @description +#' Downsample terra-based images for plotting. Uses +#' \code{\link[terra]{spatSample}} to load onlya portion of the original image, +#' speeding up plotting and lowering memory footprint. +#' +#' Default behavior of `spatSample` is to crop if only a smaller ROI is +#' needed for plotting followed by the sampling process in order to reduce +#' wasted sampling by focusing the sample space. For very large ROIs, this +#' crop can be time intensive and require writing to disk. +#' +#' This function examines the ROI dimensions as defined through the limits of +#' the spatial locations to be plotted, and decides between the following two +#' methods in order to avoid this issue: +#' \itemize{ +#' \item{\strong{Method A.} First crop original image, then sample +#' `max_sample` (default = 5e5) values to generate final image. Intended +#' for smaller ROIs. Force usage of this method by setting +#' `flex_resample = FALSE`} +#' \item{\strong{Method B.} First oversample, then crop. Intended for larger +#' ROIs. Base sample size is `max_sample`, which is then multiplied by a +#' scale factor >1 that increases the smaller the ROI is and is defined by: +#' original dimensions/crop dimensions where the larger ratio between x +#' and y dims is chosen. Scale factor is capped by +#' \code{max_resample_scale}} +#' } +#' Control points for this function are set by \code{max_crop} which decides +#' the max ROI area after which switchover to method B happens in order to +#' avoid laborious crops and \code{max_resample_scale} which determines the +#' maximum scale factor for number of values to sample. Both values can be +#' adjusted depending on system resources. Additionally, \code{flex_resample} +#' determines if this switching behavior happens. +#' When set to \code{FALSE}, only method A is used. +#' @param img giotto image to plot +#' @param plot_ext extent of plot (required) +#' @param img_border if not 0 or FALSE, expand plot_ext by this percentage on +#' each side before applying crop on image. See details +#' @param flex_resample logical. Default = TRUE. Forces usage of method A when +#' FALSE. +#' @param max_sample numeric. Default = 5e5. Maximum n values to sample from the +#' image. If larger than `max_crop`, will override `max_crop.` +#' Globally settable with the option "giotto.plot_img_max_sample" +#' @param max_crop numeric. Default = 1e8. Maximum crop size (px area) allowed +#' for \strong{method A} before switching to \strong{method B} +#' (see description). +#' Globally settable with option "giotto.plot_img_max_crop" +#' @param max_resample_scale numeric. Default = 100. Maximum scalefactor allowed +#' to be applied on `max_sample` in order to oversample when compensating +#' for decreased resolution when cropping after sampling. Globally settable with +#' option "giotto.plot_img_max_resample_scale". +#' @details +#' **img_border** +#' expand ext to use for plotting the image. This makes it so that the image +#' is not cut off sharply at the edge of the plot extent. Needed since plots +#' often define extent by centroids, and polygons may hang over the edge of the +#' extent. +#' @returns a giotto image cropped and resampled properly for plotting +#' @examples +#' \dontrun{ +#' img <- GiottoData::loadSubObjectMini("giottoLargeImage") +#' .auto_resample_gimage(img) +#' } +#' @seealso \code{\link[terra]{spatSample}} +#' @keywords internal +.auto_resample_gimage <- function( + img, + plot_ext, + img_border = 0.125, + flex_resample = TRUE, + max_sample = getOption("giotto.plot_img_max_sample", 5e5), + max_crop = getOption("giotto.plot_img_max_crop", 1e8), + max_resample_scale = getOption( + "giotto.plot_img_max_resample_scale", 100 + ) +) { + + img_ext <- terra::ext(img) + bound_poly <- as.polygons(img_ext) + crop_ext <- img_ext # default + + # override max_crop if needed + if (max_sample > max_crop) max_crop <- max_sample + + # apply img border + # - cropping with extent larger than the image extent works + if (img_border > 0) { + + crop_ext <- bound_poly %>% + rescale(1 + img_border) %>% + ext() + + # determine final crop (normalizes extent when larger than available) + crop_ext <- ext(crop(bound_poly, crop_ext)) + } + + # determine ratio of crop vs original + original_dims <- dim(img)[c(2L, 1L)] # x, y ordering + ratios <- range(crop_ext) / range(img_ext) # x, y ordering + crop_dims <- original_dims * ratios + crop_area_px <- prod(crop_dims) + + if (!isTRUE(flex_resample) || crop_area_px <= max_crop) { + # [METHOD A]: + # 1. Crop if needed + # 2. resample to final image + if (!isTRUE(flex_resample) && crop_area_px > max_crop) { + warning("Plotting large regions with flex_resample == FALSE will + increase time and may require scratch space.") + } + + vmsg(.is_debug = TRUE, + sprintf("img auto_res: [A] | area: %d | max: %d", + crop_area_px, max_crop)) + + crop_img <- terra::crop( + x = img@raster_object, + y = crop_ext + ) + img@raster_object <- terra::spatSample( + crop_img, + size = max_sample, + method = "regular", + as.raster = TRUE + ) + } else { + # [METHOD B]: + # 1. Oversample + # 2. crop to final image + # Sample n values where max_sample is scaled by a value >1 + # Scale factor is fullsize image dim/crop dim. Larger of the two + # ratios is chosen + scalef <- max(1/ratios) + # This scaling is ALSO capped by max_resample_scale + if (scalef > max_resample_scale) scalef <- max_resample_scale + + vmsg(.is_debug = TRUE, + sprintf("img auto_res: [B] | scalef: %d | max_scale: %d", + scalef, max_resample_scale)) + + oversample_img <- terra::spatSample( + img@raster_object, + size = round(max_sample * scalef), + method = "regular", + as.raster = TRUE + ) + img@raster_object <- terra::crop( + x = oversample_img, + y = crop_ext + ) + } + return(img) +} + + diff --git a/R/gg_info_layers.R b/R/gg_info_layers.R index 2eca7f6..6b67b46 100644 --- a/R/gg_info_layers.R +++ b/R/gg_info_layers.R @@ -1036,8 +1036,6 @@ plot_cell_polygon_layer <- function( instrs = NULL, polygon_dt, polygon_grouping = "poly_ID", - sdimx = "x", - sdimy = "y", fill = NULL, poly_fill_gradient = NULL, fill_gradient_midpoint = NULL, @@ -1381,8 +1379,7 @@ plot_feature_hexbin_layer <- function( #' @param spat_loc_name name for spatial locations #' @param polygon_feat_type name for feature type associated with #' polygon information -#' @param sdimx x-axis dimension name (default = 'sdimx') -#' @param sdimy y-axis dimension name (default = 'sdimy') +#' @param \dots additional args passed to `giotto` `.guess_plot_extent` #' @return ggplot #' @keywords internal #' @noRd @@ -1390,87 +1387,55 @@ plot_spat_image_layer_ggplot <- function( gg_obj, gobject, gimage, - feat_type = NULL, spat_unit = NULL, + feat_type = NULL, spat_loc_name = NULL, polygon_feat_type = NULL, - sdimx = NULL, - sdimy = NULL) { - if (is.null(gobject) | is.null(gimage)) { + ...) { + if (is.null(gobject) || is.null(gimage)) { stop("A giotto object and a giotto image need to be provided") } - if (is.null(sdimx) | is.null(sdimy)) { - warning("plot_method = ggplot, but spatial dimensions for sdimx - and/or sdimy are not specified. \n - It will default to the 'sdimx' and 'sdimy' ") - sdimx <- "sdimx" - sdimy <- "sdimy" - } + # NSE vars + sdimx <- sdimy <- NULL - # Set feat_type and spat_unit - spat_unit <- set_default_spat_unit( - gobject = gobject, - spat_unit = spat_unit - ) - feat_type <- set_default_feat_type( - gobject = gobject, - spat_unit = spat_unit, - feat_type = feat_type + # prefer extent detection from polygon + spat_unit <- ifelse( + !is.null(polygon_feat_type), + yes = polygon_feat_type, + no = spat_unit ) - # spatial locations - spatlocs <- get_spatial_locations( + # allows passing of pre-defined `ext` through ... + e <- .guess_plot_extent( gobject = gobject, spat_unit = spat_unit, + feat_type = feat_type, # only used in detection from points spat_loc_name = spat_loc_name, - output = "data.table", - copy_obj = TRUE, - verbose = FALSE + prefer = c("polygon", "spatlocs", "points"), + ... ) - # Get spatial extent for positioning purposes - spat_ext <- spatlocs[, c("sdimx", "sdimy"), with = FALSE] - - # When spatial locations are missing but subcellular info is present - # Pull plot extent info from polygon info if present - - if (is.null(spat_ext)) { - gpoly <- get_polygon_info( - gobject = gobject, - polygon_name = polygon_feat_type, - return_giottoPolygon = FALSE - ) - - poly_ext <- terra::ext(gpoly)[seq_len(4)] - spat_ext <- data.table::data.table( - sdimx = c(poly_ext[["xmin"]], poly_ext[["xmax"]]), - sdimy = c(poly_ext[["ymin"]], poly_ext[["ymax"]]) - ) - } - - # If still missing, send warning - if (is.null(spat_ext)) { - warning("No spatial locations or polygon info found.\n - Plot spatial extent may be incorrect\n") - } + bounds_dt <- data.table::data.table( + sdimx = e[][c(1, 2)], + sdimy = e[][c(3, 4)] + ) # Assign region to plot - gg_obj <- gg_obj + geom_blank(data = spat_ext, aes_string(sdimx, sdimy)) + gg_obj <- gg_obj + geom_blank(data = bounds_dt, aes(sdimx, sdimy)) # Assign image(s) to plot - gg_obj <- gg_annotation_raster(ggobj = gg_obj, gimage = gimage) - - - if (!is.null(spatlocs)) { - gg_obj <- gg_obj + - geom_point( - data = spatlocs, - aes_string(sdimx, sdimy), - alpha = 0.5, - size = 0.4 - ) - } + gg_obj <- gg_annotation_raster(ggobj = gg_obj, gimage = gimage, ext = e) + + # if (!is.null(spatlocs)) { + # gg_obj <- gg_obj + + # geom_point( + # data = spatlocs, + # aes_string(sdimx, sdimy), + # alpha = 0.5, + # size = 0.4 + # ) + # } return(gg_obj) } diff --git a/R/gg_settings.R b/R/gg_settings.R new file mode 100644 index 0000000..f51d978 --- /dev/null +++ b/R/gg_settings.R @@ -0,0 +1,34 @@ + + +# wrapper for ggplot2 with some adapters for Giotto settings +# ggplot2-native arg inputs are preferred +# +.gg_theme <- function( + legend_text = 8, + axis_title = 8, + axis_text = 8, + background_color = "white", + ... +) { + a <- list(...) + + # giotto masked args + a$legend.text <- a$legend.text %null% element_text(size = legend_text) + a$axis.title <- a$axis.title %null% element_text(size = axis_title) + a$axis.text <- a$axis.text %null% element_text(size = axis_text) + a$panel.background <- a$panel.background %null% + element_rect(fill = background_color) + + # defaults + a$plot.title <- a$plot.title %null% element_text(hjust = 0.5) + a$axis.text.y <- a$axis.text.y %null% element_text(angle = 90, hjust = 0.5) + a$legend.title <- a$legend.title %null% element_blank() + a$panel.grid <- a$panel.grid %null% element_blank() + + do.call(ggplot2::theme, args = a) +} + + + + + diff --git a/R/plot_scatter.R b/R/plot_scatter.R index 103ac74..158ffc8 100644 --- a/R/plot_scatter.R +++ b/R/plot_scatter.R @@ -14,7 +14,7 @@ #' @examples #' x <- data.frame(x = rnorm(10), y = rnorm(10)) #' gg_simple_scatter(data = x, x = "x", y = "y") -#' +#' #' @export gg_simple_scatter <- function(ggobject = NULL, data, x, y, xlab = "x", ylab = "y", main = NULL, ...) { diff --git a/R/vis_spatial.R b/R/vis_spatial.R index 6672e7e..5b1a55c 100644 --- a/R/vis_spatial.R +++ b/R/vis_spatial.R @@ -20,8 +20,7 @@ #' @inheritParams plot_spatenr_params #' @param show_image show a tissue background image #' @param gimage a giotto image -#' @param image_name name of a giotto image -#' @param largeImage_name name of a giottoLargeImage +#' @param image_name name of giotto image(s) to plot #' @param spat_loc_name name of spatial locations #' @param sdimx x-axis dimension name (default = 'sdimx') #' @param sdimy y-axis dimension name (default = 'sdimy') @@ -37,6 +36,7 @@ #' @param vor_border_color border colorr for voronoi plot #' @param vor_max_radius maximum radius for voronoi 'cells' #' @param vor_alpha transparency of voronoi 'cells' +#' @param theme_param list of additional params passed to `ggplot2::theme()` #' @param verbose be verbose #' @return ggplot #' @details Description of parameters. @@ -49,7 +49,6 @@ show_image = FALSE, gimage = NULL, image_name = NULL, - largeImage_name = NULL, spat_loc_name = NULL, sdimx = "sdimx", sdimy = "sdimy", @@ -97,6 +96,8 @@ vor_alpha = 1, axis_text = 8, axis_title = 8, + theme_param = list(), + show_plot = NULL, return_plot = NULL, save_plot = NULL, @@ -110,9 +111,6 @@ point_shape, choices = c("border", "no_border", "voronoi") ) - if (!is.null(image_name) && !is.null(largeImage_name)) { - stop("Only one type of image can be used at a time") - } # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( @@ -125,57 +123,12 @@ feat_type = feat_type ) - ## giotto image ## - if (isTRUE(show_image)) { - if (!is.null(gimage)) { - gimage <- gimage - } else if (!is.null(image_name)) { - # If there is input to image_name arg - - if (length(image_name) == 1) { - gimage <- gobject@images[[image_name]] - if (is.null(gimage)) - warning("image_name: ", image_name, " does not exist \n") - } else { - gimage <- list() - for (gim in seq_len(length(image_name))) { - gimage[[gim]] <- gobject@images[[gim]] - if (is.null(gimage[[gim]])) - warning("image_name: ", gim, " does not exists \n") - } - } - } else if (!is.null(largeImage_name)) { - # If there is input to largeImage_name arg - if (length(largeImage_name) == 1) { - gimage <- plot_auto_largeImage_resample( - gobject = gobject, - largeImage_name = largeImage_name, - spat_unit = spat_unit, - spat_loc_name = spat_loc_name, - include_image_in_border = TRUE - ) - } else { - gimage <- list() - for (gim in seq_along(largeImage_name)) { - gimage[[gim]] <- plot_auto_largeImage_resample( - gobject = gobject, - largeImage_name = largeImage_name[[gim]], - spat_unit = spat_unit, - spat_loc_name = spat_loc_name, - include_image_in_border = TRUE - ) - } - } - } else { - # Default to first image available in images if no input given - # to image_name or largeImage_name args - image_name <- names(gobject@images)[1] - gimage <- gobject@images[[image_name]] - if (is.null(gimage)) { - warning(sprintf("image_name: \'%s\' does not exist", - image_name)) - } - } + ## get giotto image(s) ## + if (isTRUE(show_image) && is.null(gimage)) { + gimage <- getGiottoImage( + gobject = gobject, + name = image_name + ) } @@ -259,11 +212,11 @@ message("You have selected both individual cell IDs and a group of cells") group_cell_IDs <- cell_locations_metadata[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] select_cells <- unique(c(select_cells, group_cell_IDs)) } else if (!is.null(select_cell_groups)) { select_cells <- cell_locations_metadata[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] } if (!is.null(select_cells)) { @@ -274,8 +227,8 @@ cell_locations_metadata[cell_locations_metadata$cell_ID %in% select_cells] spatial_network <- spatial_network[spatial_network$to %in% - select_cells & spatial_network$from %in% - select_cells] + select_cells & spatial_network$from %in% + select_cells] # if specific cells are selected # cell_locations_metadata = cell_locations_metadata_selected @@ -289,7 +242,7 @@ # only keep names from selected groups if (!is.null(select_cell_groups) & !is.null(cell_color_code)) { cell_color_code <- cell_color_code[names(cell_color_code) %in% - select_cell_groups] + select_cell_groups] } # data.table and ggplot variables @@ -316,12 +269,10 @@ pl <- plot_spat_image_layer_ggplot( gg_obj = pl, gobject = gobject, - feat_type = feat_type, spat_unit = spat_unit, + feat_type = feat_type, spat_loc_name = spat_loc_name, - gimage = gimage, - sdimx = sdimx, - sdimy = sdimy + gimage = gimage ) } @@ -406,38 +357,36 @@ ) pl <- switch(point_shape, - "border" = do.call( - plot_spat_point_layer_ggplot, - args = c( - point_general_params, - point_border_specific_params - ) - ), - "no_border" = do.call( - plot_spat_point_layer_ggplot_noFILL, - args = point_general_params - ), - "voronoi" = do.call( - plot_spat_voronoi_layer_ggplot, - args = c( - point_general_params, - point_voronoi_specific_params - ) - ) + "border" = do.call( + plot_spat_point_layer_ggplot, + args = c( + point_general_params, + point_border_specific_params + ) + ), + "no_border" = do.call( + plot_spat_point_layer_ggplot_noFILL, + args = point_general_params + ), + "voronoi" = do.call( + plot_spat_voronoi_layer_ggplot, + args = c( + point_general_params, + point_voronoi_specific_params + ) + ) ) - ## adjust theme settings - pl <- pl + ggplot2::theme( - plot.title = element_text(hjust = 0.5), - legend.title = element_blank(), - legend.text = element_text(size = legend_text), - axis.title = element_text(size = axis_title), - axis.text = element_text(size = axis_text), - panel.grid = element_blank(), - panel.background = element_rect(fill = background_color) + gg_theme_args <- c( + theme_param, + legend_text = legend_text, + axis_title = axis_title, + axis_text = axis_text, + background_color = background_color ) + pl <- pl + do.call(.gg_theme, args = gg_theme_args) ## change symbol size of legend if (isTRUE(color_as_factor)) { @@ -461,7 +410,7 @@ # provide x, y and plot titles if (is.null(title)) title <- cell_color pl <- pl + ggplot2::labs(x = "x coordinates", y = "y coordinates", - title = title) + title = title) return(plot_output_handler( gobject = gobject, @@ -529,6 +478,7 @@ #' @param vor_border_color border color for voronoi plot #' @param vor_max_radius maximum radius for voronoi 'cells' #' @param vor_alpha transparency of voronoi 'cells' +#' @param theme_param list of additional params passed to `ggplot2::theme()` #' @details coord_fix_ratio: set to NULL to use default ggplot parameters #' @returns ggplot #' @examples @@ -600,18 +550,30 @@ spatPlot2D <- function( return_plot = NULL, save_plot = NULL, save_param = list(), + theme_param = list(), default_save_name = "spatPlot2D") { checkmate::assert_class(gobject, "giotto") + # deprecation message + if (!is.null(largeImage_name)) { + deprecate_warn( + when = "0.2.0", + what = "spatPlot2D(largeImage_name)", + details = c( + "Use `image_name` argument instead for all images to plot." + ) + ) + image_name <- c(image_name, largeImage_name) + } + # create args list needed for each call to .spatPlot2D_single() # 1. - grab all params available # 2. - subset to those needed - spp_params <- get_args_list() - spp_params <- spp_params[c( + spp_params <- get_args_list(keep = c( # [gobject params] "gobject", "spat_unit", "feat_type", # [image params] - "show_image", "gimage", "image_name", "largeImage_name", + "show_image", "gimage", "image_name", # [spatlocs params] "spat_loc_name", "sdimx", "sdimy", # [access spatial enrichments] @@ -640,12 +602,15 @@ spatPlot2D <- function( # [grid aes] "show_grid", "spatial_grid_name", "grid_color", # [figure params] - "coord_fix_ratio", "show_legend", "legend_text", "legend_symbol_size", - "background_color", "axis_text", "axis_title", "title", + "coord_fix_ratio", "show_legend", "legend_text", + "legend_symbol_size", "background_color", "axis_text", + "axis_title", "title", # [return params] "show_plot", "return_plot", "save_plot", "save_param", - "default_save_name" - )] + "default_save_name", + # [gg params] + "theme_param" + )) ## check group_by @@ -721,9 +686,6 @@ spatPlot2D <- function( ## plotting ## savelist <- list() - # group_by images - img_type <- ifelse(is.null(image_name), "largeImage", "image") - # group_by static settings # # update these params spp_params$spat_unit <- spat_unit @@ -741,7 +703,7 @@ spatPlot2D <- function( group <- unique_groups[group_id] subset_cell_IDs <- comb_metadata[get(group_by) == group - ][["cell_ID"]] + ][["cell_ID"]] spp_params$gobject <- subsetGiotto( gobject = gobject, spat_unit = spat_unit, @@ -751,19 +713,11 @@ spatPlot2D <- function( ) # use a different image per group if there are the same - # number of names - # provided as there are groups + # number of names provided as there are groups # Otherwise, use the same image (or NULL) for all groups (default) - switch(img_type, - "image" = if (length(unique_groups) == length(image_name)) { - spp_params$image_name <- image_name[group_id] - }, - "largeImage" = if ( - length(unique_groups) == length(largeImage_name)) { - spp_params$largeImage_name <- largeImage_name[group_id] - } - ) - + if (length(unique_groups) == length(image_name)) { + spp_params$image_name <- image_name[group_id] + } pl <- do.call(.spatPlot2D_single, args = spp_params) @@ -849,38 +803,51 @@ spatPlot <- function(...) { #' @param title title for plot (default = deconv_name) #' @param axis_text size of axis text #' @param axis_title size of axis title -#' @details Description of parameters. +#' @param theme_param list of additional params passed to `ggplot2::theme()` #' @returns ggplot #' @export spatDeconvPlot <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - deconv_name = "DWLS", - show_image = FALSE, - gimage = NULL, - image_name = NULL, - largeImage_name = NULL, - spat_loc_name = NULL, - sdimx = "sdimx", - sdimy = "sdimy", - cell_color_code = NULL, - line_color = NA, - radius = 10, - alpha = 1, - legend_text = 8, - background_color = "white", - title = NULL, - axis_text = 8, - axis_title = 8, - coord_fix_ratio = 1, - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "spatDeconvPlot") { + spat_unit = NULL, + feat_type = NULL, + deconv_name = "DWLS", + show_image = FALSE, + gimage = NULL, + image_name = NULL, + largeImage_name = NULL, + spat_loc_name = NULL, + sdimx = "sdimx", + sdimy = "sdimy", + cell_color_code = NULL, + line_color = NA, + radius = 10, + alpha = 1, + legend_text = 8, + background_color = "white", + title = NULL, + axis_text = 8, + axis_title = 8, + coord_fix_ratio = 1, + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + theme_param = list(), + default_save_name = "spatDeconvPlot") { # check for installed packages package_check(pkg_name = "scatterpie", repository = "CRAN") + # deprecation message + if (!is.null(largeImage_name)) { + deprecate_warn( + when = "0.2.0", + what = "spatDeconvPlot(largeImage_name)", + details = c( + "Use `image_name` argument instead for all images to plot." + ) + ) + image_name <- c(image_name, largeImage_name) + } + # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -892,56 +859,12 @@ spatDeconvPlot <- function(gobject, feat_type = feat_type ) - ## giotto image ## - if (show_image == TRUE) { - if (!is.null(gimage)) { - gimage <- gimage - } else if (!is.null(image_name)) { - # If there is input to image_name arg - - if (length(image_name) == 1) { - gimage <- gobject@images[[image_name]] - if (is.null(gimage)) - warning("image_name: ", image_name, " does not exist \n") - } else { - gimage <- list() - for (gim in seq_len(length(image_name))) { - gimage[[gim]] <- gobject@images[[gim]] - if (is.null(gimage[[gim]])) - warning("image_name: ", gim, " does not exists \n") - } - } - } else if (!is.null(largeImage_name)) { - # If there is input to largeImage_name arg - - if (length(largeImage_name) == 1) { - gimage <- plot_auto_largeImage_resample( - gobject = gobject, - largeImage_name = largeImage_name, - spat_unit = spat_unit, - spat_loc_name = spat_loc_name, - include_image_in_border = TRUE - ) - } else { - gimage <- list() - for (gim in seq_len(length(largeImage_name))) { - gimage[[gim]] <- plot_auto_largeImage_resample( - gobject = gobject, - largeImage_name = largeImage_name[[gim]], - spat_unit = spat_unit, - spat_loc_name = spat_loc_name, - include_image_in_border = TRUE - ) - } - } - } else { - # Default to first image available in images if no input given - # to image_name or largeImage_name args - image_name <- names(gobject@images)[1] - gimage <- gobject@images[[image_name]] - if (is.null(gimage)) - warning("image_name: ", image_name, " does not exist \n") - } + ## get giotto image(s) ## + if (isTRUE(show_image) && is.null(gimage)) { + gimage <- getGiottoImage( + gobject = gobject, + name = image_name + ) } @@ -974,16 +897,14 @@ spatDeconvPlot <- function(gobject, pl <- pl + ggplot2::theme_bw() ## plot image ## - if (show_image == TRUE & !is.null(gimage)) { + if (isTRUE(show_image) && !is.null(gimage)) { pl <- plot_spat_image_layer_ggplot( gg_obj = pl, gobject = gobject, - feat_type = feat_type, spat_unit = spat_unit, + feat_type = feat_type, spat_loc_name = spat_loc_name, - gimage = gimage, - sdimx = sdimx, - sdimy = sdimy + gimage = gimage ) } @@ -1003,16 +924,15 @@ spatDeconvPlot <- function(gobject, ) - ## adjust theme settings - pl <- pl + ggplot2::theme( - plot.title = element_text(hjust = 0.5), - legend.title = element_blank(), - legend.text = element_text(size = legend_text), - axis.title = element_text(size = axis_title), - axis.text = element_text(size = axis_text), - panel.grid = element_blank(), - panel.background = element_rect(fill = background_color) + ## adjust theme setting + gg_theme_args <- c( + theme_param, + legend_text = legend_text, + axis_title = axis_title, + axis_text = axis_text, + background_color = background_color ) + pl <- pl + do.call(.gg_theme, args = gg_theme_args) # fix coord ratio if (!is.null(coord_fix_ratio)) { @@ -1033,8 +953,8 @@ spatDeconvPlot <- function(gobject, readGiottoInstructions(gobject, param = "save_plot"), save_plot) return_plot <- ifelse(is.null(return_plot), - readGiottoInstructions(gobject, param = "return_plot"), - return_plot) + readGiottoInstructions(gobject, param = "return_plot"), + return_plot) ## print plot if (show_plot == TRUE) { @@ -1069,53 +989,53 @@ spatDeconvPlot <- function(gobject, #' @noRd #' @keywords internal .dimPlot2D_single <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - dim_reduction_to_use = "umap", - dim_reduction_name = NULL, - dim1_to_use = 1, - dim2_to_use = 2, - spat_enr_names = NULL, - show_NN_network = FALSE, - nn_network_to_use = "sNN", - network_name = "sNN.pca", - cell_color = NULL, - color_as_factor = TRUE, - cell_color_code = NULL, - cell_color_gradient = NULL, - gradient_midpoint = NULL, - gradient_style = c("divergent", "sequential"), - gradient_limits = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 0.5, - show_cluster_center = FALSE, - show_center_label = TRUE, - center_point_size = 4, - center_point_border_col = "black", - center_point_border_stroke = 0.1, - label_size = 4, - label_fontface = "bold", - edge_alpha = NULL, - point_shape = c("border", "no_border"), - point_size = 1, - point_alpha = 1, - point_border_col = "black", - point_border_stroke = 0.1, - title = NULL, - show_legend = TRUE, - legend_text = 8, - legend_symbol_size = 1, - background_color = "white", - axis_text = 8, - axis_title = 8, - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "dimPlot2D_single") { + spat_unit = NULL, + feat_type = NULL, + dim_reduction_to_use = "umap", + dim_reduction_name = NULL, + dim1_to_use = 1, + dim2_to_use = 2, + spat_enr_names = NULL, + show_NN_network = FALSE, + nn_network_to_use = "sNN", + network_name = "sNN.pca", + cell_color = NULL, + color_as_factor = TRUE, + cell_color_code = NULL, + cell_color_gradient = NULL, + gradient_midpoint = NULL, + gradient_style = c("divergent", "sequential"), + gradient_limits = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 0.5, + show_cluster_center = FALSE, + show_center_label = TRUE, + center_point_size = 4, + center_point_border_col = "black", + center_point_border_stroke = 0.1, + label_size = 4, + label_fontface = "bold", + edge_alpha = NULL, + point_shape = c("border", "no_border"), + point_size = 1, + point_alpha = 1, + point_border_col = "black", + point_border_stroke = 0.1, + title = NULL, + show_legend = TRUE, + legend_text = 8, + legend_symbol_size = 1, + background_color = "white", + axis_text = 8, + axis_title = 8, + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "dimPlot2D_single") { checkmate::assert_class(gobject, "giotto") # Set feat_type and spat_unit @@ -1136,7 +1056,7 @@ spatDeconvPlot <- function(gobject, dim_reduction_name <- dim_reduction_to_use } else { dim_reduction_name <- paste0(feat_type, ".", - dim_reduction_to_use) + dim_reduction_to_use) } } } @@ -1155,8 +1075,8 @@ spatDeconvPlot <- function(gobject, if (!dim_reduction_name %in% dim_red_names) { stop("\n dimension reduction: ", dim_reduction_to_use, - " or dimension reduction name: ", dim_reduction_name, - " is not available \n") + " or dimension reduction name: ", dim_reduction_name, + " is not available \n") } @@ -1192,7 +1112,7 @@ spatDeconvPlot <- function(gobject, cell_metadata[, cell_ID := as.character(cell_ID)] annotated_DT <- data.table::merge.data.table(cell_metadata, - dim_DT, by = "cell_ID") + dim_DT, by = "cell_ID") # create input for network @@ -1214,27 +1134,27 @@ spatDeconvPlot <- function(gobject, old_dim_names <- dim_names annotated_network_DT <- merge(network_DT, dim_DT, by.x = "from", - by.y = "cell_ID") + by.y = "cell_ID") from_dim_names <- paste0("from_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = from_dim_names) + new = from_dim_names) annotated_network_DT <- merge(annotated_network_DT, dim_DT, - by.x = "to", by.y = "cell_ID") + by.x = "to", by.y = "cell_ID") to_dim_names <- paste0("to_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = to_dim_names) + new = to_dim_names) } # add % variance information if reduction is PCA if (dim_reduction_to_use == "pca") { pcaObj <- get_dimReduction(gobject, - spat_unit = spat_unit, - feat_type = feat_type, - reduction = "cells", - reduction_method = dim_reduction_to_use, - name = dim_reduction_name, - output = "dimObj" + spat_unit = spat_unit, + feat_type = feat_type, + reduction = "cells", + reduction_method = dim_reduction_to_use, + name = dim_reduction_name, + output = "dimObj" ) eigenvalues <- pcaObj@misc$eigenvalues @@ -1257,23 +1177,23 @@ spatDeconvPlot <- function(gobject, message("You have selected both individual cell IDs and a group of cells") group_cell_IDs <- annotated_DT[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] select_cells <- unique(c(select_cells, group_cell_IDs)) } else if (!is.null(select_cell_groups)) { select_cells <- annotated_DT[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] } if (!is.null(select_cells)) { annotated_DT_other <- annotated_DT[!annotated_DT$cell_ID %in% - select_cells] + select_cells] annotated_DT_selected <- annotated_DT[annotated_DT$cell_ID %in% - select_cells] + select_cells] if (show_NN_network == TRUE) { annotated_network_DT <- annotated_network_DT[ annotated_network_DT$to %in% select_cells & - annotated_network_DT$from %in% select_cells] + annotated_network_DT$from %in% select_cells] } # if specific cells are selected @@ -1376,9 +1296,9 @@ spatDeconvPlot <- function(gobject, # provide x, y and plot titles x_title <- sprintf("%s explains %.02f%% of variance", - x_name, var_expl_vec[dim1_to_use]) + x_name, var_expl_vec[dim1_to_use]) y_title <- sprintf("%s explains %.02f%% of variance", - y_name, var_expl_vec[dim2_to_use]) + y_name, var_expl_vec[dim2_to_use]) if (is.null(title)) title <- cell_color pl <- pl + ggplot2::labs(x = x_title, y = y_title, title = title) @@ -1445,59 +1365,59 @@ spatDeconvPlot <- function(gobject, #' dimPlot2D(g) #' @export dimPlot2D <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - group_by = NULL, - group_by_subset = NULL, - dim_reduction_to_use = "umap", - dim_reduction_name = NULL, - dim1_to_use = 1, - dim2_to_use = 2, - spat_enr_names = NULL, - show_NN_network = FALSE, - nn_network_to_use = "sNN", - network_name = "sNN.pca", - cell_color = NULL, - color_as_factor = TRUE, - cell_color_code = NULL, - cell_color_gradient = NULL, - gradient_midpoint = NULL, - gradient_style = c("divergent", "sequential"), - gradient_limits = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 0.5, - show_cluster_center = FALSE, - show_center_label = TRUE, - center_point_size = 4, - center_point_border_col = "black", - center_point_border_stroke = 0.1, - label_size = 4, - label_fontface = "bold", - edge_alpha = NULL, - point_shape = c("border", "no_border"), - point_size = 1, - point_alpha = 1, - point_border_col = "black", - point_border_stroke = 0.1, - title = NULL, - show_legend = TRUE, - legend_text = 10, - legend_symbol_size = 2, - background_color = "white", - axis_text = 8, - axis_title = 8, - cow_n_col = NULL, - cow_rel_h = 1, - cow_rel_w = 1, - cow_align = "h", - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "dimPlot2D") { + spat_unit = NULL, + feat_type = NULL, + group_by = NULL, + group_by_subset = NULL, + dim_reduction_to_use = "umap", + dim_reduction_name = NULL, + dim1_to_use = 1, + dim2_to_use = 2, + spat_enr_names = NULL, + show_NN_network = FALSE, + nn_network_to_use = "sNN", + network_name = "sNN.pca", + cell_color = NULL, + color_as_factor = TRUE, + cell_color_code = NULL, + cell_color_gradient = NULL, + gradient_midpoint = NULL, + gradient_style = c("divergent", "sequential"), + gradient_limits = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 0.5, + show_cluster_center = FALSE, + show_center_label = TRUE, + center_point_size = 4, + center_point_border_col = "black", + center_point_border_stroke = 0.1, + label_size = 4, + label_fontface = "bold", + edge_alpha = NULL, + point_shape = c("border", "no_border"), + point_size = 1, + point_alpha = 1, + point_border_col = "black", + point_border_stroke = 0.1, + title = NULL, + show_legend = TRUE, + legend_text = 10, + legend_symbol_size = 2, + background_color = "white", + axis_text = 8, + axis_title = 8, + cow_n_col = NULL, + cow_rel_h = 1, + cow_rel_w = 1, + cow_align = "h", + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "dimPlot2D") { # arg_list <- c(as.list(environment())) # get all args as list checkmate::assert_class(gobject, "giotto") @@ -1734,9 +1654,9 @@ dimPlot <- function(...) { #' plotUMAP_2D(g) #' @export plotUMAP_2D <- function(gobject, - dim_reduction_name = NULL, - default_save_name = "UMAP_2D", - ...) { + dim_reduction_name = NULL, + default_save_name = "UMAP_2D", + ...) { checkmate::assert_class(gobject, "giotto") dimPlot2D( @@ -1765,9 +1685,9 @@ plotUMAP_2D <- function(gobject, #' #' @export plotUMAP <- function(gobject, - dim_reduction_name = NULL, - default_save_name = "UMAP", - ...) { + dim_reduction_name = NULL, + default_save_name = "UMAP", + ...) { checkmate::assert_class(gobject, "giotto") dimPlot2D( @@ -1801,9 +1721,9 @@ plotUMAP <- function(gobject, #' #' @export plotTSNE_2D <- function(gobject, - dim_reduction_name = NULL, - default_save_name = "tSNE_2D", - ...) { + dim_reduction_name = NULL, + default_save_name = "tSNE_2D", + ...) { checkmate::assert_class(gobject, "giotto") dimPlot2D( @@ -1833,9 +1753,9 @@ plotTSNE_2D <- function(gobject, #' #' @export plotTSNE <- function(gobject, - dim_reduction_name = NULL, - default_save_name = "tSNE", - ...) { + dim_reduction_name = NULL, + default_save_name = "tSNE", + ...) { checkmate::assert_class(gobject, "giotto") dimPlot2D( @@ -1867,9 +1787,9 @@ plotTSNE <- function(gobject, #' #' @export plotPCA_2D <- function(gobject, - dim_reduction_name = NULL, - default_save_name = "PCA_2D", - ...) { + dim_reduction_name = NULL, + default_save_name = "PCA_2D", + ...) { checkmate::assert_class(gobject, "giotto") dimPlot2D( @@ -1901,9 +1821,9 @@ plotPCA_2D <- function(gobject, #' #' @export plotPCA <- function(gobject, - dim_reduction_name = NULL, - default_save_name = "PCA", - ...) { + dim_reduction_name = NULL, + default_save_name = "PCA", + ...) { checkmate::assert_class(gobject, "giotto") dimPlot2D( @@ -1942,6 +1862,7 @@ plotPCA <- function(gobject, #' @inheritParams plot_spatnet_params #' @inheritParams plot_image_params #' @inheritParams plot_params +#' @param largeImage_name deprecated #' @param spat_loc_name name of spatial locations #' @param plot_alignment direction to align plot #' @param sdimx = spatial dimension to use on x-axis @@ -1990,86 +1911,99 @@ plotPCA <- function(gobject, #' @export #' @seealso \code{\link{spatDimPlot3D}} spatDimPlot2D <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - show_image = FALSE, - gimage = NULL, - image_name = NULL, - largeImage_name = NULL, - spat_loc_name = NULL, - plot_alignment = c("vertical", "horizontal"), - dim_reduction_to_use = "umap", - dim_reduction_name = NULL, - dim1_to_use = 1, - dim2_to_use = 2, - sdimx = "sdimx", - sdimy = "sdimy", - spat_enr_names = NULL, - cell_color = NULL, - color_as_factor = TRUE, - cell_color_code = NULL, - cell_color_gradient = NULL, - gradient_midpoint = NULL, - gradient_style = c("divergent", "sequential"), - gradient_limits = NULL, - select_cell_groups = NULL, - select_cells = NULL, - dim_point_shape = c("border", "no_border"), - dim_point_size = 1, - dim_point_alpha = 1, - dim_point_border_col = "black", - dim_point_border_stroke = 0.1, - spat_point_shape = c("border", "no_border", "voronoi"), - spat_point_size = 1, - spat_point_alpha = 1, - spat_point_border_col = "black", - spat_point_border_stroke = 0.1, - dim_show_cluster_center = FALSE, - dim_show_center_label = TRUE, - dim_center_point_size = 4, - dim_center_point_border_col = "black", - dim_center_point_border_stroke = 0.1, - dim_label_size = 4, - dim_label_fontface = "bold", - spat_show_cluster_center = FALSE, - spat_show_center_label = FALSE, - spat_center_point_size = 4, - spat_center_point_border_col = "blue", - spat_center_point_border_stroke = 0.1, - spat_label_size = 4, - spat_label_fontface = "bold", - show_NN_network = FALSE, - nn_network_to_use = "sNN", - network_name = "sNN.pca", - nn_network_alpha = 0.05, - show_spatial_network = FALSE, - spat_network_name = "Delaunay_network", - spat_network_color = "blue", - spat_network_alpha = 0.5, - show_spatial_grid = FALSE, - spat_grid_name = "spatial_grid", - spat_grid_color = "blue", - show_other_cells = TRUE, - other_cell_color = "lightgrey", - dim_other_point_size = 1, - spat_other_point_size = 1, - spat_other_cells_alpha = 0.5, - dim_show_legend = FALSE, - spat_show_legend = FALSE, - legend_text = 10, - legend_symbol_size = 2, - dim_background_color = "white", - spat_background_color = "white", - vor_border_color = "white", - vor_max_radius = 200, - vor_alpha = 1, - axis_text = 8, - axis_title = 8, - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "spatDimPlot2D") { + spat_unit = NULL, + feat_type = NULL, + show_image = FALSE, + gimage = NULL, + image_name = NULL, + largeImage_name = NULL, + spat_loc_name = NULL, + plot_alignment = c("vertical", "horizontal"), + dim_reduction_to_use = "umap", + dim_reduction_name = NULL, + dim1_to_use = 1, + dim2_to_use = 2, + sdimx = "sdimx", + sdimy = "sdimy", + spat_enr_names = NULL, + cell_color = NULL, + color_as_factor = TRUE, + cell_color_code = NULL, + cell_color_gradient = NULL, + gradient_midpoint = NULL, + gradient_style = c("divergent", "sequential"), + gradient_limits = NULL, + select_cell_groups = NULL, + select_cells = NULL, + dim_point_shape = c("border", "no_border"), + dim_point_size = 1, + dim_point_alpha = 1, + dim_point_border_col = "black", + dim_point_border_stroke = 0.1, + spat_point_shape = c("border", "no_border", "voronoi"), + spat_point_size = 1, + spat_point_alpha = 1, + spat_point_border_col = "black", + spat_point_border_stroke = 0.1, + dim_show_cluster_center = FALSE, + dim_show_center_label = TRUE, + dim_center_point_size = 4, + dim_center_point_border_col = "black", + dim_center_point_border_stroke = 0.1, + dim_label_size = 4, + dim_label_fontface = "bold", + spat_show_cluster_center = FALSE, + spat_show_center_label = FALSE, + spat_center_point_size = 4, + spat_center_point_border_col = "blue", + spat_center_point_border_stroke = 0.1, + spat_label_size = 4, + spat_label_fontface = "bold", + show_NN_network = FALSE, + nn_network_to_use = "sNN", + network_name = "sNN.pca", + nn_network_alpha = 0.05, + show_spatial_network = FALSE, + spat_network_name = "Delaunay_network", + spat_network_color = "blue", + spat_network_alpha = 0.5, + show_spatial_grid = FALSE, + spat_grid_name = "spatial_grid", + spat_grid_color = "blue", + show_other_cells = TRUE, + other_cell_color = "lightgrey", + dim_other_point_size = 1, + spat_other_point_size = 1, + spat_other_cells_alpha = 0.5, + dim_show_legend = FALSE, + spat_show_legend = FALSE, + legend_text = 10, + legend_symbol_size = 2, + dim_background_color = "white", + spat_background_color = "white", + vor_border_color = "white", + vor_max_radius = 200, + vor_alpha = 1, + axis_text = 8, + axis_title = 8, + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "spatDimPlot2D") { + + # deprecation message + if (!is.null(largeImage_name)) { + deprecate_warn( + when = "0.2.0", + what = "spatDimPlot2D(largeImage_name)", + details = c( + "Use `image_name` argument instead for all images to plot." + ) + ) + image_name <- c(image_name, largeImage_name) + } + # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -2089,8 +2023,8 @@ spatDimPlot2D <- function(gobject, if (is.null(cell_color_code)) { if (is.character(cell_color)) { cell_metadata <- pDataDT(gobject, - spat_unit = spat_unit, - feat_type = feat_type + spat_unit = spat_unit, + feat_type = feat_type ) if (cell_color %in% colnames(cell_metadata)) { if (color_as_factor == TRUE) { @@ -2165,7 +2099,6 @@ spatDimPlot2D <- function(gobject, show_image = show_image, gimage = gimage, image_name = image_name, - largeImage_name = largeImage_name, spat_loc_name = spat_loc_name, group_by = NULL, group_by_subset = NULL, @@ -2225,14 +2158,14 @@ spatDimPlot2D <- function(gobject, ncol <- 1 nrow <- 2 combo_plot <- cowplot::plot_grid(dmpl, spl, ncol = ncol, - nrow = nrow, rel_heights = c(1), - rel_widths = c(1), align = "v") + nrow = nrow, rel_heights = c(1), + rel_widths = c(1), align = "v") } else { ncol <- 2 nrow <- 1 combo_plot <- cowplot::plot_grid(dmpl, spl, ncol = ncol, - nrow = nrow, rel_heights = c(1), - rel_widths = c(1), align = "h") + nrow = nrow, rel_heights = c(1), + rel_widths = c(1), align = "h") } return(plot_output_handler( @@ -2276,6 +2209,7 @@ spatDimPlot <- function(gobject, ...) { #' @inheritParams plot_cow_params #' @inheritParams plot_image_params #' @inheritParams plot_params +#' @param largeImage_name deprecated #' @param spat_loc_name name of spatial locations #' @param sdimx x-axis dimension name (default = 'sdimx') #' @param sdimy y-axis dimension name (default = 'sdimy') @@ -2302,6 +2236,7 @@ spatDimPlot <- function(gobject, ...) { #' @param vor_border_color border colorr for voronoi plot #' @param vor_max_radius maximum radius for voronoi 'cells' #' @param vor_alpha transparency of voronoi 'cells' +#' @param theme_param list of additional params passed to `ggplot2::theme()` #' @details Description of parameters. #' @family spatial feature expression visualizations #' @returns ggplot @@ -2310,7 +2245,7 @@ spatDimPlot <- function(gobject, ...) { #' spatFeatPlot2D_single(g, feats = c("Gna12", "Ccnd2", "Btbd17")) #' #' @export -#' @seealso \code{\link{spatGenePlot3D}} +#' @seealso \code{\link{spatFeatPlot3D}} spatFeatPlot2D_single <- function( gobject, spat_unit = NULL, @@ -2357,14 +2292,28 @@ spatFeatPlot2D_single <- function( cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", + theme_param = list(), show_plot = NULL, return_plot = NULL, save_plot = NULL, save_param = list(), - default_save_name = "spatFeatPlot2D_single") { + default_save_name = "spatFeatPlot2D_single" +) { # data.table variables cell_ID <- NULL + # deprecation message + if (!is.null(largeImage_name)) { + deprecate_warn( + when = "0.2.0", + what = "spatFeatPlot2D_single(largeImage_name)", + details = c( + "Use `image_name` argument instead for all images to plot." + ) + ) + image_name <- c(image_name, largeImage_name) + } + # print, return and save parameters show_plot <- ifelse(is.null(show_plot), readGiottoInstructions(gobject, param = "show_plot"), @@ -2373,8 +2322,8 @@ spatFeatPlot2D_single <- function( readGiottoInstructions(gobject, param = "save_plot"), save_plot) return_plot <- ifelse(is.null(return_plot), - readGiottoInstructions(gobject, param = "return_plot"), - return_plot) + readGiottoInstructions(gobject, param = "return_plot"), + return_plot) # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( @@ -2387,43 +2336,22 @@ spatFeatPlot2D_single <- function( feat_type = feat_type ) - ## giotto image ## - if (show_image == TRUE) { - if (!is.null(gimage)) { - gimage <- gimage - } else if (!is.null(image_name)) { - # if there is input to image_name arg - gimage <- gobject@images[[image_name]] - if (is.null(gimage)) warning("image_name: ", image_name, - " does not exist \n") - } else if (!is.null(largeImage_name)) { - # if there is input to largeImage_name arg - - gimage <- plot_auto_largeImage_resample( - gobject = gobject, - largeImage_name = largeImage_name, - spat_unit = spat_unit, - spat_loc_name = spat_loc_name, - include_image_in_border = TRUE - ) - } else { - # Default to first image available in images if no input given - # to image_name or largeImage_name args - image_name <- names(gobject@images)[1] - gimage <- gobject@images[[image_name]] - if (is.null(gimage)) warning("image_name: ", image_name, - " does not exist \n") - } + ## get giotto image(s) ## + if (isTRUE(show_image) && is.null(gimage)) { + gimage <- getGiottoImage( + gobject = gobject, + name = image_name + ) } # point shape point_shape <- match.arg(point_shape, - choices = c("border", "no_border", "voronoi")) + choices = c("border", "no_border", "voronoi")) # expression values values <- match.arg(expression_values, unique(c("normalized", "scaled", "custom", - expression_values))) + expression_values))) expr_values <- get_expression_values( gobject = gobject, spat_unit = spat_unit, @@ -2445,7 +2373,7 @@ spatFeatPlot2D_single <- function( "selected_feat" = subset_expr_data, "cell_ID" = colnames(expr_values)) data.table::setnames(t_sub_expr_data_DT, "selected_feat", - selected_feats) + selected_feats) } else { subset_expr_data <- expr_values[rownames(expr_values) %in% selected_feats, ] @@ -2551,12 +2479,10 @@ spatFeatPlot2D_single <- function( pl <- plot_spat_image_layer_ggplot( gg_obj = pl, gobject = gobject, - feat_type = feat_type, spat_unit = spat_unit, + feat_type = feat_type, spat_loc_name = spat_loc_name, - gimage = gimage, - sdimx = sdimx, - sdimy = sdimy + gimage = gimage ) } @@ -2623,7 +2549,7 @@ spatFeatPlot2D_single <- function( upper_lim <- gradient_limits[[2]] numeric_data <- cell_locations_metadata_feats[[feat]] limit_numeric_data <- ifelse(numeric_data > upper_lim, upper_lim, - ifelse(numeric_data < lower_lim, lower_lim, numeric_data) + ifelse(numeric_data < lower_lim, lower_lim, numeric_data) ) cell_locations_metadata_feats[[feat]] <- limit_numeric_data } @@ -2812,16 +2738,15 @@ spatFeatPlot2D_single <- function( pl <- pl + ggplot2::labs(x = "coord x", y = "coord y", title = feat) } - ## theme ## - pl <- pl + ggplot2::theme( - plot.title = element_text(hjust = 0.5), - legend.title = element_blank(), - legend.text = element_text(size = legend_text), - axis.title = element_text(size = axis_title), - axis.text = element_text(size = axis_text), - panel.grid = element_blank(), - panel.background = element_rect(fill = background_color) + ## adjust theme setting + gg_theme_args <- c( + theme_param, + legend_text = legend_text, + axis_title = axis_title, + axis_text = axis_text, + background_color = background_color ) + pl <- pl + do.call(.gg_theme, args = gg_theme_args) if (!is.null(coord_fix_ratio)) { pl <- pl + ggplot2::coord_fixed(ratio = coord_fix_ratio) @@ -2852,7 +2777,7 @@ spatFeatPlot2D_single <- function( if (save_plot == TRUE) { do.call("all_plots_save_function", c(list(gobject = gobject, plot_object = combo_plot, - default_save_name = default_save_name), save_param)) + default_save_name = default_save_name), save_param)) } ## return plot @@ -2873,6 +2798,7 @@ spatFeatPlot2D_single <- function( #' @inheritParams plot_image_params #' @inheritParams plot_params #' @inheritParams plot_spatnet_params +#' @param largeImage_name deprecated #' @param spat_loc_name name of spatial locations #' @param sdimx x-axis dimension name (default = 'sdimx') #' @param sdimy y-axis dimension name (default = 'sdimy') @@ -2894,6 +2820,7 @@ spatFeatPlot2D_single <- function( #' @param vor_alpha transparency of voronoi 'cells' #' @param axis_text size of axis text #' @param axis_title size of axis title +#' @param theme_param list of additional params passed to `ggplot2::theme()` #' @details Description of parameters. #' @family spatial feature expression visualizations #' @returns ggplot @@ -2902,58 +2829,72 @@ spatFeatPlot2D_single <- function( #' spatFeatPlot2D(g, feats = "Gna12") #' #' @export -#' @seealso \code{\link{spatGenePlot3D}} +#' @seealso \code{\link{spatFeatPlot3D}} spatFeatPlot2D <- function(gobject, - feat_type = NULL, - spat_unit = NULL, - show_image = FALSE, - gimage = NULL, - image_name = NULL, - largeImage_name = NULL, - spat_loc_name = NULL, - group_by = NULL, - group_by_subset = NULL, - sdimx = "sdimx", - sdimy = "sdimy", - expression_values = c("normalized", "scaled", "custom"), - feats, - order = TRUE, - cell_color_gradient = NULL, - gradient_midpoint = NULL, - gradient_style = c("divergent", "sequential"), - gradient_limits = NULL, - show_network = FALSE, - network_color = NULL, - edge_alpha = NULL, - spatial_network_name = "Delaunay_network", - show_grid = FALSE, - grid_color = NULL, - spatial_grid_name = "spatial_grid", - midpoint = 0, - scale_alpha_with_expression = FALSE, - point_shape = c("border", "no_border", "voronoi"), - point_size = 1, - point_alpha = 1, - point_border_col = "black", - point_border_stroke = 0.1, - coord_fix_ratio = 1, - show_legend = TRUE, - legend_text = 8, - background_color = "white", - vor_border_color = "white", - vor_alpha = 1, - vor_max_radius = 200, - axis_text = 8, - axis_title = 8, - cow_n_col = NULL, - cow_rel_h = 1, - cow_rel_w = 1, - cow_align = "h", - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "spatFeatPlot2D") { + feat_type = NULL, + spat_unit = NULL, + show_image = FALSE, + gimage = NULL, + image_name = NULL, + largeImage_name = NULL, + spat_loc_name = NULL, + group_by = NULL, + group_by_subset = NULL, + sdimx = "sdimx", + sdimy = "sdimy", + expression_values = c("normalized", "scaled", "custom"), + feats, + order = TRUE, + cell_color_gradient = NULL, + gradient_midpoint = NULL, + gradient_style = c("divergent", "sequential"), + gradient_limits = NULL, + show_network = FALSE, + network_color = NULL, + edge_alpha = NULL, + spatial_network_name = "Delaunay_network", + show_grid = FALSE, + grid_color = NULL, + spatial_grid_name = "spatial_grid", + midpoint = 0, + scale_alpha_with_expression = FALSE, + point_shape = c("border", "no_border", "voronoi"), + point_size = 1, + point_alpha = 1, + point_border_col = "black", + point_border_stroke = 0.1, + coord_fix_ratio = 1, + show_legend = TRUE, + legend_text = 8, + background_color = "white", + vor_border_color = "white", + vor_alpha = 1, + vor_max_radius = 200, + axis_text = 8, + axis_title = 8, + cow_n_col = NULL, + cow_rel_h = 1, + cow_rel_w = 1, + cow_align = "h", + theme_param = list(), + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "spatFeatPlot2D") { + + # deprecation message + if (!is.null(largeImage_name)) { + deprecate_warn( + when = "0.2.0", + what = "spatFeatPlot2D(largeImage_name)", + details = c( + "Use `image_name` argument instead for all images to plot." + ) + ) + image_name <- c(image_name, largeImage_name) + } + # create args list needed for each call to spatFeatPlot2D_single() # 1. - grab all params available # 2. - subset to those needed @@ -2962,7 +2903,7 @@ spatFeatPlot2D <- function(gobject, # [gobject params] "gobject", "feat_type", "spat_unit", # [image params] - "show_image", "gimage", "image_name", "largeImage_name", + "show_image", "gimage", "image_name", # [spatlocs params] "spat_loc_name", "sdimx", "sdimy", # [expression params] @@ -2984,7 +2925,9 @@ spatFeatPlot2D <- function(gobject, "cow_n_col", "cow_rel_h", "cow_rel_w", "cow_align", # [return params] "show_plot", "return_plot", "save_plot", "save_param", - "default_save_name" + "default_save_name", + # [theme params] + "theme_param" )] ## check group_by @@ -3032,8 +2975,6 @@ spatFeatPlot2D <- function(gobject, unique_groups <- unique_groups[unique_groups %in% group_by_subset] } - # group_by images - img_type <- ifelse(is.null(image_name), "largeImage", "image") # group_by static settings # # update these params @@ -3066,15 +3007,9 @@ spatFeatPlot2D <- function(gobject, # use a different image per group if there are the same number of # names provided as there are groups # Otherwise, use the same image (or NULL) for all groups (default) - switch(img_type, - "image" = if (length(unique_groups) == length(image_name)) { - sfp_params$image_name <- image_name[group_id] - }, - "largeImage" = if ( - length(unique_groups) == length(largeImage_name)) { - sfp_params$largeImage_name <- largeImage_name[group_id] - } - ) + if (length(unique_groups) == length(image_name)) { + sfp_params$image_name <- image_name[group_id] + } pl <- do.call(spatFeatPlot2D_single, args = sfp_params) @@ -3144,44 +3079,44 @@ spatFeatPlot2D <- function(gobject, #' #' @export dimFeatPlot2D <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - expression_values = c("normalized", "scaled", "custom"), - feats = NULL, - order = TRUE, - dim_reduction_to_use = "umap", - dim_reduction_name = NULL, - dim1_to_use = 1, - dim2_to_use = 2, - show_NN_network = FALSE, - nn_network_to_use = "sNN", - network_name = "sNN.pca", - network_color = "lightgray", - edge_alpha = NULL, - scale_alpha_with_expression = FALSE, - point_shape = c("border", "no_border"), - point_size = 1, - point_alpha = 1, - cell_color_gradient = NULL, - gradient_midpoint = NULL, - gradient_style = c("divergent", "sequential"), - gradient_limits = NULL, - point_border_col = "black", - point_border_stroke = 0.1, - show_legend = TRUE, - legend_text = 10, - background_color = "white", - axis_text = 8, - axis_title = 8, - cow_n_col = NULL, - cow_rel_h = 1, - cow_rel_w = 1, - cow_align = "h", - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "dimFeatPlot2D") { + spat_unit = NULL, + feat_type = NULL, + expression_values = c("normalized", "scaled", "custom"), + feats = NULL, + order = TRUE, + dim_reduction_to_use = "umap", + dim_reduction_name = NULL, + dim1_to_use = 1, + dim2_to_use = 2, + show_NN_network = FALSE, + nn_network_to_use = "sNN", + network_name = "sNN.pca", + network_color = "lightgray", + edge_alpha = NULL, + scale_alpha_with_expression = FALSE, + point_shape = c("border", "no_border"), + point_size = 1, + point_alpha = 1, + cell_color_gradient = NULL, + gradient_midpoint = NULL, + gradient_style = c("divergent", "sequential"), + gradient_limits = NULL, + point_border_col = "black", + point_border_stroke = 0.1, + show_legend = TRUE, + legend_text = 10, + background_color = "white", + axis_text = 8, + axis_title = 8, + cow_n_col = NULL, + cow_rel_h = 1, + cow_rel_w = 1, + cow_align = "h", + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "dimFeatPlot2D") { # print, return and save parameters show_plot <- ifelse(is.null(show_plot), readGiottoInstructions(gobject, param = "show_plot"), @@ -3190,8 +3125,8 @@ dimFeatPlot2D <- function(gobject, readGiottoInstructions(gobject, param = "save_plot"), save_plot) return_plot <- ifelse(is.null(return_plot), - readGiottoInstructions(gobject, param = "return_plot"), - return_plot) + readGiottoInstructions(gobject, param = "return_plot"), + return_plot) # point shape point_shape <- match.arg(point_shape, choices = c("border", "no_border")) @@ -3214,7 +3149,7 @@ dimFeatPlot2D <- function(gobject, dim_reduction_name <- dim_reduction_to_use } else { dim_reduction_name <- paste0(feat_type, ".", - dim_reduction_to_use) + dim_reduction_to_use) } } } @@ -3223,7 +3158,7 @@ dimFeatPlot2D <- function(gobject, # expression values values <- match.arg(expression_values, unique(c("normalized", "scaled", "custom", - expression_values))) + expression_values))) expr_values <- get_expression_values( gobject = gobject, spat_unit = spat_unit, @@ -3244,7 +3179,7 @@ dimFeatPlot2D <- function(gobject, "selected_feat" = subset_expr_data, "cell_ID" = colnames(expr_values)) data.table::setnames(t_sub_expr_data_DT, "selected_feat", - selected_feats) + selected_feats) } else { subset_expr_data <- expr_values[rownames(expr_values) %in% selected_feats, ] @@ -3276,19 +3211,19 @@ dimFeatPlot2D <- function(gobject, ## annotated cell metadata cell_metadata <- get_cell_metadata(gobject, - spat_unit = spat_unit, - feat_type = feat_type, - output = "data.table", - copy_obj = TRUE + spat_unit = spat_unit, + feat_type = feat_type, + output = "data.table", + copy_obj = TRUE ) annotated_DT <- data.table::merge.data.table(cell_metadata, - dim_DT, by = "cell_ID") + dim_DT, by = "cell_ID") ## merge feat info annotated_feat_DT <- data.table::merge.data.table(annotated_DT, - t_sub_expr_data_DT, - by = "cell_ID") + t_sub_expr_data_DT, + by = "cell_ID") # create input for network if (show_NN_network == TRUE) { @@ -3312,13 +3247,13 @@ dimFeatPlot2D <- function(gobject, network_DT, dim_DT, by.x = "from", by.y = "cell_ID") from_dim_names <- paste0("from_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = from_dim_names) + new = from_dim_names) annotated_network_DT <- data.table::merge.data.table( annotated_network_DT, dim_DT, by.x = "to", by.y = "cell_ID") to_dim_names <- paste0("to_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = to_dim_names) + new = to_dim_names) } ## visualize multiple plots ## @@ -3395,8 +3330,8 @@ dimFeatPlot2D <- function(gobject, upper_lim <- gradient_limits[[2]] numeric_data <- annotated_feat_DT[[feat]] limit_numeric_data <- ifelse(numeric_data > upper_lim, - upper_lim, - ifelse(numeric_data < lower_lim, lower_lim, numeric_data) + upper_lim, + ifelse(numeric_data < lower_lim, lower_lim, numeric_data) ) annotated_feat_DT[[feat]] <- limit_numeric_data } @@ -3525,7 +3460,7 @@ dimFeatPlot2D <- function(gobject, if (save_plot == TRUE) { do.call("all_plots_save_function", c(list(gobject = gobject, plot_object = combo_plot, - default_save_name = default_save_name), save_param)) + default_save_name = default_save_name), save_param)) } ## return plot @@ -3591,67 +3526,67 @@ dimFeatPlot2D <- function(gobject, #' #' @export spatDimFeatPlot2D <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - show_image = FALSE, - gimage = NULL, - image_name = NULL, - largeImage_name = NULL, - expression_values = c("normalized", "scaled", "custom"), - plot_alignment = c("vertical", "horizontal"), - feats, - order = TRUE, - dim_reduction_to_use = "umap", - dim_reduction_name = "umap", - dim1_to_use = 1, - dim2_to_use = 2, - dim_point_shape = c("border", "no_border"), - dim_point_size = 1, - dim_point_alpha = 1, - dim_point_border_col = "black", - dim_point_border_stroke = 0.1, - show_NN_network = FALSE, - show_spatial_network = FALSE, - dim_network_color = "gray", - nn_network_to_use = "sNN", - network_name = "sNN.pca", - dim_edge_alpha = NULL, - scale_alpha_with_expression = FALSE, - sdimx = "sdimx", - sdimy = "sdimy", - spatial_network_name = "Delaunay_network", - spatial_network_color = NULL, - show_spatial_grid = FALSE, - grid_color = NULL, - spatial_grid_name = "spatial_grid", - spat_point_shape = c("border", "no_border", "voronoi"), - spat_point_size = 1, - spat_point_alpha = 1, - spat_point_border_col = "black", - spat_point_border_stroke = 0.1, - spat_edge_alpha = NULL, - cell_color_gradient = NULL, - gradient_midpoint = NULL, - gradient_style = c("divergent", "sequential"), - gradient_limits = NULL, - cow_n_col = NULL, - cow_rel_h = 1, - cow_rel_w = 1, - cow_align = "h", - show_legend = TRUE, - legend_text = 10, - dim_background_color = "white", - spat_background_color = "white", - vor_border_color = "white", - vor_max_radius = 200, - vor_alpha = 1, - axis_text = 8, - axis_title = 8, - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "spatDimFeatPlot2D") { + spat_unit = NULL, + feat_type = NULL, + show_image = FALSE, + gimage = NULL, + image_name = NULL, + largeImage_name = NULL, + expression_values = c("normalized", "scaled", "custom"), + plot_alignment = c("vertical", "horizontal"), + feats, + order = TRUE, + dim_reduction_to_use = "umap", + dim_reduction_name = "umap", + dim1_to_use = 1, + dim2_to_use = 2, + dim_point_shape = c("border", "no_border"), + dim_point_size = 1, + dim_point_alpha = 1, + dim_point_border_col = "black", + dim_point_border_stroke = 0.1, + show_NN_network = FALSE, + show_spatial_network = FALSE, + dim_network_color = "gray", + nn_network_to_use = "sNN", + network_name = "sNN.pca", + dim_edge_alpha = NULL, + scale_alpha_with_expression = FALSE, + sdimx = "sdimx", + sdimy = "sdimy", + spatial_network_name = "Delaunay_network", + spatial_network_color = NULL, + show_spatial_grid = FALSE, + grid_color = NULL, + spatial_grid_name = "spatial_grid", + spat_point_shape = c("border", "no_border", "voronoi"), + spat_point_size = 1, + spat_point_alpha = 1, + spat_point_border_col = "black", + spat_point_border_stroke = 0.1, + spat_edge_alpha = NULL, + cell_color_gradient = NULL, + gradient_midpoint = NULL, + gradient_style = c("divergent", "sequential"), + gradient_limits = NULL, + cow_n_col = NULL, + cow_rel_h = 1, + cow_rel_w = 1, + cow_align = "h", + show_legend = TRUE, + legend_text = 10, + dim_background_color = "white", + spat_background_color = "white", + vor_border_color = "white", + vor_max_radius = 200, + vor_alpha = 1, + axis_text = 8, + axis_title = 8, + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "spatDimFeatPlot2D") { plot_alignment <- match.arg(plot_alignment, choices = c("vertical", "horizontal")) @@ -3808,6 +3743,7 @@ spatDimFeatPlot2D <- function(gobject, #' @param vor_border_color border colorr for voronoi plot #' @param vor_max_radius maximum radius for voronoi 'cells' #' @param vor_alpha transparency of voronoi 'cells' +#' @param theme_param list of additional params passed to `ggplot2::theme()` #' @details Description of parameters. #' @family spatial cell annotation visualizations #' @returns ggplot @@ -3817,64 +3753,65 @@ spatDimFeatPlot2D <- function(gobject, #' #' @export spatCellPlot2D <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - show_image = FALSE, - gimage = NULL, - image_name = NULL, - largeImage_name = NULL, - sdimx = "sdimx", - sdimy = "sdimy", - spat_enr_names = NULL, - cell_annotation_values = NULL, - cell_color_gradient = NULL, - gradient_midpoint = NULL, - gradient_style = c("divergent", "sequential"), - gradient_limits = NULL, - select_cell_groups = NULL, - select_cells = NULL, - point_shape = c("border", "no_border", "voronoi"), - point_size = 3, - point_alpha = 1, - point_border_col = "black", - point_border_stroke = 0.1, - show_cluster_center = FALSE, - show_center_label = FALSE, - center_point_size = 4, - center_point_border_col = "black", - center_point_border_stroke = 0.1, - label_size = 4, - label_fontface = "bold", - show_network = FALSE, - spatial_network_name = "Delaunay_network", - network_color = NULL, - network_alpha = 1, - show_grid = FALSE, - spatial_grid_name = "spatial_grid", - grid_color = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 1, - other_cells_alpha = 0.1, - coord_fix_ratio = 1, - show_legend = TRUE, - legend_text = 8, - legend_symbol_size = 1, - background_color = "white", - vor_border_color = "white", - vor_max_radius = 200, - vor_alpha = 1, - axis_text = 8, - axis_title = 8, - cow_n_col = NULL, - cow_rel_h = 1, - cow_rel_w = 1, - cow_align = "h", - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "spatCellPlot2D") { + spat_unit = NULL, + feat_type = NULL, + show_image = FALSE, + gimage = NULL, + image_name = NULL, + largeImage_name = NULL, + sdimx = "sdimx", + sdimy = "sdimy", + spat_enr_names = NULL, + cell_annotation_values = NULL, + cell_color_gradient = NULL, + gradient_midpoint = NULL, + gradient_style = c("divergent", "sequential"), + gradient_limits = NULL, + select_cell_groups = NULL, + select_cells = NULL, + point_shape = c("border", "no_border", "voronoi"), + point_size = 3, + point_alpha = 1, + point_border_col = "black", + point_border_stroke = 0.1, + show_cluster_center = FALSE, + show_center_label = FALSE, + center_point_size = 4, + center_point_border_col = "black", + center_point_border_stroke = 0.1, + label_size = 4, + label_fontface = "bold", + show_network = FALSE, + spatial_network_name = "Delaunay_network", + network_color = NULL, + network_alpha = 1, + show_grid = FALSE, + spatial_grid_name = "spatial_grid", + grid_color = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 1, + other_cells_alpha = 0.1, + coord_fix_ratio = 1, + show_legend = TRUE, + legend_text = 8, + legend_symbol_size = 1, + background_color = "white", + vor_border_color = "white", + vor_max_radius = 200, + vor_alpha = 1, + axis_text = 8, + axis_title = 8, + cow_n_col = NULL, + cow_rel_h = 1, + cow_rel_w = 1, + cow_align = "h", + theme_param = list(), + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "spatCellPlot2D") { # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -3962,6 +3899,8 @@ spatCellPlot2D <- function(gobject, vor_alpha = vor_alpha, axis_text = axis_text, axis_title = axis_title, + theme_param = theme_param, + # hardcoded on purpose below show_plot = FALSE, return_plot = TRUE, save_plot = FALSE, @@ -4036,55 +3975,55 @@ spatCellPlot <- function(...) { #' #' @export dimCellPlot2D <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - dim_reduction_to_use = "umap", - dim_reduction_name = "umap", - dim1_to_use = 1, - dim2_to_use = 2, - spat_enr_names = NULL, - cell_annotation_values = NULL, - show_NN_network = FALSE, - nn_network_to_use = "sNN", - network_name = "sNN.pca", - cell_color_code = NULL, - cell_color_gradient = NULL, - gradient_midpoint = NULL, - gradient_style = c("divergent", "sequential"), - gradient_limits = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 0.5, - show_cluster_center = FALSE, - show_center_label = TRUE, - center_point_size = 4, - center_point_border_col = "black", - center_point_border_stroke = 0.1, - label_size = 4, - label_fontface = "bold", - edge_alpha = NULL, - point_shape = c("border", "no_border"), - point_size = 1, - point_alpha = 1, - point_border_col = "black", - point_border_stroke = 0.1, - show_legend = TRUE, - legend_text = 8, - legend_symbol_size = 1, - background_color = "white", - axis_text = 8, - axis_title = 8, - cow_n_col = NULL, - cow_rel_h = 1, - cow_rel_w = 1, - cow_align = "h", - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "dimCellPlot2D") { + spat_unit = NULL, + feat_type = NULL, + dim_reduction_to_use = "umap", + dim_reduction_name = "umap", + dim1_to_use = 1, + dim2_to_use = 2, + spat_enr_names = NULL, + cell_annotation_values = NULL, + show_NN_network = FALSE, + nn_network_to_use = "sNN", + network_name = "sNN.pca", + cell_color_code = NULL, + cell_color_gradient = NULL, + gradient_midpoint = NULL, + gradient_style = c("divergent", "sequential"), + gradient_limits = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 0.5, + show_cluster_center = FALSE, + show_center_label = TRUE, + center_point_size = 4, + center_point_border_col = "black", + center_point_border_stroke = 0.1, + label_size = 4, + label_fontface = "bold", + edge_alpha = NULL, + point_shape = c("border", "no_border"), + point_size = 1, + point_alpha = 1, + point_border_col = "black", + point_border_stroke = 0.1, + show_legend = TRUE, + legend_text = 8, + legend_symbol_size = 1, + background_color = "white", + axis_text = 8, + axis_title = 8, + cow_n_col = NULL, + cow_rel_h = 1, + cow_rel_w = 1, + cow_align = "h", + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "dimCellPlot2D") { # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -4280,87 +4219,87 @@ dimCellPlot <- function(gobject, ...) { #' #' @export spatDimCellPlot2D <- function(gobject, - feat_type = NULL, - spat_unit = NULL, - show_image = FALSE, - gimage = NULL, - image_name = NULL, - largeImage_name = NULL, - plot_alignment = c("vertical", "horizontal"), - spat_enr_names = NULL, - cell_annotation_values = NULL, - dim_reduction_to_use = "umap", - dim_reduction_name = "umap", - dim1_to_use = 1, - dim2_to_use = 2, - sdimx = "sdimx", - sdimy = "sdimy", - cell_color_gradient = NULL, - gradient_midpoint = NULL, - gradient_style = c("divergent", "sequential"), - gradient_limits = NULL, - select_cell_groups = NULL, - select_cells = NULL, - dim_point_shape = c("border", "no_border"), - dim_point_size = 1, - dim_point_alpha = 1, - dim_point_border_col = "black", - dim_point_border_stroke = 0.1, - spat_point_shape = c("border", "no_border", "voronoi"), - spat_point_size = 1, - spat_point_alpha = 1, - spat_point_border_col = "black", - spat_point_border_stroke = 0.1, - dim_show_cluster_center = FALSE, - dim_show_center_label = TRUE, - dim_center_point_size = 4, - dim_center_point_border_col = "black", - dim_center_point_border_stroke = 0.1, - dim_label_size = 4, - dim_label_fontface = "bold", - spat_show_cluster_center = FALSE, - spat_show_center_label = FALSE, - spat_center_point_size = 4, - spat_center_point_border_col = "black", - spat_center_point_border_stroke = 0.1, - spat_label_size = 4, - spat_label_fontface = "bold", - show_NN_network = FALSE, - nn_network_to_use = "sNN", - nn_network_name = "sNN.pca", - dim_edge_alpha = 0.5, - spat_show_network = FALSE, - spatial_network_name = "Delaunay_network", - spat_network_color = "red", - spat_network_alpha = 0.5, - spat_show_grid = FALSE, - spatial_grid_name = "spatial_grid", - spat_grid_color = "green", - show_other_cells = TRUE, - other_cell_color = "grey", - dim_other_point_size = 0.5, - spat_other_point_size = 0.5, - spat_other_cells_alpha = 0.5, - show_legend = TRUE, - legend_text = 8, - legend_symbol_size = 1, - dim_background_color = "white", - spat_background_color = "white", - vor_border_color = "white", - vor_max_radius = 200, - vor_alpha = 1, - axis_text = 8, - axis_title = 8, - coord_fix_ratio = 1, - cow_n_col = NULL, - cow_rel_h = 1, - cow_rel_w = 1, - cow_align = "h", - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "spatDimCellPlot2D") { + feat_type = NULL, + spat_unit = NULL, + show_image = FALSE, + gimage = NULL, + image_name = NULL, + largeImage_name = NULL, + plot_alignment = c("vertical", "horizontal"), + spat_enr_names = NULL, + cell_annotation_values = NULL, + dim_reduction_to_use = "umap", + dim_reduction_name = "umap", + dim1_to_use = 1, + dim2_to_use = 2, + sdimx = "sdimx", + sdimy = "sdimy", + cell_color_gradient = NULL, + gradient_midpoint = NULL, + gradient_style = c("divergent", "sequential"), + gradient_limits = NULL, + select_cell_groups = NULL, + select_cells = NULL, + dim_point_shape = c("border", "no_border"), + dim_point_size = 1, + dim_point_alpha = 1, + dim_point_border_col = "black", + dim_point_border_stroke = 0.1, + spat_point_shape = c("border", "no_border", "voronoi"), + spat_point_size = 1, + spat_point_alpha = 1, + spat_point_border_col = "black", + spat_point_border_stroke = 0.1, + dim_show_cluster_center = FALSE, + dim_show_center_label = TRUE, + dim_center_point_size = 4, + dim_center_point_border_col = "black", + dim_center_point_border_stroke = 0.1, + dim_label_size = 4, + dim_label_fontface = "bold", + spat_show_cluster_center = FALSE, + spat_show_center_label = FALSE, + spat_center_point_size = 4, + spat_center_point_border_col = "black", + spat_center_point_border_stroke = 0.1, + spat_label_size = 4, + spat_label_fontface = "bold", + show_NN_network = FALSE, + nn_network_to_use = "sNN", + nn_network_name = "sNN.pca", + dim_edge_alpha = 0.5, + spat_show_network = FALSE, + spatial_network_name = "Delaunay_network", + spat_network_color = "red", + spat_network_alpha = 0.5, + spat_show_grid = FALSE, + spatial_grid_name = "spatial_grid", + spat_grid_color = "green", + show_other_cells = TRUE, + other_cell_color = "grey", + dim_other_point_size = 0.5, + spat_other_point_size = 0.5, + spat_other_cells_alpha = 0.5, + show_legend = TRUE, + legend_text = 8, + legend_symbol_size = 1, + dim_background_color = "white", + spat_background_color = "white", + vor_border_color = "white", + vor_max_radius = 200, + vor_alpha = 1, + axis_text = 8, + axis_title = 8, + coord_fix_ratio = 1, + cow_n_col = NULL, + cow_rel_h = 1, + cow_rel_w = 1, + cow_align = "h", + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "spatDimCellPlot2D") { plot_alignment <- match.arg(plot_alignment, choices = c("vertical", "horizontal")) @@ -4481,14 +4420,14 @@ spatDimCellPlot2D <- function(gobject, ncol <- 1 nrow <- 2 combo_plot <- cowplot::plot_grid(dmpl, spl, ncol = ncol, nrow = nrow, - rel_heights = c(1), rel_widths = c(1), - align = "v") + rel_heights = c(1), rel_widths = c(1), + align = "v") } else { ncol <- 2 nrow <- 1 combo_plot <- cowplot::plot_grid(dmpl, spl, ncol = ncol, nrow = nrow, - rel_heights = c(1), rel_widths = c(1), - align = "h") + rel_heights = c(1), rel_widths = c(1), + align = "h") } return(plot_output_handler( @@ -4542,30 +4481,30 @@ spatDimCellPlot <- function(...) { #' @returns plotly object #' @keywords internal .dimPlot_2d_plotly <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - dim_reduction_to_use = "umap", - dim_reduction_name = "umap", - dim1_to_use = 1, - dim2_to_use = 2, - spat_enr_names = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 0.5, - show_NN_network = FALSE, - nn_network_to_use = "sNN", - network_name = "sNN.pca", - color_as_factor = TRUE, - cell_color = NULL, - cell_color_code = NULL, - show_cluster_center = FALSE, - show_center_label = TRUE, - center_point_size = 4, - label_size = 4, - edge_alpha = NULL, - point_size = 5) { + spat_unit = NULL, + feat_type = NULL, + dim_reduction_to_use = "umap", + dim_reduction_name = "umap", + dim1_to_use = 1, + dim2_to_use = 2, + spat_enr_names = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 0.5, + show_NN_network = FALSE, + nn_network_to_use = "sNN", + network_name = "sNN.pca", + color_as_factor = TRUE, + cell_color = NULL, + cell_color_code = NULL, + show_cluster_center = FALSE, + show_center_label = TRUE, + center_point_size = 4, + label_size = 4, + edge_alpha = NULL, + point_size = 5) { # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -4625,16 +4564,16 @@ spatDimCellPlot <- function(...) { old_dim_names <- dim_names annotated_network_DT <- merge(network_DT, dim_DT, by.x = "from", - by.y = "cell_ID") + by.y = "cell_ID") from_dim_names <- paste0("from_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = from_dim_names) + new = from_dim_names) annotated_network_DT <- merge(annotated_network_DT, dim_DT, - by.x = "to", by.y = "cell_ID") + by.x = "to", by.y = "cell_ID") to_dim_names <- paste0("to_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = to_dim_names) + new = to_dim_names) } @@ -4667,19 +4606,19 @@ spatDimCellPlot <- function(...) { message("You have selected both individual cell IDs and a group of cells") group_cell_IDs <- annotated_DT[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] select_cells <- unique(c(select_cells, group_cell_IDs)) } else if (!is.null(select_cell_groups)) { select_cells <- annotated_DT[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] } if (!is.null(select_cells)) { annotated_DT_other <- annotated_DT[!annotated_DT$cell_ID %in% - select_cells] + select_cells] annotated_DT_selected <- annotated_DT[annotated_DT$cell_ID %in% - select_cells] + select_cells] if (show_NN_network == TRUE) { annotated_network_DT <- annotated_network_DT[ @@ -4767,7 +4706,7 @@ spatDimCellPlot <- function(...) { y = annotated_DT_other[[dim_names[2]]], # legendgroup = annotated_DT[[cell_color]], marker = list(size = other_point_size, - color = other_cell_color), + color = other_cell_color), showlegend = FALSE ) } @@ -4790,7 +4729,7 @@ spatDimCellPlot <- function(...) { colors = cell_color_code, legendgroup = annotated_DT_centers[[cell_color]], marker = list(size = center_point_size, symbol = "x", - symbols = "x"), + symbols = "x"), showlegend = FALSE ) } @@ -4819,9 +4758,9 @@ spatDimCellPlot <- function(...) { x_name <- paste0("pca", "-", dim_names[1]) y_name <- paste0("pca", "-", dim_names[2]) x_title <- sprintf("%s explains %.02f%% of variance", - x_name, var_expl_vec[1]) + x_name, var_expl_vec[1]) y_title <- sprintf("%s explains %.02f%% of variance", y_name, - var_expl_vec[2]) + var_expl_vec[2]) } } else { x_title <- paste(dim_reduction_to_use, dim_names[1], sep = " ") @@ -4845,31 +4784,31 @@ spatDimCellPlot <- function(...) { #' @returns plotly object #' @keywords internal .dimPlot_3d_plotly <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - dim_reduction_to_use = "umap", - dim_reduction_name = "umap", - dim1_to_use = 1, - dim2_to_use = 2, - dim3_to_use = 3, - spat_enr_names = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 0.5, - show_NN_network = FALSE, - nn_network_to_use = "sNN", - network_name = "sNN.pca", - color_as_factor = TRUE, - cell_color = NULL, - cell_color_code = NULL, - show_cluster_center = FALSE, - show_center_label = TRUE, - center_point_size = 4, - label_size = 4, - edge_alpha = NULL, - point_size = 1) { + spat_unit = NULL, + feat_type = NULL, + dim_reduction_to_use = "umap", + dim_reduction_name = "umap", + dim1_to_use = 1, + dim2_to_use = 2, + dim3_to_use = 3, + spat_enr_names = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 0.5, + show_NN_network = FALSE, + nn_network_to_use = "sNN", + network_name = "sNN.pca", + color_as_factor = TRUE, + cell_color = NULL, + cell_color_code = NULL, + show_cluster_center = FALSE, + show_center_label = TRUE, + center_point_size = 4, + label_size = 4, + edge_alpha = NULL, + point_size = 1) { # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -4927,16 +4866,16 @@ spatDimCellPlot <- function(...) { old_dim_names <- dim_names annotated_network_DT <- merge(network_DT, dim_DT, by.x = "from", - by.y = "cell_ID") + by.y = "cell_ID") from_dim_names <- paste0("from_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = from_dim_names) + new = from_dim_names) annotated_network_DT <- merge(annotated_network_DT, dim_DT, - by.x = "to", by.y = "cell_ID") + by.x = "to", by.y = "cell_ID") to_dim_names <- paste0("to_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = to_dim_names) + new = to_dim_names) } if (dim_reduction_to_use == "pca") { @@ -4968,18 +4907,18 @@ spatDimCellPlot <- function(...) { message("You have selected both individual cell IDs and a group of cells") group_cell_IDs <- annotated_DT[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] select_cells <- unique(c(select_cells, group_cell_IDs)) } else if (!is.null(select_cell_groups)) { select_cells <- annotated_DT[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] } if (!is.null(select_cells)) { annotated_DT_other <- annotated_DT[!annotated_DT$cell_ID %in% - select_cells] + select_cells] annotated_DT_selected <- annotated_DT[annotated_DT$cell_ID %in% - select_cells] + select_cells] if (show_NN_network == TRUE) { annotated_network_DT <- annotated_network_DT[ @@ -5046,7 +4985,7 @@ spatDimCellPlot <- function(...) { z = annotated_DT_other[[dim_names[3]]], # colors = other_cell_color, marker = list(size = other_point_size, - color = other_cell_color), + color = other_cell_color), showlegend = FALSE ) } @@ -5132,11 +5071,11 @@ spatDimCellPlot <- function(...) { y_name <- paste0("pca", "-", dim_names[2]) z_name <- paste0("pca", "-", dim_names[3]) x_title <- sprintf("%s explains %.02f%% of variance", - x_name, var_expl_vec[1]) + x_name, var_expl_vec[1]) y_title <- sprintf("%s explains %.02f%% of variance", - y_name, var_expl_vec[2]) + y_name, var_expl_vec[2]) z_title <- sprintf("%s explains %.02f%% of variance", - z_name, var_expl_vec[3]) + z_name, var_expl_vec[3]) } } else { x_title <- paste(dim_reduction_to_use, dim_names[1], sep = " ") @@ -5150,7 +5089,7 @@ spatDimCellPlot <- function(...) { zaxis = list(title = z_title) ), legend = list(x = 100, y = 0.5, - font = list(family = "sans-serif", size = 12)) + font = list(family = "sans-serif", size = 12)) ) return(pl) } @@ -5165,36 +5104,36 @@ spatDimCellPlot <- function(...) { #' @returns plotly (dimplot3D only) #' @export dimPlot3D <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - dim_reduction_to_use = "umap", - dim_reduction_name = "umap", - dim1_to_use = 1, - dim2_to_use = 2, - dim3_to_use = 3, - spat_enr_names = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 2, - show_NN_network = FALSE, - nn_network_to_use = "sNN", - network_name = "sNN.pca", - color_as_factor = TRUE, - cell_color = NULL, - cell_color_code = NULL, - show_cluster_center = FALSE, - show_center_label = TRUE, - center_point_size = 4, - label_size = 4, - edge_alpha = NULL, - point_size = 3, - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "dim3D") { + spat_unit = NULL, + feat_type = NULL, + dim_reduction_to_use = "umap", + dim_reduction_name = "umap", + dim1_to_use = 1, + dim2_to_use = 2, + dim3_to_use = 3, + spat_enr_names = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 2, + show_NN_network = FALSE, + nn_network_to_use = "sNN", + network_name = "sNN.pca", + color_as_factor = TRUE, + cell_color = NULL, + cell_color_code = NULL, + show_cluster_center = FALSE, + show_center_label = TRUE, + center_point_size = 4, + label_size = 4, + edge_alpha = NULL, + point_size = 3, + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "dim3D") { if (is.null(dim3_to_use)) { message("create 2D plot") @@ -5265,8 +5204,8 @@ dimPlot3D <- function(gobject, readGiottoInstructions(gobject, param = "save_plot"), save_plot) return_plot <- ifelse(is.null(return_plot), - readGiottoInstructions(gobject, param = "return_plot"), - return_plot) + readGiottoInstructions(gobject, param = "return_plot"), + return_plot) ## print plot if (show_plot == TRUE) { @@ -5277,7 +5216,7 @@ dimPlot3D <- function(gobject, if (save_plot == TRUE) { do.call("all_plots_save_function", c(list(gobject = gobject, plot_object = pl, - default_save_name = default_save_name), save_param)) + default_save_name = default_save_name), save_param)) } ## return plot @@ -5304,9 +5243,9 @@ dimPlot3D <- function(gobject, #' #' @export plotUMAP_3D <- function(gobject, - dim_reduction_name = "umap", - default_save_name = "UMAP_3D", - ...) { + dim_reduction_name = "umap", + default_save_name = "UMAP_3D", + ...) { dimPlot3D( gobject = gobject, dim_reduction_to_use = "umap", @@ -5334,9 +5273,9 @@ plotUMAP_3D <- function(gobject, #' #' @export plotTSNE_3D <- function(gobject, - dim_reduction_name = "tsne", - default_save_name = "TSNE_3D", - ...) { + dim_reduction_name = "tsne", + default_save_name = "TSNE_3D", + ...) { dimPlot3D( gobject = gobject, dim_reduction_to_use = "tsne", @@ -5364,9 +5303,9 @@ plotTSNE_3D <- function(gobject, #' #' @export plotPCA_3D <- function(gobject, - dim_reduction_name = "pca", - default_save_name = "PCA_3D", - ...) { + dim_reduction_name = "pca", + default_save_name = "PCA_3D", + ...) { dimPlot3D( gobject = gobject, dim_reduction_to_use = "pca", @@ -5390,36 +5329,36 @@ plotPCA_3D <- function(gobject, #' @returns plotly object #' @keywords internal .spatPlot_2d_plotly <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - spat_loc_name = "raw", - sdimx = NULL, - sdimy = NULL, - spat_enr_names = NULL, - point_size = 3, - cell_color = NULL, - cell_color_code = NULL, - color_as_factor = TRUE, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 0.5, - show_network = FALSE, - spatial_network_name = "spatial_network", - network_color = "lightgray", - network_alpha = 1, - other_cell_alpha = 0.5, - show_grid = FALSE, - spatial_grid_name = "spatial_grid", - grid_color = NULL, - grid_alpha = 1, - show_legend = TRUE, - axis_scale = c("cube", "real", "custom"), - custom_ratio = NULL, - x_ticks = NULL, - y_ticks = NULL, - show_plot = FALSE) { + spat_unit = NULL, + feat_type = NULL, + spat_loc_name = "raw", + sdimx = NULL, + sdimy = NULL, + spat_enr_names = NULL, + point_size = 3, + cell_color = NULL, + cell_color_code = NULL, + color_as_factor = TRUE, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 0.5, + show_network = FALSE, + spatial_network_name = "spatial_network", + network_color = "lightgray", + network_alpha = 1, + other_cell_alpha = 0.5, + show_grid = FALSE, + spatial_grid_name = "spatial_grid", + grid_color = NULL, + grid_alpha = 1, + show_legend = TRUE, + axis_scale = c("cube", "real", "custom"), + custom_ratio = NULL, + x_ticks = NULL, + y_ticks = NULL, + show_plot = FALSE) { # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -5433,9 +5372,9 @@ plotPCA_3D <- function(gobject, ## get spatial cell locations cell_locations <- get_spatial_locations(gobject, - spat_unit = spat_unit, - spat_loc_name = spat_loc_name, - output = "data.table" + spat_unit = spat_unit, + spat_loc_name = spat_loc_name, + output = "data.table" ) if (is.null(cell_locations)) { return(NULL) @@ -5445,9 +5384,9 @@ plotPCA_3D <- function(gobject, ## extract spatial network if (show_network == TRUE) { spatial_network <- get_spatialNetwork(gobject, - spat_unit = spat_unit, - name = spatial_network_name, - output = "networkDT" + spat_unit = spat_unit, + name = spatial_network_name, + output = "networkDT" ) } else { spatial_network <- NULL @@ -5456,9 +5395,9 @@ plotPCA_3D <- function(gobject, ## extract spatial grid if (show_grid == TRUE) { spatial_grid <- get_spatialGrid(gobject, - spat_unit = spat_unit, - feat_type = feat_type, - spatial_grid_name + spat_unit = spat_unit, + feat_type = feat_type, + spatial_grid_name ) } else { spatial_grid <- NULL @@ -5490,11 +5429,11 @@ plotPCA_3D <- function(gobject, message("You have selected both individual cell IDs and a group of cells") group_cell_IDs <- cell_locations_metadata[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] select_cells <- unique(c(select_cells, group_cell_IDs)) } else if (!is.null(select_cell_groups)) { select_cells <- cell_locations_metadata[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] } @@ -5504,8 +5443,8 @@ plotPCA_3D <- function(gobject, cell_locations_metadata_selected <- cell_locations_metadata[ cell_locations_metadata$cell_ID %in% select_cells] spatial_network <- spatial_network[spatial_network$to %in% - select_cells & spatial_network$from %in% - select_cells] + select_cells & spatial_network$from %in% + select_cells] # if specific cells are selected # cell_locations_metadata = cell_locations_metadata_selected @@ -5521,10 +5460,10 @@ plotPCA_3D <- function(gobject, ### set ratio ratio <- plotly_axis_scale_2D(cell_locations, - sdimx = sdimx, - sdimy = sdimy, - mode = axis_scale, - custom_ratio = custom_ratio + sdimx = sdimx, + sdimy = sdimy, + mode = axis_scale, + custom_ratio = custom_ratio ) @@ -5619,7 +5558,7 @@ plotPCA_3D <- function(gobject, x = ~sdimx, y = ~sdimy, marker = list(size = other_point_size, - color = other_cell_color), + color = other_cell_color), opacity = other_cell_alpha ) } @@ -5682,36 +5621,36 @@ plotPCA_3D <- function(gobject, #' @returns plotly object #' @keywords internal .spatPlot_3d_plotly <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - spat_loc_name = "raw", - sdimx = NULL, - sdimy = NULL, - sdimz = NULL, - spat_enr_names = NULL, - point_size = 3, - cell_color = NULL, - cell_color_code = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 0.5, - show_network = FALSE, - spatial_network_name = "spatial_network", - network_color = NULL, - network_alpha = 1, - other_cell_alpha = 0.5, - show_grid = FALSE, - spatial_grid_name = "spatial_grid", - title = "", - show_legend = TRUE, - axis_scale = c("cube", "real", "custom"), - custom_ratio = NULL, - x_ticks = NULL, - y_ticks = NULL, - z_ticks = NULL, - show_plot = FALSE) { + spat_unit = NULL, + feat_type = NULL, + spat_loc_name = "raw", + sdimx = NULL, + sdimy = NULL, + sdimz = NULL, + spat_enr_names = NULL, + point_size = 3, + cell_color = NULL, + cell_color_code = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 0.5, + show_network = FALSE, + spatial_network_name = "spatial_network", + network_color = NULL, + network_alpha = 1, + other_cell_alpha = 0.5, + show_grid = FALSE, + spatial_grid_name = "spatial_grid", + title = "", + show_legend = TRUE, + axis_scale = c("cube", "real", "custom"), + custom_ratio = NULL, + x_ticks = NULL, + y_ticks = NULL, + z_ticks = NULL, + show_plot = FALSE) { # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -5725,9 +5664,9 @@ plotPCA_3D <- function(gobject, ## get spatial cell locations cell_locations <- get_spatial_locations(gobject, - spat_unit = spat_unit, - spat_loc_name = spat_loc_name, - output = "data.table" + spat_unit = spat_unit, + spat_loc_name = spat_loc_name, + output = "data.table" ) if (is.null(cell_locations)) { return(NULL) @@ -5736,9 +5675,9 @@ plotPCA_3D <- function(gobject, ## extract spatial network if (show_network == TRUE) { spatial_network <- get_spatialNetwork(gobject, - spat_unit = spat_unit, - name = spatial_network_name, - output = "networkDT" + spat_unit = spat_unit, + name = spatial_network_name, + output = "networkDT" ) } else { spatial_network <- NULL @@ -5747,9 +5686,9 @@ plotPCA_3D <- function(gobject, ## extract spatial grid if (show_grid == TRUE) { spatial_grid <- get_spatialGrid(gobject, - spat_unit = spat_unit, - feat_type = feat_type, - spatial_grid_name + spat_unit = spat_unit, + feat_type = feat_type, + spatial_grid_name ) } else { spatial_grid <- NULL @@ -5782,11 +5721,11 @@ plotPCA_3D <- function(gobject, message("You have selected both individual cell IDs and a group of cells") group_cell_IDs <- cell_locations_metadata[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] select_cells <- unique(c(select_cells, group_cell_IDs)) } else if (!is.null(select_cell_groups)) { select_cells <- cell_locations_metadata[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] } if (!is.null(select_cells)) { @@ -5795,7 +5734,7 @@ plotPCA_3D <- function(gobject, cell_locations_metadata_selected <- cell_locations_metadata[ cell_locations_metadata$cell_ID %in% select_cells] spatial_network <- spatial_network[spatial_network$to %in% - select_cells & spatial_network$from %in% select_cells] + select_cells & spatial_network$from %in% select_cells] # if specific cells are selected # cell_locations_metadata = cell_locations_metadata_selected @@ -5811,11 +5750,11 @@ plotPCA_3D <- function(gobject, ### set ratio ratio <- plotly_axis_scale_3D(cell_locations, - sdimx = sdimx, - sdimy = sdimy, - sdimz = sdimz, - mode = axis_scale, - custom_ratio = custom_ratio + sdimx = sdimx, + sdimy = sdimy, + sdimz = sdimz, + mode = axis_scale, + custom_ratio = custom_ratio ) @@ -5949,41 +5888,41 @@ plotPCA_3D <- function(gobject, #' @param z_ticks set the number of ticks on the z-axis #' @export spatPlot3D <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - sdimx = "sdimx", - sdimy = "sdimy", - sdimz = "sdimz", - spat_enr_names = NULL, - point_size = 3, - cell_color = NULL, - cell_color_code = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 0.5, - other_cell_alpha = 0.5, - show_network = FALSE, - spatial_network_name = "Delaunay_network", - network_color = NULL, - network_alpha = 1, - show_grid = FALSE, - spatial_grid_name = "spatial_grid", - grid_color = NULL, - grid_alpha = 1, - title = "", - show_legend = TRUE, - axis_scale = c("cube", "real", "custom"), - custom_ratio = NULL, - x_ticks = NULL, - y_ticks = NULL, - z_ticks = NULL, - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "spat3D") { + spat_unit = NULL, + feat_type = NULL, + sdimx = "sdimx", + sdimy = "sdimy", + sdimz = "sdimz", + spat_enr_names = NULL, + point_size = 3, + cell_color = NULL, + cell_color_code = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 0.5, + other_cell_alpha = 0.5, + show_network = FALSE, + spatial_network_name = "Delaunay_network", + network_color = NULL, + network_alpha = 1, + show_grid = FALSE, + spatial_grid_name = "spatial_grid", + grid_color = NULL, + grid_alpha = 1, + title = "", + show_legend = TRUE, + axis_scale = c("cube", "real", "custom"), + custom_ratio = NULL, + x_ticks = NULL, + y_ticks = NULL, + z_ticks = NULL, + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "spat3D") { if (is.null(sdimz)) { message("create 2D plot") @@ -6057,8 +5996,8 @@ spatPlot3D <- function(gobject, readGiottoInstructions(gobject, param = "save_plot"), save_plot) return_plot <- ifelse(is.null(return_plot), - readGiottoInstructions(gobject, param = "return_plot"), - return_plot) + readGiottoInstructions(gobject, param = "return_plot"), + return_plot) ## print plot if (show_plot == TRUE) { @@ -6069,7 +6008,7 @@ spatPlot3D <- function(gobject, if (save_plot == TRUE) { do.call("all_plots_save_function", c(list(gobject = gobject, plot_object = pl, - default_save_name = default_save_name), save_param)) + default_save_name = default_save_name), save_param)) } ## return plot @@ -6150,57 +6089,57 @@ spatPlot3D <- function(gobject, #' @family spatial and dimension reduction visualizations #' @export spatDimPlot3D <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - plot_alignment = c("horizontal", "vertical"), - dim_reduction_to_use = "umap", - dim_reduction_name = "umap", - dim1_to_use = 1, - dim2_to_use = 2, - dim3_to_use = 3, - spat_loc_name = NULL, - sdimx = "sdimx", - sdimy = "sdimy", - sdimz = "sdimz", - spat_enr_names = NULL, - show_NN_network = FALSE, - nn_network_to_use = "sNN", - network_name = "sNN.pca", - nn_network_color = "lightgray", - nn_network_alpha = 0.5, - show_cluster_center = FALSE, - show_center_label = TRUE, - center_point_size = 4, - label_size = 16, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 1.5, - cell_color = NULL, - color_as_factor = TRUE, - cell_color_code = NULL, - dim_point_size = 3, - show_spatial_network = FALSE, - spatial_network_name = "Delaunay_network", - spatial_network_color = "lightgray", - spatial_network_alpha = 0.5, - show_spatial_grid = FALSE, - spatial_grid_name = "spatial_grid", - spatial_grid_color = NULL, - spatial_grid_alpha = 0.5, - spatial_point_size = 3, - axis_scale = c("cube", "real", "custom"), - custom_ratio = NULL, - x_ticks = NULL, - y_ticks = NULL, - z_ticks = NULL, - legend_text_size = 12, - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "spatDimPlot3D") { + spat_unit = NULL, + feat_type = NULL, + plot_alignment = c("horizontal", "vertical"), + dim_reduction_to_use = "umap", + dim_reduction_name = "umap", + dim1_to_use = 1, + dim2_to_use = 2, + dim3_to_use = 3, + spat_loc_name = NULL, + sdimx = "sdimx", + sdimy = "sdimy", + sdimz = "sdimz", + spat_enr_names = NULL, + show_NN_network = FALSE, + nn_network_to_use = "sNN", + network_name = "sNN.pca", + nn_network_color = "lightgray", + nn_network_alpha = 0.5, + show_cluster_center = FALSE, + show_center_label = TRUE, + center_point_size = 4, + label_size = 16, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 1.5, + cell_color = NULL, + color_as_factor = TRUE, + cell_color_code = NULL, + dim_point_size = 3, + show_spatial_network = FALSE, + spatial_network_name = "Delaunay_network", + spatial_network_color = "lightgray", + spatial_network_alpha = 0.5, + show_spatial_grid = FALSE, + spatial_grid_name = "spatial_grid", + spatial_grid_color = NULL, + spatial_grid_alpha = 0.5, + spatial_point_size = 3, + axis_scale = c("cube", "real", "custom"), + custom_ratio = NULL, + x_ticks = NULL, + y_ticks = NULL, + z_ticks = NULL, + legend_text_size = 12, + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "spatDimPlot3D") { # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -6244,8 +6183,8 @@ spatDimPlot3D <- function(gobject, ) annotated_DT <- merge(cell_metadata, dim_DT, by = "cell_ID") spatial_locations <- get_spatial_locations(gobject, - spat_unit = spat_unit, - spat_loc_name = spat_loc_name + spat_unit = spat_unit, + spat_loc_name = spat_loc_name ) if (is.null(spatial_locations)) { return(NULL) @@ -6297,16 +6236,16 @@ spatDimPlot3D <- function(gobject, old_dim_names <- dim_names annotated_network_DT <- merge(network_DT, dim_DT, by.x = "from", - by.y = "cell_ID") + by.y = "cell_ID") from_dim_names <- paste0("from_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = from_dim_names) + new = from_dim_names) annotated_network_DT <- merge(annotated_network_DT, dim_DT, - by.x = "to", by.y = "cell_ID") + by.x = "to", by.y = "cell_ID") to_dim_names <- paste0("to_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = to_dim_names) + new = to_dim_names) } @@ -6315,9 +6254,9 @@ spatDimPlot3D <- function(gobject, ## extract spatial network if (show_spatial_network == TRUE) { spatial_network <- get_spatialNetwork(gobject, - spat_unit = spat_unit, - name = spatial_network_name, - output = "networkDT" + spat_unit = spat_unit, + name = spatial_network_name, + output = "networkDT" ) } else { spatial_network <- NULL @@ -6327,9 +6266,9 @@ spatDimPlot3D <- function(gobject, ## extract spatial grid if (show_spatial_grid == TRUE) { spatial_grid <- get_spatialGrid(gobject, - spat_unit = spat_unit, - feat_type = feat_type, - spatial_grid_name + spat_unit = spat_unit, + feat_type = feat_type, + spatial_grid_name ) } else { spatial_grid <- NULL @@ -6340,8 +6279,8 @@ spatDimPlot3D <- function(gobject, if (is.null(cell_color_code)) { if (is.character(cell_color)) { cell_metadata <- pDataDT(gobject, - feat_type = feat_type, - spat_unit = spat_unit + feat_type = feat_type, + spat_unit = spat_unit ) if (cell_color %in% colnames(cell_metadata)) { if (color_as_factor == TRUE) { @@ -6365,30 +6304,30 @@ spatDimPlot3D <- function(gobject, message("You have selected both individual cell IDs and a group of cells") group_cell_IDs <- annotated_DT[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] select_cells <- unique(c(select_cells, group_cell_IDs)) } else if (!is.null(select_cell_groups)) { select_cells <- annotated_DT[get(cell_color) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] } if (!is.null(select_cells)) { annotated_DT_other <- annotated_DT[!annotated_DT$cell_ID %in% - select_cells] + select_cells] annotated_DT_selected <- annotated_DT[annotated_DT$cell_ID %in% - select_cells] + select_cells] if (show_NN_network == TRUE) { annotated_network_DT <- annotated_network_DT[ annotated_network_DT$to %in% select_cells & - annotated_network_DT$from %in% select_cells] + annotated_network_DT$from %in% select_cells] } if (show_spatial_network == TRUE) { spatial_network <- spatial_network[spatial_network$to %in% - select_cells & - spatial_network$from %in% - select_cells] + select_cells & + spatial_network$from %in% + select_cells] } # if specific cells are selected @@ -6488,7 +6427,7 @@ spatDimPlot3D <- function(gobject, colors = cell_color_code, legendgroup = annotated_DT_centers[[cell_color]], marker = list(size = center_point_size, symbol = "x", - symbols = "x"), + symbols = "x"), showlegend = FALSE ) } @@ -6514,7 +6453,7 @@ spatDimPlot3D <- function(gobject, x = annotated_DT_other[[dim_names[1]]], y = annotated_DT_other[[dim_names[2]]], marker = list(size = other_point_size, - color = other_cell_color), + color = other_cell_color), showlegend = FALSE ) } @@ -6523,9 +6462,9 @@ spatDimPlot3D <- function(gobject, x_name <- paste0("pca", "-", dim_names[1]) y_name <- paste0("pca", "-", dim_names[2]) x_title <- sprintf("%s explains %.02f%% of variance", - x_name, var_expl_vec[1]) + x_name, var_expl_vec[1]) y_title <- sprintf("%s explains %.02f%% of variance", - y_name, var_expl_vec[2]) + y_name, var_expl_vec[2]) } } else { x_title <- paste(dim_reduction_to_use, dim_names[1], sep = " ") @@ -6535,8 +6474,8 @@ spatDimPlot3D <- function(gobject, xaxis = list(title = x_title), yaxis = list(title = y_title), legend = list(x = 100, y = 0.5, - font = list(family = "sans-serif", - size = legend_text_size)) + font = list(family = "sans-serif", + size = legend_text_size)) ) } # 3D plot @@ -6589,7 +6528,7 @@ spatDimPlot3D <- function(gobject, y = annotated_DT_other[[dim_names[2]]], z = annotated_DT_other[[dim_names[3]]], marker = list(size = other_point_size, - color = other_cell_color), + color = other_cell_color), showlegend = FALSE ) } @@ -6667,11 +6606,11 @@ spatDimPlot3D <- function(gobject, y_name <- paste0("pca", "-", dim_names[2]) z_name <- paste0("pca", "-", dim_names[3]) x_title <- sprintf("%s explains %.02f%% of variance", - x_name, var_expl_vec[1]) + x_name, var_expl_vec[1]) y_title <- sprintf("%s explains %.02f%% of variance", - y_name, var_expl_vec[2]) + y_name, var_expl_vec[2]) z_title <- sprintf("%s explains %.02f%% of variance", - z_name, var_expl_vec[3]) + z_name, var_expl_vec[3]) } else { x_title <- paste(dim_reduction_to_use, dim_names[1], sep = " ") y_title <- paste(dim_reduction_to_use, dim_names[2], sep = " ") @@ -6789,7 +6728,7 @@ spatDimPlot3D <- function(gobject, x = annotated_DT_other[[sdimx]], y = annotated_DT_other[[sdimy]], marker = list(size = other_point_size, - color = other_cell_color), + color = other_cell_color), showlegend = FALSE ) } @@ -6797,8 +6736,8 @@ spatDimPlot3D <- function(gobject, xaxis = list(title = "X"), yaxis = list(title = "Y"), legend = list(x = 100, y = 0.5, - font = list(family = "sans-serif", - size = legend_text_size)) + font = list(family = "sans-serif", + size = legend_text_size)) ) } @@ -6808,8 +6747,8 @@ spatDimPlot3D <- function(gobject, axis_scale <- match.arg(axis_scale, c("cube", "real", "custom")) ratio <- plotly_axis_scale_3D(annotated_DT_selected, - sdimx = sdimx, sdimy = sdimy, sdimz = sdimz, - mode = axis_scale, custom_ratio = custom_ratio + sdimx = sdimx, sdimy = sdimy, sdimz = sdimz, + mode = axis_scale, custom_ratio = custom_ratio ) spl <- plotly::plot_ly(scene = "scene2") if (!is.null(cell_color)) { @@ -6852,7 +6791,7 @@ spatDimPlot3D <- function(gobject, y = annotated_DT_other[[sdimy]], z = annotated_DT_other[[sdimz]], marker = list(size = other_point_size, - color = other_cell_color), + color = other_cell_color), showlegend = FALSE ) } @@ -6893,15 +6832,15 @@ spatDimPlot3D <- function(gobject, if (is.null(dim3_to_use) & is.null(sdimz)) { if (plot_alignment == "vertical") { combo_plot <- plotly::subplot(dpl, spl, nrows = 2, - titleX = TRUE, titleY = TRUE) + titleX = TRUE, titleY = TRUE) } else { combo_plot <- plotly::subplot(dpl, spl, titleX = TRUE, - titleY = TRUE) + titleY = TRUE) } } else if (!is.null(dim3_to_use) & is.null(sdimz)) { if (plot_alignment == "vertical") { combo_plot <- plotly::subplot(dpl, spl, nrows = 2, titleX = TRUE, - titleY = TRUE) %>% + titleY = TRUE) %>% plotly::layout(scene = list( domain = list(x = c(0, 1), y = c(0, 0.5)), xaxis = list(title = x_title), @@ -6910,7 +6849,7 @@ spatDimPlot3D <- function(gobject, )) } else { combo_plot <- plotly::subplot(dpl, spl, titleX = TRUE, - titleY = TRUE) %>% + titleY = TRUE) %>% plotly::layout(scene = list( domain = list(x = c(0, 0.5), y = c(0, 1)), xaxis = list(title = x_title), @@ -6921,7 +6860,7 @@ spatDimPlot3D <- function(gobject, } else if (is.null(dim3_to_use) & !is.null(sdimz)) { if (plot_alignment == "vertical") { combo_plot <- plotly::subplot(dpl, spl, nrows = 2, titleX = TRUE, - titleY = TRUE) %>% + titleY = TRUE) %>% plotly::layout(scene2 = list( domain = list(x = c(0, 1), y = c(0.5, 1)), xaxis = list(title = "X", nticks = x_ticks), @@ -6936,7 +6875,7 @@ spatDimPlot3D <- function(gobject, )) } else { combo_plot <- plotly::subplot(dpl, spl, titleX = TRUE, - titleY = TRUE) %>% + titleY = TRUE) %>% plotly::layout(scene2 = list( domain = list(x = c(0.5, 1), y = c(0, 1)), xaxis = list(title = "X", nticks = x_ticks), @@ -6953,7 +6892,7 @@ spatDimPlot3D <- function(gobject, } else if (!is.null(dim3_to_use) & !is.null(sdimz)) { if (plot_alignment == "vertical") { combo_plot <- plotly::subplot(dpl, spl, nrows = 2, titleX = TRUE, - titleY = TRUE) %>% + titleY = TRUE) %>% plotly::layout( scene = list( domain = list(x = c(0, 1), y = c(0, 0.5)), @@ -6976,7 +6915,7 @@ spatDimPlot3D <- function(gobject, ) } else { combo_plot <- plotly::subplot(dpl, spl, titleX = TRUE, - titleY = TRUE) %>% + titleY = TRUE) %>% plotly::layout( scene = list( domain = list(x = c(0, 0.5), y = c(0, 1)), @@ -7007,8 +6946,8 @@ spatDimPlot3D <- function(gobject, readGiottoInstructions(gobject, param = "save_plot"), save_plot) return_plot <- ifelse(is.null(return_plot), - readGiottoInstructions(gobject, param = "return_plot"), - return_plot) + readGiottoInstructions(gobject, param = "return_plot"), + return_plot) ## print plot if (show_plot == TRUE) { @@ -7019,7 +6958,7 @@ spatDimPlot3D <- function(gobject, if (save_plot == TRUE) { do.call("all_plots_save_function", c(list(gobject = gobject, plot_object = combo_plot, - default_save_name = default_save_name), save_param)) + default_save_name = default_save_name), save_param)) } ## return plot @@ -7032,18 +6971,16 @@ spatDimPlot3D <- function(gobject, # ** #### -# ** gene 3D plot #### +# ** feature 3D plot #### - -#' @title spatGenePlot3D -#' @name spatGenePlot3D +#' @title spatFeatPlot3D +#' @name spatFeatPlot3D #' @description Visualize cells and gene expression according to spatial #' coordinates #' @inheritParams data_access_params #' @inheritParams plot_output_params #' @param spat_loc_name name of spatial locations to use #' @param expression_values gene expression values to use -#' @param genes deprecated #' @param feats feats to show #' @param spat_enr_names names of spatial enrichment results to include #' @@ -7073,58 +7010,50 @@ spatDimPlot3D <- function(gobject, #' @param x_ticks set the number of ticks on the x-axis #' @param y_ticks set the number of ticks on the y-axis #' @param z_ticks set the number of ticks on the z-axis -#' @details Description of parameters. +#' @param ... additional params to pass #' @family spatial gene expression visualizations #' @returns plotly #' @examples #' g <- GiottoData::loadGiottoMini("starmap") -#' spatGenePlot3D(g, feats = "Slc17a7") +#' spatFeatPlot3D(g, feats = "Slc17a7") #' #' @export -spatGenePlot3D <- function(gobject, - spat_unit = NULL, - feat_type = NULL, - spat_loc_name = "raw", - expression_values = c("normalized", "scaled", "custom"), - genes = deprecated(), - feats, - spat_enr_names = NULL, - show_network = FALSE, - network_color = NULL, - spatial_network_name = "Delaunay_network", - edge_alpha = NULL, - cluster_column = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = FALSE, - other_cell_color = "lightgrey", - other_point_size = 1, - genes_high_color = NULL, - genes_mid_color = "white", - genes_low_color = "blue", - show_grid = FALSE, - spatial_grid_name = "spatial_grid", - point_size = 2, - show_legend = TRUE, - axis_scale = c("cube", "real", "custom"), - custom_ratio = NULL, - x_ticks = NULL, - y_ticks = NULL, - z_ticks = NULL, - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "spatGenePlot3D") { - # deprecate - if (GiottoUtils::is_present(genes)) { - deprecate_warn( - "0.0.0.9000", - "GiottoVisuals::spatGenePlot3D(genes = )", - "GiottoVisuals::spatGenePlot3D(feats = )" - ) - feats <- genes - } +spatFeatPlot3D <- function(gobject, + spat_unit = NULL, + feat_type = NULL, + spat_loc_name = "raw", + expression_values = c("normalized", "scaled", "custom"), + feats, + spat_enr_names = NULL, + show_network = FALSE, + network_color = NULL, + spatial_network_name = "Delaunay_network", + edge_alpha = NULL, + cluster_column = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = FALSE, + other_cell_color = "lightgrey", + other_point_size = 1, + genes_high_color = NULL, + genes_mid_color = "white", + genes_low_color = "blue", + show_grid = FALSE, + spatial_grid_name = "spatial_grid", + point_size = 2, + show_legend = TRUE, + axis_scale = c("cube", "real", "custom"), + custom_ratio = NULL, + x_ticks = NULL, + y_ticks = NULL, + z_ticks = NULL, + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "spatFeatPlot3D", + ... +) { # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( @@ -7154,7 +7083,7 @@ spatGenePlot3D <- function(gobject, # only keep genes that are in the dataset selected_genes <- selected_genes[selected_genes %in% rownames(expr_values)] - # get selected gene expression values in data.table format + # get selected feature expression values in data.table format if (length(selected_genes) == 1) { subset_expr_data <- expr_values[rownames(expr_values) %in% selected_genes, ] @@ -7162,7 +7091,7 @@ spatGenePlot3D <- function(gobject, "selected_gene" = subset_expr_data, "cell_ID" = colnames(expr_values)) data.table::setnames(t_sub_expr_data_DT, - "selected_gene", selected_genes) + "selected_gene", selected_genes) } else { subset_expr_data <- expr_values[rownames(expr_values) %in% selected_genes, ] @@ -7188,9 +7117,9 @@ spatGenePlot3D <- function(gobject, ## extract spatial network if (show_network == TRUE) { spatial_network <- get_spatialNetwork(gobject, - spat_unit = spat_unit, - name = spatial_network_name, - output = "networkDT" + spat_unit = spat_unit, + name = spatial_network_name, + output = "networkDT" ) } else { spatial_network <- NULL @@ -7199,9 +7128,9 @@ spatGenePlot3D <- function(gobject, ## extract spatial grid if (show_grid == TRUE) { spatial_grid <- get_spatialGrid(gobject, - spat_unit = spat_unit, - feat_type = feat_type, - spatial_grid_name + spat_unit = spat_unit, + feat_type = feat_type, + spatial_grid_name ) } else { spatial_grid <- NULL @@ -7233,11 +7162,11 @@ spatGenePlot3D <- function(gobject, message("You have selected both individual cell IDs and a group of cells") group_cell_IDs <- cell_locations_metadata[get(cluster_column) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] select_cells <- unique(c(select_cells, group_cell_IDs)) } else if (!is.null(select_cell_groups)) { select_cells <- cell_locations_metadata[get(cluster_column) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] } if (!is.null(select_cells)) { @@ -7246,14 +7175,14 @@ spatGenePlot3D <- function(gobject, cell_locations_metadata_selected <- cell_locations_metadata[ cell_locations_metadata$cell_ID %in% select_cells] spatial_network <- spatial_network[spatial_network$to %in% - select_cells & spatial_network$from %in% select_cells] + select_cells & spatial_network$from %in% select_cells] # if specific cells are selected cell_locations_metadata <- cell_locations_metadata_selected } cell_locations_metadata_genes <- merge(cell_locations_metadata, - t_sub_expr_data_DT, by = "cell_ID") + t_sub_expr_data_DT, by = "cell_ID") @@ -7261,8 +7190,8 @@ spatGenePlot3D <- function(gobject, axis_scale <- match.arg(axis_scale, c("cube", "real", "custom")) ratio <- plotly_axis_scale_3D(cell_locations_metadata_genes, - sdimx = "sdimx", sdimy = "sdimy", sdimz = "sdimz", - mode = axis_scale, custom_ratio = custom_ratio + sdimx = "sdimx", sdimy = "sdimy", sdimz = "sdimz", + mode = axis_scale, custom_ratio = custom_ratio ) @@ -7304,7 +7233,7 @@ spatGenePlot3D <- function(gobject, marker = list(size = point_size), color = cell_locations_metadata_genes[[gene]], colors = c(genes_low_color, genes_mid_color, - genes_high_color[i]) + genes_high_color[i]) ) if (show_other_cells == TRUE) { @@ -7368,118 +7297,118 @@ spatGenePlot3D <- function(gobject, } else if (length(savelist) == 2) { cowplot <- plotly::subplot(savelist) %>% plotly::layout( - scene = list( - xaxis = list(title = "X", nticks = x_ticks), - yaxis = list(title = "Y", nticks = y_ticks), - zaxis = list(title = "Z", nticks = z_ticks), - aspectmode = "manual", - aspectratio = list( - x = ratio[[1]], - y = ratio[[2]], - z = ratio[[3]] - ) - ), - scene2 = list( - xaxis = list(title = "X", nticks = x_ticks), - yaxis = list(title = "Y", nticks = y_ticks), - zaxis = list(title = "Z", nticks = z_ticks), - aspectmode = "manual", - aspectratio = list( - x = ratio[[1]], - y = ratio[[2]], - z = ratio[[3]] - ) - ), - # annotations = annotations, - legend = list(x = 100, y = 0) - ) + scene = list( + xaxis = list(title = "X", nticks = x_ticks), + yaxis = list(title = "Y", nticks = y_ticks), + zaxis = list(title = "Z", nticks = z_ticks), + aspectmode = "manual", + aspectratio = list( + x = ratio[[1]], + y = ratio[[2]], + z = ratio[[3]] + ) + ), + scene2 = list( + xaxis = list(title = "X", nticks = x_ticks), + yaxis = list(title = "Y", nticks = y_ticks), + zaxis = list(title = "Z", nticks = z_ticks), + aspectmode = "manual", + aspectratio = list( + x = ratio[[1]], + y = ratio[[2]], + z = ratio[[3]] + ) + ), + # annotations = annotations, + legend = list(x = 100, y = 0) + ) } else if (length(savelist) == 3) { cowplot <- plotly::subplot(savelist) %>% plotly::layout( - scene = list( - xaxis = list(title = "X", nticks = x_ticks), - yaxis = list(title = "Y", nticks = y_ticks), - zaxis = list(title = "Z", nticks = z_ticks), - aspectmode = "manual", - aspectratio = list( - x = ratio[[1]], - y = ratio[[2]], - z = ratio[[3]] - ) - ), - scene2 = list( - xaxis = list(title = "X", nticks = x_ticks), - yaxis = list(title = "Y", nticks = y_ticks), - zaxis = list(title = "Z", nticks = z_ticks), - aspectmode = "manual", - aspectratio = list( - x = ratio[[1]], - y = ratio[[2]], - z = ratio[[3]] - ) - ), - scene3 = list( - xaxis = list(title = "X", nticks = x_ticks), - yaxis = list(title = "Y", nticks = y_ticks), - zaxis = list(title = "Z", nticks = z_ticks), - aspectmode = "manual", - aspectratio = list( - x = ratio[[1]], - y = ratio[[2]], - z = ratio[[3]] - ) - ), - legend = list(x = 100, y = 0) - ) + scene = list( + xaxis = list(title = "X", nticks = x_ticks), + yaxis = list(title = "Y", nticks = y_ticks), + zaxis = list(title = "Z", nticks = z_ticks), + aspectmode = "manual", + aspectratio = list( + x = ratio[[1]], + y = ratio[[2]], + z = ratio[[3]] + ) + ), + scene2 = list( + xaxis = list(title = "X", nticks = x_ticks), + yaxis = list(title = "Y", nticks = y_ticks), + zaxis = list(title = "Z", nticks = z_ticks), + aspectmode = "manual", + aspectratio = list( + x = ratio[[1]], + y = ratio[[2]], + z = ratio[[3]] + ) + ), + scene3 = list( + xaxis = list(title = "X", nticks = x_ticks), + yaxis = list(title = "Y", nticks = y_ticks), + zaxis = list(title = "Z", nticks = z_ticks), + aspectmode = "manual", + aspectratio = list( + x = ratio[[1]], + y = ratio[[2]], + z = ratio[[3]] + ) + ), + legend = list(x = 100, y = 0) + ) } else if (length(savelist) == 4) { cowplot <- plotly::subplot(savelist) %>% plotly::layout( - scene = list( - xaxis = list(title = "X", nticks = x_ticks), - yaxis = list(title = "Y", nticks = y_ticks), - zaxis = list(title = "Z", nticks = z_ticks), - aspectmode = "manual", - aspectratio = list( - x = ratio[[1]], - y = ratio[[2]], - z = ratio[[3]] - ) - ), - scene2 = list( - xaxis = list(title = "X", nticks = x_ticks), - yaxis = list(title = "Y", nticks = y_ticks), - zaxis = list(title = "Z", nticks = z_ticks), - aspectmode = "manual", - aspectratio = list( - x = ratio[[1]], - y = ratio[[2]], - z = ratio[[3]] - ) - ), - scene3 = list( - xaxis = list(title = "X", nticks = x_ticks), - yaxis = list(title = "Y", nticks = y_ticks), - zaxis = list(title = "Z", nticks = z_ticks), - aspectmode = "manual", - aspectratio = list( - x = ratio[[1]], - y = ratio[[2]], - z = ratio[[3]] - ) - ), - scene4 = list( - xaxis = list(title = "X", nticks = x_ticks), - yaxis = list(title = "Y", nticks = y_ticks), - zaxis = list(title = "Z", nticks = z_ticks), - aspectmode = "manual", - aspectratio = list( - x = ratio[[1]], - y = ratio[[2]], - z = ratio[[3]] - ) - ), - legend = list(x = 100, y = 0) - ) + scene = list( + xaxis = list(title = "X", nticks = x_ticks), + yaxis = list(title = "Y", nticks = y_ticks), + zaxis = list(title = "Z", nticks = z_ticks), + aspectmode = "manual", + aspectratio = list( + x = ratio[[1]], + y = ratio[[2]], + z = ratio[[3]] + ) + ), + scene2 = list( + xaxis = list(title = "X", nticks = x_ticks), + yaxis = list(title = "Y", nticks = y_ticks), + zaxis = list(title = "Z", nticks = z_ticks), + aspectmode = "manual", + aspectratio = list( + x = ratio[[1]], + y = ratio[[2]], + z = ratio[[3]] + ) + ), + scene3 = list( + xaxis = list(title = "X", nticks = x_ticks), + yaxis = list(title = "Y", nticks = y_ticks), + zaxis = list(title = "Z", nticks = z_ticks), + aspectmode = "manual", + aspectratio = list( + x = ratio[[1]], + y = ratio[[2]], + z = ratio[[3]] + ) + ), + scene4 = list( + xaxis = list(title = "X", nticks = x_ticks), + yaxis = list(title = "Y", nticks = y_ticks), + zaxis = list(title = "Z", nticks = z_ticks), + aspectmode = "manual", + aspectratio = list( + x = ratio[[1]], + y = ratio[[2]], + z = ratio[[3]] + ) + ), + legend = list(x = 100, y = 0) + ) } @@ -7490,8 +7419,8 @@ spatGenePlot3D <- function(gobject, readGiottoInstructions(gobject, param = "save_plot"), save_plot) return_plot <- ifelse(is.null(return_plot), - readGiottoInstructions(gobject, param = "return_plot"), - return_plot) + readGiottoInstructions(gobject, param = "return_plot"), + return_plot) ## print plot if (show_plot == TRUE) { print(cowplot) @@ -7501,7 +7430,7 @@ spatGenePlot3D <- function(gobject, if (save_plot == TRUE) { do.call("all_plots_save_function", c(list(gobject = gobject, plot_object = cowplot, - default_save_name = default_save_name), save_param)) + default_save_name = default_save_name), save_param)) } ## return plot @@ -7510,10 +7439,20 @@ spatGenePlot3D <- function(gobject, } } +#' @describeIn spatFeatPlot3D deprecated +#' @export +spatGenePlot3D <- function(...) { + deprecate_warn( + when = "0.2.0", + what = "spatGenePlot3D()", + with = "spatFeatPlot3D()" + ) + spatFeatPlot3D(...) +} -#' @title dimGenePlot3D -#' @name dimGenePlot3D +#' @title dimFeatPlot3D +#' @name dimFeatPlot3D #' @description Visualize cells and gene expression according to #' dimension reduction coordinates #' @inheritParams data_access_params @@ -7552,40 +7491,40 @@ spatGenePlot3D <- function(gobject, #' @returns plotly #' @examples #' g <- GiottoData::loadGiottoMini("starmap") -#' dimGenePlot3D(g, genes = "Slc17a7") +#' dimFeatPlot3D(g, genes = "Slc17a7") #' #' @export -dimGenePlot3D <- function(gobject, - feat_type = NULL, - spat_unit = NULL, - expression_values = c("normalized", "scaled", "custom"), - genes = NULL, - dim_reduction_to_use = "umap", - dim_reduction_name = "umap", - dim1_to_use = 1, - dim2_to_use = 2, - dim3_to_use = 3, - show_NN_network = FALSE, - nn_network_to_use = "sNN", - network_name = "sNN.pca", - network_color = "lightgray", - cluster_column = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 1, - edge_alpha = NULL, - point_size = 2, - genes_high_color = NULL, - genes_mid_color = "white", - genes_low_color = "blue", - show_legend = TRUE, - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "dimGenePlot3D") { +dimFeatPlot3D <- function(gobject, + feat_type = NULL, + spat_unit = NULL, + expression_values = c("normalized", "scaled", "custom"), + genes = NULL, + dim_reduction_to_use = "umap", + dim_reduction_name = "umap", + dim1_to_use = 1, + dim2_to_use = 2, + dim3_to_use = 3, + show_NN_network = FALSE, + nn_network_to_use = "sNN", + network_name = "sNN.pca", + network_color = "lightgray", + cluster_column = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 1, + edge_alpha = NULL, + point_size = 2, + genes_high_color = NULL, + genes_mid_color = "white", + genes_low_color = "blue", + show_legend = TRUE, + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "dimFeatPlot3D") { # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -7636,10 +7575,10 @@ dimGenePlot3D <- function(gobject, ## dimension reduction ## dim_dfr <- get_dimReduction(gobject, - reduction = "cells", - reduction_method = dim_reduction_to_use, - name = dim_reduction_name, - output = "data.table" + reduction = "cells", + reduction_method = dim_reduction_to_use, + name = dim_reduction_name, + output = "data.table" ) dim_dfr <- dim_dfr[, c(dim1_to_use, dim2_to_use, dim3_to_use)] dim_names <- colnames(dim_dfr) @@ -7648,8 +7587,8 @@ dimGenePlot3D <- function(gobject, ## annotated cell metadata cell_metadata <- pDataDT(gobject, - feat_type = feat_type, - spat_unit = spat_unit + feat_type = feat_type, + spat_unit = spat_unit ) annotated_DT <- merge(cell_metadata, dim_DT, by = "cell_ID") @@ -7676,13 +7615,13 @@ dimGenePlot3D <- function(gobject, network_DT, dim_DT, by.x = "from", by.y = "cell_ID") from_dim_names <- paste0("from_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = from_dim_names) + new = from_dim_names) annotated_network_DT <- merge( annotated_network_DT, dim_DT, by.x = "to", by.y = "cell_ID") to_dim_names <- paste0("to_", old_dim_names) data.table::setnames(annotated_network_DT, old = old_dim_names, - new = to_dim_names) + new = to_dim_names) } @@ -7695,11 +7634,11 @@ dimGenePlot3D <- function(gobject, message("You have selected both individual cell IDs and a group of cells") group_cell_IDs <- annotated_DT[get(cluster_column) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] select_cells <- unique(c(select_cells, group_cell_IDs)) } else if (!is.null(select_cell_groups)) { select_cells <- annotated_DT[get(cluster_column) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] } if (!is.null(select_cells)) { @@ -7710,8 +7649,8 @@ dimGenePlot3D <- function(gobject, if (show_NN_network == TRUE) { annotated_network_DT <- annotated_network_DT[ - annotated_network_DT$to %in% select_cells & - annotated_network_DT$from %in% select_cells] + annotated_network_DT$to %in% select_cells & + annotated_network_DT$from %in% select_cells] } # if specific cells are selected @@ -7857,32 +7796,32 @@ dimGenePlot3D <- function(gobject, } else if (length(savelist) == 4) { cowplot <- plotly::subplot(savelist) %>% plotly::layout( - scene = list( - domain = list(x = c(0, 0.5), y = c(0, 0.5)), - xaxis = list(title = titleX), - yaxis = list(title = titleY), - zaxis = list(title = titleZ) - ), - scene2 = list( - domain = list(x = c(0.5, 1), y = c(0, 0.5)), - xaxis = list(title = titleX), - yaxis = list(title = titleY), - zaxis = list(title = titleZ) - ), - scene3 = list( - domain = list(x = c(0, 0.5), y = c(0.5, 1)), - xaxis = list(title = titleX), - yaxis = list(title = titleY), - zaxis = list(title = titleZ) - ), - scene4 = list( - domain = list(x = c(0.5, 1), y = c(0.5, 1)), - xaxis = list(title = titleX), - yaxis = list(title = titleY), - zaxis = list(title = titleZ) - ), - legend = list(x = 100, y = 0) - ) + scene = list( + domain = list(x = c(0, 0.5), y = c(0, 0.5)), + xaxis = list(title = titleX), + yaxis = list(title = titleY), + zaxis = list(title = titleZ) + ), + scene2 = list( + domain = list(x = c(0.5, 1), y = c(0, 0.5)), + xaxis = list(title = titleX), + yaxis = list(title = titleY), + zaxis = list(title = titleZ) + ), + scene3 = list( + domain = list(x = c(0, 0.5), y = c(0.5, 1)), + xaxis = list(title = titleX), + yaxis = list(title = titleY), + zaxis = list(title = titleZ) + ), + scene4 = list( + domain = list(x = c(0.5, 1), y = c(0.5, 1)), + xaxis = list(title = titleX), + yaxis = list(title = titleY), + zaxis = list(title = titleZ) + ), + legend = list(x = 100, y = 0) + ) } show_plot <- ifelse(is.null(show_plot), @@ -7892,8 +7831,8 @@ dimGenePlot3D <- function(gobject, readGiottoInstructions(gobject, param = "save_plot"), save_plot) return_plot <- ifelse(is.null(return_plot), - readGiottoInstructions(gobject, param = "return_plot"), - return_plot) + readGiottoInstructions(gobject, param = "return_plot"), + return_plot) ## print plot if (show_plot == TRUE) { print(cowplot) @@ -7903,7 +7842,7 @@ dimGenePlot3D <- function(gobject, if (save_plot == TRUE) { do.call("all_plots_save_function", c(list(gobject = gobject, plot_object = cowplot, - default_save_name = default_save_name), save_param)) + default_save_name = default_save_name), save_param)) } ## return plot @@ -7912,11 +7851,22 @@ dimGenePlot3D <- function(gobject, } } +#' @describeIn dimFeatPlot3D deprecated +#' @param ... additional params to pass +#' @export +dimGenePlot3D <- function(...) { + deprecate_warn( + when = "0.2.0", + what = "dimGenePlot3D()", + with = "dimFeatPlot3D()" + ) + dimFeatPlot3D(...) +} -#' @title spatDimGenePlot3D -#' @name spatDimGenePlot3D +#' @title spatDimFeatPlot3D +#' @name spatDimFeatPlot3D #' @description Visualize cells according to spatial AND dimension #' reduction coordinates in ggplot mode #' @inheritParams data_access_params @@ -7977,60 +7927,60 @@ dimGenePlot3D <- function(gobject, #' @returns plotly #' @examples #' g <- GiottoData::loadGiottoMini("starmap") -#' spatDimGenePlot3D(g, genes = "Slc17a7") +#' spatDimFeatPlot3D(g, genes = "Slc17a7") #' #' @export -spatDimGenePlot3D <- function(gobject, - feat_type = NULL, - spat_unit = NULL, - spat_loc_name = NULL, - expression_values = c("normalized", "scaled", "custom"), - plot_alignment = c("horizontal", "vertical"), - dim_reduction_to_use = "umap", - dim_reduction_name = "umap", - dim1_to_use = 1, - dim2_to_use = 2, - dim3_to_use = NULL, - sdimx = "sdimx", - sdimy = "sdimy", - sdimz = "sdimz", - genes, - cluster_column = NULL, - select_cell_groups = NULL, - select_cells = NULL, - show_other_cells = TRUE, - other_cell_color = "lightgrey", - other_point_size = 1.5, - show_NN_network = FALSE, - nn_network_to_use = "sNN", - nn_network_color = "lightgrey", - nn_network_alpha = 0.5, - network_name = "sNN.pca", - label_size = 16, - genes_low_color = "blue", - genes_mid_color = "white", - genes_high_color = "red", - dim_point_size = 3, - show_spatial_network = FALSE, - spatial_network_name = "Delaunay_network", - spatial_network_color = "lightgray", - spatial_network_alpha = 0.5, - show_spatial_grid = FALSE, - spatial_grid_name = "spatial_grid", - spatial_grid_color = NULL, - spatial_grid_alpha = 0.5, - spatial_point_size = 3, - legend_text_size = 12, - axis_scale = c("cube", "real", "custom"), - custom_ratio = NULL, - x_ticks = NULL, - y_ticks = NULL, - z_ticks = NULL, - show_plot = NULL, - return_plot = NULL, - save_plot = NULL, - save_param = list(), - default_save_name = "spatDimGenePlot3D") { +spatDimFeatPlot3D <- function(gobject, + feat_type = NULL, + spat_unit = NULL, + spat_loc_name = NULL, + expression_values = c("normalized", "scaled", "custom"), + plot_alignment = c("horizontal", "vertical"), + dim_reduction_to_use = "umap", + dim_reduction_name = "umap", + dim1_to_use = 1, + dim2_to_use = 2, + dim3_to_use = NULL, + sdimx = "sdimx", + sdimy = "sdimy", + sdimz = "sdimz", + genes, + cluster_column = NULL, + select_cell_groups = NULL, + select_cells = NULL, + show_other_cells = TRUE, + other_cell_color = "lightgrey", + other_point_size = 1.5, + show_NN_network = FALSE, + nn_network_to_use = "sNN", + nn_network_color = "lightgrey", + nn_network_alpha = 0.5, + network_name = "sNN.pca", + label_size = 16, + genes_low_color = "blue", + genes_mid_color = "white", + genes_high_color = "red", + dim_point_size = 3, + show_spatial_network = FALSE, + spatial_network_name = "Delaunay_network", + spatial_network_color = "lightgray", + spatial_network_alpha = 0.5, + show_spatial_grid = FALSE, + spatial_grid_name = "spatial_grid", + spatial_grid_color = NULL, + spatial_grid_alpha = 0.5, + spatial_point_size = 3, + legend_text_size = 12, + axis_scale = c("cube", "real", "custom"), + custom_ratio = NULL, + x_ticks = NULL, + y_ticks = NULL, + z_ticks = NULL, + show_plot = NULL, + return_plot = NULL, + save_plot = NULL, + save_param = list(), + default_save_name = "spatDimFeatPlot3D") { # Set feat_type and spat_unit spat_unit <- set_default_spat_unit( gobject = gobject, @@ -8075,10 +8025,10 @@ spatDimGenePlot3D <- function(gobject, ## dimension reduction ## dim_dfr <- get_dimReduction(gobject, - reduction = "cells", - reduction_method = dim_reduction_to_use, - name = dim_reduction_name, - output = "data.table" + reduction = "cells", + reduction_method = dim_reduction_to_use, + name = dim_reduction_name, + output = "data.table" ) dim_dfr <- dim_dfr[, c(dim1_to_use, dim2_to_use, dim3_to_use)] dim_names <- colnames(dim_dfr) @@ -8088,8 +8038,8 @@ spatDimGenePlot3D <- function(gobject, ## annotated cell metadata cell_metadata <- pDataDT(gobject, - feat_type = feat_type, - spat_unit = spat_unit + feat_type = feat_type, + spat_unit = spat_unit ) cell_locations <- get_spatial_locations( gobject = gobject, @@ -8130,7 +8080,7 @@ spatDimGenePlot3D <- function(gobject, annotated_network_DT, old = old_dim_names, new = from_dim_names) annotated_network_DT <- merge(annotated_network_DT, dim_DT, - by.x = "to", by.y = "cell_ID") + by.x = "to", by.y = "cell_ID") to_dim_names <- paste0("to_", old_dim_names) data.table::setnames( annotated_network_DT, old = old_dim_names, new = to_dim_names) @@ -8140,9 +8090,9 @@ spatDimGenePlot3D <- function(gobject, ## extract spatial network if (show_spatial_network == TRUE) { spatial_network <- get_spatialNetwork(gobject, - spat_unit = spat_unit, - name = spatial_network_name, - output = "networkDT" + spat_unit = spat_unit, + name = spatial_network_name, + output = "networkDT" ) } else { spatial_network <- NULL @@ -8151,9 +8101,9 @@ spatDimGenePlot3D <- function(gobject, ## extract spatial grid if (show_spatial_grid == TRUE) { spatial_grid <- get_spatialGrid(gobject, - spat_unit = spat_unit, - feat_type = feat_type, - spatial_grid_name + spat_unit = spat_unit, + feat_type = feat_type, + spatial_grid_name ) } else { spatial_grid <- NULL @@ -8169,11 +8119,11 @@ spatDimGenePlot3D <- function(gobject, message("You have selected both individual cell IDs and a group of cells") group_cell_IDs <- annotated_DT[get(cluster_column) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] select_cells <- unique(c(select_cells, group_cell_IDs)) } else if (!is.null(select_cell_groups)) { select_cells <- annotated_DT[get(cluster_column) %in% - select_cell_groups][["cell_ID"]] + select_cell_groups][["cell_ID"]] } if (!is.null(select_cells)) { @@ -8268,8 +8218,8 @@ spatDimGenePlot3D <- function(gobject, xaxis = list(title = x_title), yaxis = list(title = y_title), legend = list(x = 100, y = 0.5, - font = list(family = "sans-serif", - size = legend_text_size)) + font = list(family = "sans-serif", + size = legend_text_size)) ) } # 3D plot @@ -8294,7 +8244,7 @@ spatDimGenePlot3D <- function(gobject, y = annotated_DT_other[[dim_names[2]]], z = annotated_DT_other[[dim_names[3]]], marker = list(size = other_point_size, - color = other_cell_color), + color = other_cell_color), showlegend = FALSE ) } @@ -8428,8 +8378,8 @@ spatDimGenePlot3D <- function(gobject, xaxis = list(title = "X"), yaxis = list(title = "Y"), legend = list(x = 100, y = 0.5, - font = list(family = "sans-serif", - size = legend_text_size)) + font = list(family = "sans-serif", + size = legend_text_size)) ) } @@ -8438,8 +8388,8 @@ spatDimGenePlot3D <- function(gobject, else { axis_scale <- match.arg(axis_scale, c("cube", "real", "custom")) ratio <- plotly_axis_scale_3D(annotated_DT, - sdimx = sdimx, sdimy = sdimy, sdimz = sdimz, - mode = axis_scale, custom_ratio = custom_ratio + sdimx = sdimx, sdimy = sdimy, sdimz = sdimz, + mode = axis_scale, custom_ratio = custom_ratio ) @@ -8622,8 +8572,8 @@ spatDimGenePlot3D <- function(gobject, readGiottoInstructions(gobject, param = "save_plot"), save_plot) return_plot <- ifelse(is.null(return_plot), - readGiottoInstructions(gobject, param = "return_plot"), - return_plot) + readGiottoInstructions(gobject, param = "return_plot"), + return_plot) ## print plot if (show_plot == TRUE) { @@ -8634,7 +8584,7 @@ spatDimGenePlot3D <- function(gobject, if (save_plot == TRUE) { do.call("all_plots_save_function", c(list(gobject = gobject, plot_object = combo_plot, - default_save_name = default_save_name), save_param)) + default_save_name = default_save_name), save_param)) } ## return plot @@ -8642,3 +8592,18 @@ spatDimGenePlot3D <- function(gobject, return(combo_plot) } } + +#' @describeIn spatDimFeatPlot3D deprecated +#' @param ... additional params to pass +#' @export +spatDimGenePlot3D <- function(...) { + deprecate_warn( + when = "0.2.0", + what = "spatDimGenePlot3D()", + with = "spatDimFeatPlot3D()" + ) + spatDimFeatPlot3D(...) +} + + + diff --git a/R/vis_spatial_in_situ.R b/R/vis_spatial_in_situ.R index f85029d..c927084 100644 --- a/R/vis_spatial_in_situ.R +++ b/R/vis_spatial_in_situ.R @@ -12,6 +12,7 @@ #' @inheritParams plot_poly_params #' @inheritParams plot_image_params #' @inheritParams plot_spatenr_params +#' @param largeImage_name deprecated #' @param spat_loc_name name of spatial locations #' @param feats named list of features to plot #' @param feat_type feature types of the feats @@ -33,6 +34,7 @@ #' @param plot_method method to plot points #' @param plot_last which layer to show on top of plot, #' polygons (default) or points. +#' @param theme_param list of additional params passed to `ggplot2::theme()` #' @param verbose be verbose #' @returns ggplot #' @examples @@ -88,7 +90,7 @@ spatInSituPlotPoints <- function(gobject, polygon_fill_gradient_style = c("divergent", "sequential"), polygon_fill_as_factor = NULL, polygon_fill_code = NULL, - polygon_alpha = 0.5, + polygon_alpha = NULL, polygon_line_size = 0.4, axis_text = 8, axis_title = 8, @@ -98,6 +100,7 @@ spatInSituPlotPoints <- function(gobject, show_legend = TRUE, plot_method = c("ggplot", "scattermore", "scattermost"), plot_last = c("polygons", "points"), + theme_param = list(), show_plot = NULL, return_plot = NULL, save_plot = NULL, @@ -117,12 +120,27 @@ spatInSituPlotPoints <- function(gobject, } } - if (is.null(feats)) { + send_warn <- getOption("giotto.warn_sispp_feats", TRUE) + if (is.null(feats) && send_warn) { warning(wrap_txt( "You need to select features (feats) and modify feature types (feat_type) if you want to show individual features - (e.g. transcripts)" + (e.g. transcripts) + This warning is shown once per session" )) + options("giotto.warn_sispp_feats" = FALSE) + } + + # deprecation message + if (!is.null(largeImage_name)) { + deprecate_warn( + when = "0.2.0", + what = "spatInSituPlotPoints(largeImage_name)", + details = c( + "Use `image_name` argument instead for all images to plot." + ) + ) + image_name <- c(image_name, largeImage_name) } # check valid input @@ -130,18 +148,12 @@ spatInSituPlotPoints <- function(gobject, ## giotto image ## if (isTRUE(show_image)) { - gimage <- select_gimage( + + # get 1 or more images + gimage <- getGiottoImage( gobject = gobject, - gimage = gimage, - image_name = image_name, - largeImage_name = largeImage_name, - spat_unit = spat_unit, - spat_loc_name = spat_loc_name, - feat_type = feat_type, - polygon_feat_type = polygon_feat_type + name = image_name ) - - if (isTRUE(verbose)) wrap_msg("select image done") } # start plotting @@ -157,9 +169,7 @@ spatInSituPlotPoints <- function(gobject, feat_type = feat_type, spat_loc_name = spat_loc_name, polygon_feat_type = polygon_feat_type, - gimage = gimage, - sdimx = "sdimx", - sdimy = "sdimy" + gimage = gimage ) if (isTRUE(verbose)) wrap_msg("plot image layer done") @@ -218,6 +228,13 @@ spatInSituPlotPoints <- function(gobject, ## 2. plot polygons/morphology second/last if (isTRUE(show_polygon)) { + + if (isTRUE(show_image)) { + polygon_alpha <- polygon_alpha %null% 0.5 + } else { + polygon_alpha <- polygon_alpha %null% 1 + } + # Set feat_type and spat_unit polygon_feat_type <- set_default_spat_unit( gobject = gobject, @@ -229,12 +246,6 @@ spatInSituPlotPoints <- function(gobject, feat_type = feat_type ) - # feat_type = set_default_feat_type(gobject = gobject, - # feat_type = feat_type) - # if(is.null(polygon_feat_type)) { - # polygon_feat_type = gobject@expression_feat[[1]] - # } - polygon_combo <- combineCellData( gobject = gobject, spat_loc_name = spat_loc_name, @@ -254,8 +265,6 @@ spatInSituPlotPoints <- function(gobject, instrs = instructions(gobject), polygon_dt = polygon_dt, polygon_grouping = "poly_ID", - sdimx = sdimx, - sdimy = sdimy, fill = polygon_fill, poly_fill_gradient = polygon_fill_gradient, fill_gradient_midpoint = polygon_fill_gradient_midpoint, @@ -307,8 +316,6 @@ spatInSituPlotPoints <- function(gobject, instrs = instructions(gobject), polygon_dt = polygon_dt, polygon_grouping = "poly_ID", - sdimx = sdimx, - sdimy = sdimy, fill = polygon_fill, poly_fill_gradient = polygon_fill_gradient, fill_gradient_midpoint = polygon_fill_gradient_midpoint, @@ -374,15 +381,14 @@ spatInSituPlotPoints <- function(gobject, } ## 3. adjust theme settings - plot <- plot + ggplot2::theme( - plot.title = ggplot2::element_text(hjust = 0.5), - legend.title = ggplot2::element_blank(), - legend.text = ggplot2::element_text(size = legend_text), - axis.title = ggplot2::element_text(size = axis_title), - axis.text = ggplot2::element_text(size = axis_text), - panel.grid = ggplot2::element_blank(), - panel.background = ggplot2::element_rect(fill = background_color) + gg_theme_args <- c( + theme_param, + legend_text = legend_text, + axis_title = axis_title, + axis_text = axis_text, + background_color = background_color ) + plot <- plot + do.call(.gg_theme, args = gg_theme_args) if (!is.null(coord_fix_ratio)) { @@ -477,8 +483,6 @@ spatInSituPlotPoints <- function(gobject, instrs = instructions(gobject), polygon_dt, polygon_grouping = "poly_ID", - sdimx = sdimx, - sdimy = sdimy, fill = polygon_fill, fill_as_factor = polygon_fill_as_factor, color = polygon_color, @@ -769,8 +773,6 @@ spatInSituPlotHex <- function( instrs = instructions(gobject), polygon_dt, polygon_grouping = "poly_ID", - sdimx = sdimx, - sdimy = sdimy, fill = polygon_fill, fill_as_factor = polygon_fill_as_factor, color = polygon_color, diff --git a/README.Rmd b/README.Rmd index 90bcd08..ebc57f1 100644 --- a/README.Rmd +++ b/README.Rmd @@ -50,14 +50,12 @@ pak::pak("drieslab/GiottoVisuals") - `plot_dendrogram.R` - dendrograms `vis_` - specific plotting functions organized by topic -- `vis_hvf.R` - highly variable features plots -- `vis_pc.R` - principle components plots -- `vis_spatial.R` - -- `vis_spatial_in_situ.R` - +- `vis_spatial.R` - plotting with aggregated information +- `vis_spatial_in_situ.R` - plotting with raw points and polygons information `gg_` - ggplot2 related - `gg_info_layers.R` - modular internals for plotting the different layers of information -- `` +- `gg_annotation_raster.R` - image pipeline Other: - `color_palettes.R` - color palettes and factory functions diff --git a/man/auto_image_resample.Rd b/man/auto_image_resample.Rd new file mode 100644 index 0000000..6ecf76e --- /dev/null +++ b/man/auto_image_resample.Rd @@ -0,0 +1,95 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/gg_annotation_raster.R +\name{auto_image_resample} +\alias{auto_image_resample} +\alias{.auto_resample_gimage} +\title{Optimized image resampling} +\usage{ +.auto_resample_gimage( + img, + plot_ext, + img_border = 0.125, + flex_resample = TRUE, + max_sample = getOption("giotto.plot_img_max_sample", 5e+05), + max_crop = getOption("giotto.plot_img_max_crop", 1e+08), + max_resample_scale = getOption("giotto.plot_img_max_resample_scale", 100) +) +} +\arguments{ +\item{img}{giotto image to plot} + +\item{plot_ext}{extent of plot (required)} + +\item{img_border}{if not 0 or FALSE, expand plot_ext by this percentage on +each side before applying crop on image. See details} + +\item{flex_resample}{logical. Default = TRUE. Forces usage of method A when +FALSE.} + +\item{max_sample}{numeric. Default = 5e5. Maximum n values to sample from the +image. If larger than \code{max_crop}, will override \code{max_crop.} +Globally settable with the option "giotto.plot_img_max_sample"} + +\item{max_crop}{numeric. Default = 1e8. Maximum crop size (px area) allowed +for \strong{method A} before switching to \strong{method B} +(see description). +Globally settable with option "giotto.plot_img_max_crop"} + +\item{max_resample_scale}{numeric. Default = 100. Maximum scalefactor allowed +to be applied on \code{max_sample} in order to oversample when compensating +for decreased resolution when cropping after sampling. Globally settable with +option "giotto.plot_img_max_resample_scale".} +} +\value{ +a giotto image cropped and resampled properly for plotting +} +\description{ +Downsample terra-based images for plotting. Uses +\code{\link[terra]{spatSample}} to load onlya portion of the original image, +speeding up plotting and lowering memory footprint. + +Default behavior of \code{spatSample} is to crop if only a smaller ROI is +needed for plotting followed by the sampling process in order to reduce +wasted sampling by focusing the sample space. For very large ROIs, this +crop can be time intensive and require writing to disk. + +This function examines the ROI dimensions as defined through the limits of +the spatial locations to be plotted, and decides between the following two +methods in order to avoid this issue: +\itemize{ +\item{\strong{Method A.} First crop original image, then sample +\code{max_sample} (default = 5e5) values to generate final image. Intended +for smaller ROIs. Force usage of this method by setting +\code{flex_resample = FALSE}} +\item{\strong{Method B.} First oversample, then crop. Intended for larger +ROIs. Base sample size is \code{max_sample}, which is then multiplied by a +scale factor >1 that increases the smaller the ROI is and is defined by: +original dimensions/crop dimensions where the larger ratio between x +and y dims is chosen. Scale factor is capped by +\code{max_resample_scale}} +} +Control points for this function are set by \code{max_crop} which decides +the max ROI area after which switchover to method B happens in order to +avoid laborious crops and \code{max_resample_scale} which determines the +maximum scale factor for number of values to sample. Both values can be +adjusted depending on system resources. Additionally, \code{flex_resample} +determines if this switching behavior happens. +When set to \code{FALSE}, only method A is used. +} +\details{ +\strong{img_border} +expand ext to use for plotting the image. This makes it so that the image +is not cut off sharply at the edge of the plot extent. Needed since plots +often define extent by centroids, and polygons may hang over the edge of the +extent. +} +\examples{ +\dontrun{ +img <- GiottoData::loadSubObjectMini("giottoLargeImage") +.auto_resample_gimage(img) +} +} +\seealso{ +\code{\link[terra]{spatSample}} +} +\keyword{internal} diff --git a/man/dimGenePlot3D.Rd b/man/dimFeatPlot3D.Rd similarity index 90% rename from man/dimGenePlot3D.Rd rename to man/dimFeatPlot3D.Rd index 6952833..5e7fc73 100644 --- a/man/dimGenePlot3D.Rd +++ b/man/dimFeatPlot3D.Rd @@ -1,10 +1,11 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/vis_spatial.R -\name{dimGenePlot3D} +\name{dimFeatPlot3D} +\alias{dimFeatPlot3D} \alias{dimGenePlot3D} -\title{dimGenePlot3D} +\title{dimFeatPlot3D} \usage{ -dimGenePlot3D( +dimFeatPlot3D( gobject, feat_type = NULL, spat_unit = NULL, @@ -35,8 +36,10 @@ dimGenePlot3D( return_plot = NULL, save_plot = NULL, save_param = list(), - default_save_name = "dimGenePlot3D" + default_save_name = "dimFeatPlot3D" ) + +dimGenePlot3D(...) } \arguments{ \item{gobject}{giotto object} @@ -103,6 +106,8 @@ see \code{\link{showSaveParameters}}} \item{default_save_name}{default save name for saving, don't change, change save_name in save_param} + +\item{...}{additional params to pass} } \value{ plotly @@ -114,9 +119,14 @@ dimension reduction coordinates \details{ Description of parameters. } +\section{Functions}{ +\itemize{ +\item \code{dimGenePlot3D()}: deprecated + +}} \examples{ g <- GiottoData::loadGiottoMini("starmap") -dimGenePlot3D(g, genes = "Slc17a7") +dimFeatPlot3D(g, genes = "Slc17a7") } \concept{dimension reduction gene expression visualizations} diff --git a/man/dot-spatPlot2D_single.Rd b/man/dot-spatPlot2D_single.Rd index aeff0af..67cce0c 100644 --- a/man/dot-spatPlot2D_single.Rd +++ b/man/dot-spatPlot2D_single.Rd @@ -11,7 +11,6 @@ show_image = FALSE, gimage = NULL, image_name = NULL, - largeImage_name = NULL, spat_loc_name = NULL, sdimx = "sdimx", sdimy = "sdimy", @@ -59,6 +58,7 @@ vor_alpha = 1, axis_text = 8, axis_title = 8, + theme_param = list(), show_plot = NULL, return_plot = NULL, save_plot = NULL, @@ -78,9 +78,7 @@ \item{gimage}{a giotto image} -\item{image_name}{name of a giotto image} - -\item{largeImage_name}{name of a giottoLargeImage} +\item{image_name}{name of giotto image(s) to plot} \item{spat_loc_name}{name of spatial locations} @@ -129,6 +127,8 @@ use or vector of colors to use (minimum of 2).} \item{vor_alpha}{transparency of voronoi 'cells'} +\item{theme_param}{list of additional params passed to \code{ggplot2::theme()}} + \item{show_plot}{logical. show plot} \item{return_plot}{logical. return ggplot object} diff --git a/man/gg_annotation_raster.Rd b/man/gg_annotation_raster.Rd index 7e5f574..2b81d22 100644 --- a/man/gg_annotation_raster.Rd +++ b/man/gg_annotation_raster.Rd @@ -11,12 +11,17 @@ \S4method{gg_annotation_raster}{gg,giottoImage}(ggobj, gimage, ...) -\S4method{gg_annotation_raster}{gg,giottoLargeImage}(ggobj, gimage, ...) +\S4method{gg_annotation_raster}{gg,giottoLargeImage}(ggobj, gimage, ext = NULL, ...) } \arguments{ \item{ggobj}{ggplot2 \code{gg} object} \item{gimage}{\code{giottoLargeImage}, \code{giottoImage} or \code{list} thereof} + +\item{\dots}{additional params to pass} + +\item{ext}{Object that responds to \code{ext()}. Defines the plot spatial ROI +that the image should be sampled for.} } \value{ \code{gg} object with images to plot appended as annotation rasters @@ -24,4 +29,9 @@ \description{ Append image to ggplot as annotation_raster } +\details{ +No ... params are implemented for \code{giottoImage}. \cr ... params for +\code{giottoLargeImage} passes to automated resampling params see +\code{?auto_image_resample} for details +} \keyword{internal} diff --git a/man/plot_poly_params.Rd b/man/plot_poly_params.Rd index 5cb6f5c..9e3d742 100644 --- a/man/plot_poly_params.Rd +++ b/man/plot_poly_params.Rd @@ -10,7 +10,7 @@ \item{polygon_feat_type}{feature type associated with polygon information} -\item{polygon_color}{color for polygon border} +\item{polygon_color}{color for polygon border. Set \code{NA} to remove border} \item{polygon_bg_color}{color for polygon background (overruled by polygon_fill)} diff --git a/man/spatCellPlot.Rd b/man/spatCellPlot.Rd index 9d2a6a8..45bd5c4 100644 --- a/man/spatCellPlot.Rd +++ b/man/spatCellPlot.Rd @@ -60,6 +60,7 @@ spatCellPlot2D( cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", + theme_param = list(), show_plot = NULL, return_plot = NULL, save_plot = NULL, @@ -183,6 +184,8 @@ cell_color parameter} \item{cow_align}{cowplot param: how to align} +\item{theme_param}{list of additional params passed to \code{ggplot2::theme()}} + \item{show_plot}{logical. show plot} \item{return_plot}{logical. return ggplot object} diff --git a/man/spatDeconvPlot.Rd b/man/spatDeconvPlot.Rd index 9b3bc06..118084b 100644 --- a/man/spatDeconvPlot.Rd +++ b/man/spatDeconvPlot.Rd @@ -30,6 +30,7 @@ spatDeconvPlot( return_plot = NULL, save_plot = NULL, save_param = list(), + theme_param = list(), default_save_name = "spatDeconvPlot" ) } @@ -86,6 +87,8 @@ named vector of colors} \item{save_param}{list of saving parameters, see \code{\link{showSaveParameters}}} +\item{theme_param}{list of additional params passed to \code{ggplot2::theme()}} + \item{default_save_name}{default save name for saving, don't change, change save_name in save_param} } @@ -96,6 +99,3 @@ ggplot Visualize cell type enrichment / deconvolution results in a scatterpie } -\details{ -Description of parameters. -} diff --git a/man/spatDimGenePlot3D.Rd b/man/spatDimFeatPlot3D.Rd similarity index 93% rename from man/spatDimGenePlot3D.Rd rename to man/spatDimFeatPlot3D.Rd index cd2bae2..47d157a 100644 --- a/man/spatDimGenePlot3D.Rd +++ b/man/spatDimFeatPlot3D.Rd @@ -1,10 +1,11 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/vis_spatial.R -\name{spatDimGenePlot3D} +\name{spatDimFeatPlot3D} +\alias{spatDimFeatPlot3D} \alias{spatDimGenePlot3D} -\title{spatDimGenePlot3D} +\title{spatDimFeatPlot3D} \usage{ -spatDimGenePlot3D( +spatDimFeatPlot3D( gobject, feat_type = NULL, spat_unit = NULL, @@ -55,8 +56,10 @@ spatDimGenePlot3D( return_plot = NULL, save_plot = NULL, save_param = list(), - default_save_name = "spatDimGenePlot3D" + default_save_name = "spatDimFeatPlot3D" ) + +spatDimGenePlot3D(...) } \arguments{ \item{gobject}{giotto object} @@ -163,6 +166,8 @@ see \code{\link{showSaveParameters}}} \item{default_save_name}{default save name for saving, don't change, change save_name in save_param} + +\item{...}{additional params to pass} } \value{ plotly @@ -174,9 +179,14 @@ reduction coordinates in ggplot mode \details{ Description of parameters. } +\section{Functions}{ +\itemize{ +\item \code{spatDimGenePlot3D()}: deprecated + +}} \examples{ g <- GiottoData::loadGiottoMini("starmap") -spatDimGenePlot3D(g, genes = "Slc17a7") +spatDimFeatPlot3D(g, genes = "Slc17a7") } \concept{spatial and dimension reduction gene expression visualizations} diff --git a/man/spatDimPlot.Rd b/man/spatDimPlot.Rd index 6232edb..1ad271b 100644 --- a/man/spatDimPlot.Rd +++ b/man/spatDimPlot.Rd @@ -104,8 +104,7 @@ spatDimPlot(gobject, ...) \item{image_name}{name of a giotto image or multiple images with group_by} -\item{largeImage_name}{name of a giottoLargeImage or multiple images -with group_by} +\item{largeImage_name}{deprecated} \item{spat_loc_name}{name of spatial locations} diff --git a/man/spatFeatPlot2D.Rd b/man/spatFeatPlot2D.Rd index cf20cbf..7676ef3 100644 --- a/man/spatFeatPlot2D.Rd +++ b/man/spatFeatPlot2D.Rd @@ -51,6 +51,7 @@ spatFeatPlot2D( cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", + theme_param = list(), show_plot = NULL, return_plot = NULL, save_plot = NULL, @@ -71,8 +72,7 @@ spatFeatPlot2D( \item{image_name}{name of a giotto image or multiple images with group_by} -\item{largeImage_name}{name of a giottoLargeImage or multiple images -with group_by} +\item{largeImage_name}{deprecated} \item{spat_loc_name}{name of spatial locations} @@ -155,6 +155,8 @@ or 'sequential' (scaled based on data range)} \item{cow_align}{cowplot param: how to align} +\item{theme_param}{list of additional params passed to \code{ggplot2::theme()}} + \item{show_plot}{logical. show plot} \item{return_plot}{logical. return ggplot object} @@ -183,7 +185,7 @@ spatFeatPlot2D(g, feats = "Gna12") } \seealso{ -\code{\link{spatGenePlot3D}} +\code{\link{spatFeatPlot3D}} Other spatial feature expression visualizations: \code{\link{spatFeatPlot2D_single}()} diff --git a/man/spatFeatPlot2D_single.Rd b/man/spatFeatPlot2D_single.Rd index 794923f..5534e8a 100644 --- a/man/spatFeatPlot2D_single.Rd +++ b/man/spatFeatPlot2D_single.Rd @@ -50,6 +50,7 @@ spatFeatPlot2D_single( cow_rel_h = 1, cow_rel_w = 1, cow_align = "h", + theme_param = list(), show_plot = NULL, return_plot = NULL, save_plot = NULL, @@ -70,8 +71,7 @@ spatFeatPlot2D_single( \item{image_name}{name of a giotto image or multiple images with group_by} -\item{largeImage_name}{name of a giottoLargeImage or multiple images -with group_by} +\item{largeImage_name}{deprecated} \item{spat_loc_name}{name of spatial locations} @@ -152,6 +152,8 @@ ggplot alpha parameter} \item{cow_align}{cowplot param: how to align} +\item{theme_param}{list of additional params passed to \code{ggplot2::theme()}} + \item{show_plot}{logical. show plot} \item{return_plot}{logical. return ggplot object} @@ -180,7 +182,7 @@ spatFeatPlot2D_single(g, feats = c("Gna12", "Ccnd2", "Btbd17")) } \seealso{ -\code{\link{spatGenePlot3D}} +\code{\link{spatFeatPlot3D}} Other spatial feature expression visualizations: \code{\link{spatFeatPlot2D}()} diff --git a/man/spatGenePlot3D.Rd b/man/spatFeatPlot3D.Rd similarity index 90% rename from man/spatGenePlot3D.Rd rename to man/spatFeatPlot3D.Rd index 66a55c4..f20f1da 100644 --- a/man/spatGenePlot3D.Rd +++ b/man/spatFeatPlot3D.Rd @@ -1,16 +1,16 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/vis_spatial.R -\name{spatGenePlot3D} +\name{spatFeatPlot3D} +\alias{spatFeatPlot3D} \alias{spatGenePlot3D} -\title{spatGenePlot3D} +\title{spatFeatPlot3D} \usage{ -spatGenePlot3D( +spatFeatPlot3D( gobject, spat_unit = NULL, feat_type = NULL, spat_loc_name = "raw", expression_values = c("normalized", "scaled", "custom"), - genes = deprecated(), feats, spat_enr_names = NULL, show_network = FALSE, @@ -39,8 +39,11 @@ spatGenePlot3D( return_plot = NULL, save_plot = NULL, save_param = list(), - default_save_name = "spatGenePlot3D" + default_save_name = "spatFeatPlot3D", + ... ) + +spatGenePlot3D(...) } \arguments{ \item{gobject}{giotto object} @@ -53,8 +56,6 @@ spatGenePlot3D( \item{expression_values}{gene expression values to use} -\item{genes}{deprecated} - \item{feats}{feats to show} \item{spat_enr_names}{names of spatial enrichment results to include} @@ -115,6 +116,8 @@ see \code{\link{showSaveParameters}}} \item{default_save_name}{default save name for saving, don't change, change save_name in save_param} + +\item{...}{additional params to pass} } \value{ plotly @@ -123,12 +126,14 @@ plotly Visualize cells and gene expression according to spatial coordinates } -\details{ -Description of parameters. -} +\section{Functions}{ +\itemize{ +\item \code{spatGenePlot3D()}: deprecated + +}} \examples{ g <- GiottoData::loadGiottoMini("starmap") -spatGenePlot3D(g, feats = "Slc17a7") +spatFeatPlot3D(g, feats = "Slc17a7") } \concept{spatial gene expression visualizations} diff --git a/man/spatInSituPlotDensity.Rd b/man/spatInSituPlotDensity.Rd index 9ddab41..197ccc1 100644 --- a/man/spatInSituPlotDensity.Rd +++ b/man/spatInSituPlotDensity.Rd @@ -52,7 +52,7 @@ spatInSituPlotDensity( \item{polygon_feat_type}{feature type associated with polygon information} -\item{polygon_color}{color for polygon border} +\item{polygon_color}{color for polygon border. Set \code{NA} to remove border} \item{polygon_fill}{character. what to color to fill polgyons by (e.g. metadata col or spatial enrichment col)} diff --git a/man/spatInSituPlotHex.Rd b/man/spatInSituPlotHex.Rd index 8037180..1a2c91a 100644 --- a/man/spatInSituPlotHex.Rd +++ b/man/spatInSituPlotHex.Rd @@ -60,7 +60,7 @@ spatInSituPlotHex( \item{polygon_feat_type}{feature type associated with polygon information} -\item{polygon_color}{color for polygon border} +\item{polygon_color}{color for polygon border. Set \code{NA} to remove border} \item{polygon_fill}{character. what to color to fill polgyons by (e.g. metadata col or spatial enrichment col)} diff --git a/man/spatInSituPlotPoints.Rd b/man/spatInSituPlotPoints.Rd index 1e8afd1..a25b7e7 100644 --- a/man/spatInSituPlotPoints.Rd +++ b/man/spatInSituPlotPoints.Rd @@ -35,7 +35,7 @@ spatInSituPlotPoints( polygon_fill_gradient_style = c("divergent", "sequential"), polygon_fill_as_factor = NULL, polygon_fill_code = NULL, - polygon_alpha = 0.5, + polygon_alpha = NULL, polygon_line_size = 0.4, axis_text = 8, axis_title = 8, @@ -45,6 +45,7 @@ spatInSituPlotPoints( show_legend = TRUE, plot_method = c("ggplot", "scattermore", "scattermost"), plot_last = c("polygons", "points"), + theme_param = list(), show_plot = NULL, return_plot = NULL, save_plot = NULL, @@ -62,8 +63,7 @@ spatInSituPlotPoints( \item{image_name}{name of a giotto image or multiple images with group_by} -\item{largeImage_name}{name of a giottoLargeImage or multiple images -with group_by} +\item{largeImage_name}{deprecated} \item{spat_unit}{spatial unit (e.g. "cell")} @@ -102,7 +102,7 @@ number which will be recycled to length 2.} \item{polygon_feat_type}{feature type associated with polygon information} -\item{polygon_color}{color for polygon border} +\item{polygon_color}{color for polygon border. Set \code{NA} to remove border} \item{polygon_bg_color}{color for polygon background (overruled by polygon_fill)} @@ -144,6 +144,8 @@ color scaling) or 'sequential' (scaled based on data range)} \item{plot_last}{which layer to show on top of plot, polygons (default) or points.} +\item{theme_param}{list of additional params passed to \code{ggplot2::theme()}} + \item{show_plot}{logical. show plot} \item{return_plot}{logical. return ggplot object} diff --git a/man/spatPlot.Rd b/man/spatPlot.Rd index 17921de..139ccbc 100644 --- a/man/spatPlot.Rd +++ b/man/spatPlot.Rd @@ -71,6 +71,7 @@ spatPlot2D( return_plot = NULL, save_plot = NULL, save_param = list(), + theme_param = list(), default_save_name = "spatPlot2D" ) @@ -254,6 +255,8 @@ cell_color parameter} \item{save_param}{list of saving parameters, see \code{\link{showSaveParameters}}} +\item{theme_param}{list of additional params passed to \code{ggplot2::theme()}} + \item{default_save_name}{default save name for saving, don't change, change save_name in save_param}