I am visualizing wanting to make a histogram in R using my normalized counts (from SCTransform). Specifically, i want the y-axis to be normalized counts and the x-axis to be my seurat Idents (i have: A, B, C, D) and grouped by Wildtype (WT) and Mutant(MT).
Is this possible in R with seurat object as i do not really know how to retrieve this data to make my plot.
I'm showing for one gene only in this code, you can do similarly for other genes as well.
rm(list=ls())
library(Seurat)
library(SeuratData)
library(ggplot2)
seurat_obj=UpdateSeuratObject(pbmc3k.final)
# Add a new column 'condition' and assign values 'WT' and 'MT'
metadata <- data.frame(
cell_id = colnames(seurat_obj),
other_metadata_column = sample(c("Group1", "Group2"), ncol(seurat_obj), replace = TRUE)
)
# Assign the metadata to Seurat object
seurat_obj <- AddMetaData(object = seurat_obj, metadata = metadata)
seurat_obj@meta.data$condition <- ifelse(metadata$other_metadata_column == "Group1", "WT", "MT")
# Print the updated metadata to verify
head(seurat_obj@meta.data)
normalized_counts <- GetAssayData(seurat_obj, layer = "data", assay = "RNA")
# Extract meta data
metadata <- seurat_obj@meta.data
# Specify the gene of interest
gene_of_interest <- "MS4A1"
# Get the normalized counts for the specific gene
gene_data <- normalized_counts[gene_of_interest, ]
# Combine with metadata
plot_data <- data.frame(
Seurat_Ident = Idents(seurat_obj),
Condition = metadata$condition, # Replace 'condition' with your actual column name in metadata
Normalized_Count = gene_data
)
# Check the data
head(plot_data)
# Plot the histogram
ggplot(plot_data, aes(x = Seurat_Ident, y = Normalized_Count, fill = Condition)) +
geom_histogram(stat = "identity", position = "dodge") +
labs(title = paste("Normalized counts for", gene_of_interest),
x = "Seurat Idents",
y = "Normalized Counts") +
theme_minimal() +
scale_fill_manual(values = c("WT" = "blue", "MT" = "red"))
ADD COMMENT
• link
updated 6 months ago by
Ram
44k
•
written 6 months ago by
bk11
★
3.0k
0
Entering edit mode
I just saw this and wanted to say thank you. I am attempting to make this plot for TimePoint (metadata information) week 1 instead of a specific gene. Is that even possible?
Please see title of your question what had you asked for help. Now you are asking something else. You can get normalized counts for genes, right?? and not for week 1 or others...
Yes, you are right! I just needed some confirmation. I am still just learning how to do single cell analysis so i mostly always have questions as there is something new. Thank you i really appreciated your answer.
I just saw this and wanted to say thank you. I am attempting to make this plot for TimePoint (metadata information) week 1 instead of a specific gene. Is that even possible?
Please see title of your question what had you asked for help. Now you are asking something else. You can get normalized counts for genes, right?? and not for week 1 or others...
Yes, you are right! I just needed some confirmation. I am still just learning how to do single cell analysis so i mostly always have questions as there is something new. Thank you i really appreciated your answer.