Answers.Body: <p>Based on your example, you can use count for each phase of cell cycle for each cell types in each condition. Here, I'm using <code>dplyr</code> package to do that but most likely, you could have the same output using various methods:</p>
  <pre class="lang-r prettyprint-override"><code>DF &lt;- rbind(dfhead, dftail)
  
  library(dplyr)
  DF_Count &lt;- DF %&gt;%group_by(orig.ident,Phase,old.ident) %&gt;%
    count() %&gt;%
    ungroup() %&gt;%
    group_by(orig.ident,old.ident) %&gt;%
    mutate(Freq = n/sum(n)*100)
  
  # A tibble: 8 x 5
  # Groups:   orig.ident, old.ident [5]
    orig.ident Phase old.ident      n  Freq
    &lt;chr&gt;      &lt;fct&gt; &lt;fct&gt;      &lt;int&gt; &lt;dbl&gt;
  1 treated    G1    Fibroblast     1 100  
  2 treated    G1    Stellate       1 100  
  3 treated    G2M   Tumor          1  33.3
  4 treated    S     Tumor          2  66.7
  5 treated    S     Mast cell      1 100  
  6 untreated  G1    Acinar         1  16.7
  7 untreated  G2M   Acinar         2  33.3
  8 untreated  S     Acinar         3  50  
  </code></pre>
  <p>As you can see, <code>DF_Count</code> has the frequency for each cell types of each phase of the cell cycle in function of the condition. We can use <code>DF_Count</code> to get the following plot. Using <code>facet_wrap</code>, you can create two panels based on the condition column and thus represent the cell cycle of each cell types in function of the treatment condition:</p>
  <pre class="lang-r prettyprint-override"><code>ggplot(DF_Count, aes(x = old.ident, y = Freq, fill = Phase))+
    geom_col()+
    geom_text(aes(label = paste(round(Freq, 2),&quot;%&quot;)),position = position_stack(vjust = 0.5))+
    facet_wrap(~orig.ident)
  </code></pre>
  <p><a href="https://i.sstatic.net/hcjcz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/hcjcz.png" alt="enter image description here" /></a></p>
  <p>Does it look what you are expecting ?</p>
  <p>NB: <code>DF</code> here is the addition of your head and tail of your <code>all.combined</code> dataframe.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-21T00:22:03.077Z
Answers.Text: How about the other cell types? Can I just make `DF <- all.combined@metadata`? That way all the cell types are included?
Answers.UserDisplayName: user6774
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-21T00:25:13.293Z
Answers.Text: It should work. Here the graph represent only the portion of data you have provided. If you think this answer was useful, do not hesitate to vote or validate it. Learn more here: https://bioinformatics.stackexchange.com/help/someone-answers
Answers.UserDisplayName: dc37
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-21T00:11:07.753Z
Answers.LastActivityDate: 2020-01-21T00:11:07.753Z
Answers.LastEditDate: 2020-06-18T08:30:41.743Z
Answers.UserDisplayName: dc37
Body: <p>I want to create a stacked bar-graph with different cell cycles for each cell type within each condition. I have uploaded the file for it. Don't know how to go about it as now I have another condition (cell cycle). I was able to do the bar graph comparing cell cycle per condition but now I want to compare cell type and cell cycle in each condition</p>
  
  <p>output of dput(head(all.combined@metadata))</p>
  
  <pre><code>structure(list(orig.ident = c("treated", "treated", "treated", 
      "treated", "treated", "treated"), nCount_RNA = c(1892, 307, 1348, 
      3699, 4205, 4468), nFeature_RNA = c(960L, 243L, 765L, 1612L, 
      1341L, 1644L), percent.mt = c(0.211416490486258, 1.62866449511401, 
      4.45103857566766, 4.4065963773993, 0.0713436385255648, 3.87197851387645
      ), RNA_snn_res.0.5 = structure(c(11L, 11L, 5L, 6L, 11L, 13L), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
      "13", "14", "15", "16", "17", "18", "19"), class = "factor"), seurat_clusters = structure(c(11L, 11L, 5L, 6L, 11L, 13L), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"), class = "factor"), S.Score = c(0.476893835992198, -0.0200784617568548, -0.0335915198305002, -0.0247184276246385, 0.010785196602457, 0.0190008903712199), G2M.Score = c(0.204441469200986, 0.173804859670862, -0.0313235510969097, -0.0376796363661889, -0.0559526905696905, -0.0122031631356698), Phase = structure(c(3L, 2L, 1L, 1L, 3L, 3L), .Label = c("G1", "G2M", "S"), class = "factor"), old.ident = structure(c(7L,7L, 1L, 4L, 7L, 9L), .Label = c("Fibroblast", "T cell", "Macrophage", "Stellate", "Acinar", "Endothelial", "Tumor", "B cell", "Mast cell", "Ductal", "Islets of Langerhans"), class = "factor")), row.names = c("treated_AAACGCTAGCGGGTTA-1", "treated_AAAGGTAAGTACAGAT-1", "treated_AAAGTGAGTTTGATCG-1", "treated_AAATGGACAAAGTGTA-1", 
      "treated_AACAAAGGTCGACTTA-1", "treated_AACAGGGTCCTAGCCT-1"), class = "data.frame")
  </code></pre>
  
  <p>output of dput(tail(all.combined@metadata))</p>
  
  <pre><code>structure(list(orig.ident = c("untreated", "untreated", "untreated", 
  "untreated", "untreated", "untreated"), nCount_RNA = c(901, 823, 
  1184, 1835, 1147, 1407), nFeature_RNA = c(482L, 479L, 649L, 1043L, 
  604L, 709L), percent.mt = c(1.77580466148724, 2.91616038882138, 
  4.22297297297297, 3.86920980926431, 2.0052310374891, 4.05117270788913
  ), RNA_snn_res.0.5 = structure(c(7L, 7L, 7L, 14L, 7L, 7L), .Label = c("0", 
  "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
  "13", "14", "15", "16", "17", "18", "19"), class = "factor"), 
      seurat_clusters = structure(c(7L, 7L, 7L, 14L, 7L, 7L), .Label = c("0", 
      "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 
      "12", "13", "14", "15", "16", "17", "18", "19"), class = "factor"), 
      S.Score = c(-0.0320858200243315, 0.0304725660342869, 0.0215996091745327, 
      0.0384166213301423, 0.144956251122548, -0.0242770509986111
      ), G2M.Score = c(0.0904224391544142, 0.050148242050667, -0.0178041670730754, 
      -0.0112596867977946, -0.0519554524339088, -0.0136533184257381
      ), Phase = structure(c(2L, 2L, 3L, 3L, 3L, 1L), .Label = c("G1", 
      "G2M", "S"), class = "factor"), old.ident = structure(c(5L, 
      5L, 5L, 5L, 5L, 5L), .Label = c("Fibroblast", "T cell", "Macrophage", 
      "Stellate", "Acinar", "Endothelial", "Tumor", "B cell", "Mast cell", 
      "Ductal", "Islets of Langerhans"), class = "factor")), row.names = c("untreated_TTTGGTTGTCTAATCG-18", 
  "untreated_TTTGGTTTCCCGAGGT-18", "untreated_TTTGTTGAGAACTGAT-18", 
  "untreated_TTTGTTGAGCTCGGCT-18", "untreated_TTTGTTGAGTGCCTCG-18", 
  "untreated_TTTGTTGCACGGTGCT-18"), class = "data.frame")
  </code></pre>
  
  <p>Been using this code to generate the previous graph.</p>
  
  <pre><code>ggplot(CC, aes(x = Condition, y = Percent, fill = Cell_Cycle))+
    geom_bar(stat = "identity")+
    geom_text(aes(label = paste(round(Percent,2),"%")), position = position_stack(vjust =  0.5))
  </code></pre>
  
  <p><a href="https://i.sstatic.net/fkecc.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/fkecc.png" alt="cell cycle by condition"></a></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-20T23:25:51.180Z
Comments.Text: What is `DF` ? Can you provide an image of the graph when you are comparing cell cycle in each condition ? Your example "all.combined@metadata" contains only "untreated" conditions, maybe you should update your example to provide few lines of treated conditions too.
Comments.UserDisplayName: dc37
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-20T23:35:05.240Z
Comments.Text: `geom_bar(stat = "identity")` is rarely correct, as bar plots should be used for count-level data only. If you want to use a value, then use `geom_col()` instead - this is explained in the help for `geom_bar`.
Comments.UserDisplayName: gringer
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-20T23:52:36.770Z
Comments.Text: You don't have to post all the metadata file, but few lines of each condition "treated / untreated" could be helpful.
Comments.UserDisplayName: dc37
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-20T23:52:54.647Z
Comments.Text: @dc37 I posted the head and tail of the dataset.
Comments.UserDisplayName: user6774
Tags: <single-cell><ggplot2>
Title: Stacked barplot for single cell analysis
ThreadText: # Stacked barplot for single cell analysis
  
  (Asked by: username_0 on 2020-01-20)
  
  I want to create a stacked bar-graph with different cell cycles for each cell type within each condition. I have uploaded the file for it. Don't know how to go about it as now I have another condition (cell cycle). I was able to do the bar graph comparing cell cycle per condition but now I want to compare cell type and cell cycle in each condition
  
  output of dput(head(all.combined@metadata))
  
  ```
  structure(list(orig.ident = c("treated", "treated", "treated", 
      "treated", "treated", "treated"), nCount_RNA = c(1892, 307, 1348, 
      3699, 4205, 4468), nFeature_RNA = c(960L, 243L, 765L, 1612L, 
      1341L, 1644L), percent.mt = c(0.211416490486258, 1.62866449511401, 
      4.45103857566766, 4.4065963773993, 0.0713436385255648, 3.87197851387645
      ), RNA_snn_res.0.5 = structure(c(11L, 11L, 5L, 6L, 11L, 13L), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
      "13", "14", "15", "16", "17", "18", "19"), class = "factor"), seurat_clusters = structure(c(11L, 11L, 5L, 6L, 11L, 13L), .Label = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"), class = "factor"), S.Score = c(0.476893835992198, -0.0200784617568548, -0.0335915198305002, -0.0247184276246385, 0.010785196602457, 0.0190008903712199), G2M.Score = c(0.204441469200986, 0.173804859670862, -0.0313235510969097, -0.0376796363661889, -0.0559526905696905, -0.0122031631356698), Phase = structure(c(3L, 2L, 1L, 1L, 3L, 3L), .Label = c("G1", "G2M", "S"), class = "factor"), old.ident = structure(c(7L,7L, 1L, 4L, 7L, 9L), .Label = c("Fibroblast", "T cell", "Macrophage", "Stellate", "Acinar", "Endothelial", "Tumor", "B cell", "Mast cell", "Ductal", "Islets of Langerhans"), class = "factor")), row.names = c("treated_AAACGCTAGCGGGTTA-1", "treated_AAAGGTAAGTACAGAT-1", "treated_AAAGTGAGTTTGATCG-1", "treated_AAATGGACAAAGTGTA-1", 
      "treated_AACAAAGGTCGACTTA-1", "treated_AACAGGGTCCTAGCCT-1"), class = "data.frame")
  
  ```
  
  output of dput(tail(all.combined@metadata))
  
  ```
  structure(list(orig.ident = c("untreated", "untreated", "untreated", 
  "untreated", "untreated", "untreated"), nCount_RNA = c(901, 823, 
  1184, 1835, 1147, 1407), nFeature_RNA = c(482L, 479L, 649L, 1043L, 
  604L, 709L), percent.mt = c(1.77580466148724, 2.91616038882138, 
  4.22297297297297, 3.86920980926431, 2.0052310374891, 4.05117270788913
  ), RNA_snn_res.0.5 = structure(c(7L, 7L, 7L, 14L, 7L, 7L), .Label = c("0", 
  "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
  "13", "14", "15", "16", "17", "18", "19"), class = "factor"), 
      seurat_clusters = structure(c(7L, 7L, 7L, 14L, 7L, 7L), .Label = c("0", 
      "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 
      "12", "13", "14", "15", "16", "17", "18", "19"), class = "factor"), 
      S.Score = c(-0.0320858200243315, 0.0304725660342869, 0.0215996091745327, 
      0.0384166213301423, 0.144956251122548, -0.0242770509986111
      ), G2M.Score = c(0.0904224391544142, 0.050148242050667, -0.0178041670730754, 
      -0.0112596867977946, -0.0519554524339088, -0.0136533184257381
      ), Phase = structure(c(2L, 2L, 3L, 3L, 3L, 1L), .Label = c("G1", 
      "G2M", "S"), class = "factor"), old.ident = structure(c(5L, 
      5L, 5L, 5L, 5L, 5L), .Label = c("Fibroblast", "T cell", "Macrophage", 
      "Stellate", "Acinar", "Endothelial", "Tumor", "B cell", "Mast cell", 
      "Ductal", "Islets of Langerhans"), class = "factor")), row.names = c("untreated_TTTGGTTGTCTAATCG-18", 
  "untreated_TTTGGTTTCCCGAGGT-18", "untreated_TTTGTTGAGAACTGAT-18", 
  "untreated_TTTGTTGAGCTCGGCT-18", "untreated_TTTGTTGAGTGCCTCG-18", 
  "untreated_TTTGTTGCACGGTGCT-18"), class = "data.frame")
  
  ```
  
  Been using this code to generate the previous graph.
  
  ```
  ggplot(CC, aes(x = Condition, y = Percent, fill = Cell_Cycle))+
    geom_bar(stat = "identity")+
    geom_text(aes(label = paste(round(Percent,2),"%")), position = position_stack(vjust =  0.5))
  
  ```
  
  [![cell cycle by condition](https://i.sstatic.net/fkecc.png)](https://i.sstatic.net/fkecc.png)
  
  **Tags:** single-cell, ggplot2
  
  > **Comments:**
  > **username_1**: What is `DF` ? Can you provide an image of the graph when you are comparing cell cycle in each condition ? Your example "all.combined@metadata" contains only "untreated" conditions, maybe you should update your example to provide few lines of treated conditions too.  
  > **username_2**: `geom_bar(stat = "identity")` is rarely correct, as bar plots should be used for count-level data only. If you want to use a value, then use `geom_col()` instead - this is explained in the help for `geom_bar`.  
  > **username_1**: You don't have to post all the metadata file, but few lines of each condition "treated / untreated" could be helpful.  
  > **username_0**: @username_1 I posted the head and tail of the dataset.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-01-21. Score: 1
  
  Based on your example, you can use count for each phase of cell cycle for each cell types in each condition. Here, I'm using `dplyr` package to do that but most likely, you could have the same output using various methods:
  
  ```r
  DF <- rbind(dfhead, dftail)
  
  library(dplyr)
  DF_Count <- DF %>%group_by(orig.ident,Phase,old.ident) %>%
    count() %>%
    ungroup() %>%
    group_by(orig.ident,old.ident) %>%
    mutate(Freq = n/sum(n)*100)
  
  # A tibble: 8 x 5
  # Groups:   orig.ident, old.ident [5]
    orig.ident Phase old.ident      n  Freq
    <chr>      <fct> <fct>      <int> <dbl>
  1 treated    G1    Fibroblast     1 100  
  2 treated    G1    Stellate       1 100  
  3 treated    G2M   Tumor          1  33.3
  4 treated    S     Tumor          2  66.7
  5 treated    S     Mast cell      1 100  
  6 untreated  G1    Acinar         1  16.7
  7 untreated  G2M   Acinar         2  33.3
  8 untreated  S     Acinar         3  50  
  
  ```
  
  As you can see, `DF_Count` has the frequency for each cell types of each phase of the cell cycle in function of the condition. We can use `DF_Count` to get the following plot. Using `facet_wrap`, you can create two panels based on the condition column and thus represent the cell cycle of each cell types in function of the treatment condition:
  
  ```r
  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)
  
  ```
  
  [![enter image description here](https://i.sstatic.net/hcjcz.png)](https://i.sstatic.net/hcjcz.png)
  
  Does it look what you are expecting ?
  
  NB: `DF` here is the addition of your head and tail of your `all.combined` dataframe.
  
  > **Comments:**
  > **username_0**: How about the other cell types? Can I just make `DF <- all.combined@metadata`? That way all the cell types are included?  
  > **username_1**: It should work. Here the graph represent only the portion of data you have provided. If you think this answer was useful, do not hesitate to vote or validate it. Learn more here: https://bioinformatics.stackexchange.com/help/someone-answers  
  
  ---
  
  