3434# ' @importFrom rlang %||% syms
3535# ' @importFrom dplyr group_by summarise
3636# ' @importFrom tidyr pivot_longer
37- # ' @importFrom SeuratObject GetAssayData Embeddings DefaultDimReduc Graphs
37+ # ' @importFrom SeuratObject GetAssayData Embeddings DefaultDimReduc Graphs Reductions Idents
3838# ' @importFrom plotthis ViolinPlot BoxPlot BarPlot DotPlot RidgePlot FeatureDimPlot Heatmap CorPlot CorPairsPlot
3939# ' @examples
4040# ' \donttest{
215215FeatureStatPlot <- function (
216216 object , features , plot_type = c(" violin" , " box" , " bar" , " ridge" , " dim" , " cor" , " heatmap" , " dot" ),
217217 reduction = NULL , graph = NULL , bg_cutoff = 0 , dims = 1 : 2 , rows_name = " Features" ,
218- ident = " seurat_clusters " , assay = NULL , layer = NULL , agg = mean , group_by = NULL ,
218+ ident = NULL , assay = NULL , layer = NULL , agg = mean , group_by = NULL ,
219219 split_by = NULL , facet_by = NULL , xlab = NULL , ylab = NULL , x_text_angle = NULL , ...
220220) {
221221 plot_type <- match.arg(plot_type )
222222 if (! is.null(facet_by ) && plot_type != " dim" ) {
223223 stop(" Cannot facet plots because the plots are facetted by the 'features'." )
224224 }
225225
226- reduction <- reduction %|| % DefaultDimReduc(object )
226+ reduction <- reduction %|| % (
227+ if (is.null(Reductions(object ))) NULL else DefaultDimReduc(object )
228+ )
227229 # dim plot may use expression for highlighting cells
228230 # Heatmap may use other variables as annotations, but shrinking only includes minimal columns
229231 should_shrink <- ! plot_type %in% c(" dim" , " heatmap" , " dot" )
@@ -233,11 +235,24 @@ FeatureStatPlot <- function(
233235 assay_data <- GetAssayData(object , assay = assay , layer = layer )
234236 assay_feature <- intersect(unlisted_features , rownames(assay_data ))
235237 assay_data <- t(as.matrix(assay_data [assay_feature , , drop = FALSE ]))
236- data <- cbind(Embeddings(object , reduction = reduction ), object @ meta.data , assay_data )
238+ if (is.null(reduction )) {
239+ data <- cbind(object @ meta.data , assay_data )
240+ } else {
241+ data <- cbind(Embeddings(object , reduction = reduction ), object @ meta.data , assay_data )
242+ }
237243
244+ if (is.null(ident )) {
245+ ident <- " Identity"
246+ data $ Identity <- Idents(object )
247+ }
238248 if (should_shrink ) {
239249 dims <- if (is.null(dims )) NULL else colnames(data )[dims ]
240- data <- data [, c(dims , ident , unlisted_features , group_by , if (isTRUE(split_by )) NULL else split_by ), drop = FALSE ]
250+ selected_columns <- c(dims , ident , unlisted_features , group_by , if (! isTRUE(split_by )) split_by )
251+ nonexisting_columns <- setdiff(selected_columns , colnames(data ))
252+ if (length(nonexisting_columns ) > 0 ) {
253+ stop(" [FeatureStatPlot] The following columns are not found in the object: " , paste(nonexisting_columns , collapse = " , " ))
254+ }
255+ data <- data [, selected_columns , drop = FALSE ]
241256 }
242257 if (should_pivot ) {
243258 data <- pivot_longer(data , cols = unlisted_features , names_to = " .features" , values_to = " .value" )
0 commit comments