After running the following function
count_table <- table(Sample.@meta.data$seurat_clusters, Sample@meta.data$orig.ident, Sample@meta.data$singlr_labels)
View(count_table)
str(count_table)
'table' int [1:15, 1:2, 1:20] 0 0 0 0 0 0 2 0 273 0 ...
- attr(*, "dimnames")=List of 3
..$ : chr [1:15] "0" "1" "2" "3" ...
..$ : chr [1:2] "Sample1" "Sample2"
..$ : chr [1:20] "B_cell" "BM & Prog." "CMP" "DC" ...
I want to create a combined bar plot for that I used the following function but ends with an error.
ggplot(count_table) + geom_bar(aes(x=singlr_labels, fill=orig.ident), position = "dodge") + facet_wrap(~seurat_clusters)
1 answer
in R, table() is used to build contingency tables as specified in R doc : https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/table
table uses the cross-classifying factors to build a contingency table of the counts at each combination of factor levels.
To build an actual table, or in the correct nomenclature a "dataframe" use data.frame() function
Also rename the variable accordingly
count_table <- data.frame(seurat_clusters = Sample@meta.data$seurat_clusters,
orig.ident = Sample@meta.data$orig.ident,
singlr_labels = Sample@meta.data$singlr_labels)
Then plot
ggplot(count_table) +
geom_bar(aes(x=singlr_labels, fill=orig.ident), position = "dodge") +
facet_wrap(~seurat_clusters)
But you could directly use seurat metadata and plot it without building a temporary dataframe as :
Sample@meta.data %>%
ggplot() +
geom_bar(aes(x=singlr_labels, fill=orig.ident), position = "dodge") +
facet_wrap(~seurat_clusters)
Advice : Try to take a R basic course to familiarize with R syntax. Once that learned you will be much more confortable working using R.
Log in to answer this question.