0
$\begingroup$

I want to create a dodged barchart with different cell cycles for each cell type within each condition.

dput(DF_count)

structure(list(orig.ident = c("treated", "treated", "treated", 
"treated", "treated", "treated", "treated", "treated", "treated", 
"treated", "treated", "treated", "treated", "treated", "treated", 
"treated", "treated", "treated", "treated", "treated", "treated", 
"treated", "treated", "treated", "treated", "treated", "treated", 
"treated", "treated", "treated", "treated", "treated", "treated", 
"untreated", "untreated", "untreated", "untreated", "untreated", 
"untreated", "untreated", "untreated", "untreated", "untreated", 
"untreated", "untreated", "untreated", "untreated", "untreated", 
"untreated", "untreated", "untreated", "untreated", "untreated", 
"untreated", "untreated", "untreated", "untreated", "untreated", 
"untreated", "untreated", "untreated", "untreated", "untreated", 
"untreated", "untreated", "untreated"), Phase = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L), .Label = c("G1", "G2M", "S"), class = "factor"), old.ident = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L), .Label = c("Fibroblast", "T cell", "Macrophage", 
"Stellate", "Acinar", "Endothelial", "Tumor", "B cell", "Mast cell", 
"Ductal", "Islets of Langerhans"), class = "factor"), n = c(3257L, 
666L, 560L, 446L, 91L, 464L, 192L, 506L, 162L, 207L, 90L, 603L, 
608L, 238L, 98L, 22L, 110L, 215L, 95L, 63L, 97L, 10L, 2191L, 
618L, 335L, 349L, 35L, 294L, 250L, 231L, 142L, 153L, 16L, 2910L, 
1259L, 853L, 559L, 1134L, 348L, 306L, 133L, 116L, 12L, 192L, 
706L, 1186L, 374L, 112L, 616L, 106L, 277L, 32L, 47L, 6L, 27L, 
1715L, 1312L, 575L, 390L, 1036L, 250L, 252L, 91L, 66L, 8L, 68L
), Freq = c(53.8258139150554, 35.2008456659619, 49.4263018534863, 
49.9440089585666, 61.4864864864865, 53.4562211981567, 29.2237442922374, 
60.8173076923077, 44.141689373297, 45.2954048140044, 77.5862068965517, 
9.96529499256321, 32.1353065539112, 21.0061782877317, 10.9742441209406, 
14.8648648648649, 12.6728110599078, 32.7245053272451, 11.4182692307692, 
17.1662125340599, 21.2253829321663, 8.62068965517241, 36.2088910923814, 
32.6638477801269, 29.567519858782, 39.0817469204927, 23.6486486486486, 
33.8709677419355, 38.0517503805175, 27.7644230769231, 38.6920980926431, 
33.4792122538293, 13.7931034482759, 54.5863815419246, 33.5107798775619, 
47.3362930077692, 52.6861451460886, 40.7035175879397, 49.4318181818182, 
36.6467065868263, 51.953125, 50.6550218340611, 46.1538461538462, 
66.8989547038328, 13.2432939410992, 31.5677402182592, 20.7547169811321, 
10.5560791705938, 22.1105527638191, 15.0568181818182, 33.1736526946108, 
12.5, 20.5240174672489, 23.0769230769231, 9.40766550522648, 32.1703245169762, 
34.9214799041789, 31.9089900110988, 36.7577756833176, 37.1859296482412, 
35.5113636363636, 30.1796407185629, 35.546875, 28.82096069869, 
30.7692307692308, 23.6933797909408)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -66L), groups = structure(list(
    orig.ident = c("treated", "treated", "treated", "treated", 
    "treated", "treated", "treated", "treated", "treated", "treated", 
    "treated", "untreated", "untreated", "untreated", "untreated", 
    "untreated", "untreated", "untreated", "untreated", "untreated", 
    "untreated", "untreated"), old.ident = structure(c(1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L, 10L, 11L), .Label = c("Fibroblast", "T cell", 
    "Macrophage", "Stellate", "Acinar", "Endothelial", "Tumor", 
    "B cell", "Mast cell", "Ductal", "Islets of Langerhans"), class = "factor"), 
    .rows = list(c(1L, 12L, 23L), c(2L, 13L, 24L), c(3L, 14L, 
    25L), c(4L, 15L, 26L), c(5L, 16L, 27L), c(6L, 17L, 28L), 
        c(7L, 18L, 29L), c(8L, 19L, 30L), c(9L, 20L, 31L), c(10L, 
        21L, 32L), c(11L, 22L, 33L), c(34L, 45L, 56L), c(35L, 
        46L, 57L), c(36L, 47L, 58L), c(37L, 48L, 59L), c(38L, 
        49L, 60L), c(39L, 50L, 61L), c(40L, 51L, 62L), c(41L, 
        52L, 63L), c(42L, 53L, 64L), c(43L, 54L, 65L), c(44L, 
        55L, 66L))), row.names = c(NA, -22L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

Used this code to generate a stacked bar graph:

ggplot(DF_Count, aes(x = old.ident, y = Freq, fill = Phase))+
  geom_col()+
  geom_text(aes(label = paste(round(Freq, 2),"%")),position = position_stack(vjust = 0.5))+
  facet_wrap(~orig.ident)

But want the 2 conditions to be next to each other on the same graph. How do I use this dataset and create a dodged bar chart to have both conditions together?

$\endgroup$
1
$\begingroup$

Here is a rather straight-forward work-around for your problem:

ggplot(DF_Count, aes(x = orig.ident, y = Freq, fill = Phase))+
  geom_col()+
  geom_text(aes(label = paste(round(Freq, 2),"%")),position = position_stack(vjust = 0.5))+
  facet_wrap(~old.ident)

enter image description here

It does not give a single plot or a dodged bar chart representing the two conditions you would like to visualize. For an exact solution to your problem, see this (actually the answer below the linked answer is analogous to what I have suggested above).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.