Sorry, I did not think that the code of the function was important. I though it could be managed by just changing the Quarto options.
Anyway, this is the function:
# Print all the stamps associated with a patient using the cluster information to color the cells
generate_spatial_plots <- function(
patient_data,
cluster_var,
cluster_name = NULL,
color_lookup_table = NULL,
legend_fill = "ident"
) {
# If a human friendly name is not given, use the name of the column in the Seurat object
if (is.null(cluster_name)) {
cluster_name <- cluster_var
}
# If no color lookup table is given create one
if (is.null(color_lookup_table)) {
color_lookup_table <- generate_colors_lookup_table(patient_data, cluster_var)
}
# Ensure cluster_name is a factor and sort its levels alphabetically
patient_data@meta.data[[cluster_var]] <- factor(patient_data@meta.data[[cluster_var]])
# Get a pointer to the spatial representation of the data
patient_image <- Images(patient_data)[1]
# List to be returned with all the plots
clustering_plots <- list()
# Print some information about the clusters
print(paste(cluster_name, "and number of cells in each of the cluster"))
print(table(patient_data[[cluster_var]]))
# Select the cluster as the identity
Idents(patient_data) <- cluster_var
# Plot the cells using their polygonal boundaries
DefaultBoundary(patient_data[[patient_image]]) <- "segmentation"
# Plot cells in their spatial context
stamps_list <- list()
# Loop over every core associated with the patient
for(curr_core in sort(unique(patient_data@meta.data$core_serial))) {
# Loop over every stamp associated with the current core
# A core can be associated with multiple stamps (e.g. different areas of the same metastasis)
for (curr_stamp in sort(unique(patient_data@meta.data$stamp[patient_data@meta.data$core_serial == curr_core]))) {
# Subset data from current core and stamp
core_stamp_subset <- subset(patient_data, subset = core_serial == curr_core & stamp == curr_stamp)
# Read patient number
patient_num <- get_patient_num(core_stamp_subset)
# Plot the current core/stamp combination with all the cells in their spatial context and colored by cluster
stamp_plot <- ImageDimPlot(
core_stamp_subset,
fov = patient_image,
# Set border color to 'NA' as 'white' masks all cells when zoomed out
border.color = NA,
flip_xy = FALSE,
cols = color_lookup_table) + theme(
legend.text = element_text(size = 6),
legend.title = element_text(size = 8),
legend.key.size = unit(0.5, 'lines'),
legend.spacing = unit(0.5, 'lines')
) +
labs(
title = paste("Patient", patient_num, "Core", curr_core, ", Stamp", curr_stamp),
subtitle = cluster_name
) +
guides(
fill = guide_legend(legend_fill)
)
stamp_plot_name <- paste("Patient", patient_num, cluster_var, "core", curr_core, "stamp", as.character(curr_stamp), sep = "_")
clustering_plots[[stamp_plot_name]] <- stamp_plot
}
}
return(clustering_plots)
}
Basically, it subsets a Seurat object and use the ImageDimPlot function, also from Seurat, to generate the plots.
After consulting the Seurat web site and looking at the docs for the ImageDimPlot() function I see this note at the bottom of the page:
Value
If combine = TRUE, a patchwork ggplot object; otherwise, a list of ggplot objects
Unless I'm imagining things, this leads me to believe that the product of the function is a ggplot object, basically a list. I've done a lot of work picking apart these and changing the values of the elements, particularly with respect to legend text.
Most likely the ist_cell_types_stamps_plots object is a ggplot object. Run str(ist_cell_types_stamps_plots) in your console to get a look at the structure. You'll see a nested list structure. You'll need to comb through that looking for the element or elements that constitute the captions, but when you find them you can easily remove the captions' text.
This won't be easy, but if you are keen on removing these captions you'll invest the effort.
Let's take a completely made-up example. Suppose that after inspecting the ggplot object you see a list item like ist_cell_types_stamps_plots$facet$labels. Further suppose that that is a list of three. If you see the text that you want to remove in ist_cell_types_stamps_plots$facet$labels[1], you can remove it with ist_cell_types_stamps_plots$facet$labels[1] <- "".