Skip to content

Commit

Permalink
add vizShuffledGrids function
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeldossantospeixoto committed Aug 21, 2024
1 parent 22ba67d commit ecdbb14
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions R/visualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,60 @@ vizGrids <- function(cells, scale,



#' Visualize grids and clusters after shuffling
#'
#' @description Uses the cells sf object and size of grid to visualize the grids
#' and the shuffled null background.
#'
#' @param cells sf object; spatial (x and y) coordinates and celltypes column
#' @param shuffledList list; cell type labels shuffled at different scales
#' (output from makeShuffledCells())
#' @param scale numeric; size of the scale to plot
#' @param permutation numeric; the number of the permutation of interest.
#' @param square boolean; if true, create a squared grid, if false, make
#' hexagonal grid (default TRUE)
#' @param ofInterest character vector; a vector of specific clusters to visualize
#' @param pointSize numeric; size of points
#' @param alpha numeric; transparency of points
#'
#' @return plot
#'
#' @examples
#' \dontrun{
#' data(slide)
#' cells <- crawdad::toSF(pos = slide[,c("x", "y")], celltypes = slide$celltypes)
#' vizAllClusters(cells)
#' }
#'
#' @export
vizShuffledGrids <- function(cells, shuffledList, scale,
permutation = 1,
square = TRUE,
ofInterest = NULL, pointSize = 1, alpha = 0.5){

## define colors to be consistent with the vizCluster function
cluster_cols <- rainbow(n = length(unique(cells$celltypes)))
names(cluster_cols) <- unique(cells$celltypes)
cluster_cols['other'] <- '#E6E6E6'

## assing the shuffled labels to the celltypes column
shuffled_labels <- shuffledList[[as.character(scale)]][[as.character(permutation)]]
cells$celltypes <- as.factor(shuffled_labels)
## determine the total number of permutations to perform the offsetting
totalPermutations <- length(shuffledList[[1]])

## use vizGrids to plot
vizGrids(cells = cells, scale = scale,
permutation = permutation, totalPermutations = totalPermutations,
square = square,
ofInterest = ofInterest, pointSize = pointSize, alpha = alpha) +
## NA to gray
ggplot2::scale_color_manual(values = cluster_cols, na.value = "#E6E6E6")

}



#' Plot trends with ggplot2
#'
#' @description The input data.frame should be the results list from `findTrends()` that has been melted into a data.frame using `meltResultsList()`.
Expand Down

0 comments on commit ecdbb14

Please sign in to comment.