How to plot combined bar graph in R
1
1
Entering edit mode
18 months ago
Bioinfo ▴ 30

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)
barplot R singleR ggplot • 688 views
ADD COMMENT
6
Entering edit mode
18 months ago

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.

ADD COMMENT

Login before adding your answer.

Traffic: 2515 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6