0
$\begingroup$

I am trying to plot a box plot/stacked bar graph with error bars.

output of dput(DF)

`structure(list(Cell_Type = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("Fibroblast", "T cell", 
"Macrophage", "Tumor", "Islets of Langerhans", "Endothelial", 
"B cell"), class = "factor"), Condition = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("treated", 
"untreated"), class = "factor"), Freq = c(6051L, 1892L, 1133L, 
657L, 116L, 868L, 832L, 5331L, 3757L, 1802L, 835L, 287L, 704L, 
256L), Percent = c(52.3941466793662, 16.3823707680319, 9.8103731924842, 
5.68880422547407, 1.00441596675037, 7.51580223395965, 7.20408693393367, 
41.0962072155412, 28.9623805118717, 13.8914585260561, 6.43694110391613, 
2.21245760098674, 5.42707369719396, 1.97348134443417)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), groups = structure(list(
    Condition = structure(1:2, .Label = c("treated", "untreated"
    ), class = "factor"), .rows = list(1:7, 8:14)), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))`

Gives me the following dataset.

       Var1      Var2 Freq
1   Fibroblast   treated 6051
2       T cell   treated 1892
3   Macrophage   treated 1133
4     Stellate   treated  893
5       Acinar   treated  148
6  Endothelial   treated  868
7   Fibroblast untreated 5331
8       T cell untreated 3757
9   Macrophage untreated 1802
10    Stellate untreated 1061
11      Acinar untreated 2786
12 Endothelial untreated  704

library(dplyr)
DF <- df %>% rename(Cell_Type = Var1, Condition = Var2) %>%
  group_by(Condition) %>% 
  mutate(Percent = Freq / sum(Freq)*100)

# A tibble: 12 x 4
# Groups:   Condition [2]
   Cell_Type   Condition  Freq Percent
   <fct>       <fct>     <int>   <dbl>
 1 Fibroblast  treated    6051   55.1 
 2 T cell      treated    1892   17.2 
 3 Macrophage  treated    1133   10.3 
 4 Stellate    treated     893    8.13
 5 Acinar      treated     148    1.35
 6 Endothelial treated     868    7.90
 7 Fibroblast  untreated  5331   34.5 
 8 T cell      untreated  3757   24.3 
 9 Macrophage  untreated  1802   11.7 
10 Stellate    untreated  1061    6.87
11 Acinar      untreated  2786   18.0 
12 Endothelial untreated   704    4.56

Calculated SD/Mean with this:

my_sum_1 <- DF %>%
    group_by(Cell_Type) %>%
    summarise( 
        n=n(),
        mean=mean(Freq),
        sd=sd(Freq)
    ) %>%
    mutate( se=sd/sqrt(n))  %>%
    mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

Then plotted with this:

ggplot(my_sum_1) +
    geom_bar( aes(x=Cell_Type, y=mean), stat="identity", fill="forestgreen", alpha=0.5) +
    geom_errorbar( aes(x=Cell_Type, ymin=mean-ic, ymax=mean+ic), width=0.4, colour="orange", alpha=0.9, size=1.5) +
    ggtitle("Cell Types")

I want to plot based on condition. Not sure how I would do that. I tried the fill command but doesn't seem to work as the object is not found, I can't seem to add it back to the data set. How do I include the condition in the bar chart?

When I'm calculating sd, I think it's combining the cell type value into one, rather than taking it individually. How do I change that?

dput(head(all@meta.data))

structure(list(orig.ident = c("PDAC", "PDAC", "PDAC", "PDAC", 
"PDAC", "PDAC"), nCount_RNA = c(7945, 7616, 7849, 5499, 853, 
1039), nFeature_RNA = c(2497L, 2272L, 2303L, 2229L, 509L, 588L
), percent.mt = c(2.63058527375708, 3.7421218487395, 4.9433048796025, 
0.490998363338789, 0.234466588511137, 0.192492781520693), RNA_snn_res.0.5 = structure(c(5L, 
5L, 5L, 5L, 1L, 6L), .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(5L, 
5L, 5L, 5L, 1L, 6L), .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.0171539298241524, 
-0.00599651468836135, 0.011058574403656, -0.0424413740104151, 
0.03549521163095, -0.0243347616753686), G2M.Score = c(-0.0591387992008088, 
0.0253275642795205, -0.0512402869839816, -0.0239076248512967, 
-0.0263164126515597, -0.0339086517371782), Phase = structure(c(3L, 
2L, 3L, 1L, 3L, 1L), .Label = c("G1", "G2M", "S"), class = "factor"), 
    old.ident = structure(c(1L, 1L, 1L, 1L, 1L, 3L), .Label = c("Fibroblast", 
    "T Cell", "Endothelial", "Tumor", "Stellate", "Macrophage", 
    "B Cell", "Mast Cell", "Acinar", "Endocrine", "Exocrine"), class = "factor")), row.names = c("PDAC_AAACCCAGTCGGCTAC-1", 
"PDAC_AAACGAAGTCCAGGTC-1", "PDAC_AAAGAACAGCAAGTGC-1", "PDAC_AAAGAACAGGATGAGA-1", 
"PDAC_AAAGTGACATCCGTTC-1", "PDAC_AACAACCAGGAAGTAG-1"), class = "data.frame")
$\endgroup$
0
$\begingroup$

Your issue is coming by the fact that when you calculate mean, se, ... you removed the Condition column, here is your my_sum_1 results:

# A tibble: 7 x 6
  Cell_Type                n  mean    sd    se     ic
  <fct>                <int> <dbl> <dbl> <dbl>  <dbl>
1 Fibroblast               2 5691   509. 360    4574.
2 T cell                   2 2824. 1319. 932.  11849.
3 Macrophage               2 1468.  473. 334.   4250.
4 Tumor                    2  746   126.  89.0  1131.
5 Islets of Langerhans     2  202.  121.  85.5  1086.
6 Endothelial              2  786   116.  82    1042.
7 B cell                   2  544   407. 288    3659.

To calculate the mean, sd, ... from each cell types in each conditions, you need to group by Cell_Type and Condition by writing:

my_sum_1 <- DF %>%
  group_by(Cell_Type, Condition) %>%
  summarise( 
    n=n(),
    mean=mean(Freq),
    sd=sd(Freq)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

But now, your table looks like:

# A tibble: 14 x 7
# Groups:   Cell_Type [7]
   Cell_Type            Condition     n  mean    sd    se    ic
   <fct>                <fct>     <int> <dbl> <dbl> <dbl> <dbl>
 1 Fibroblast           treated       1  6051    NA    NA    NA
 2 Fibroblast           untreated     1  5331    NA    NA    NA
 3 T cell               treated       1  1892    NA    NA    NA
 4 T cell               untreated     1  3757    NA    NA    NA
 5 Macrophage           treated       1  1133    NA    NA    NA
 6 Macrophage           untreated     1  1802    NA    NA    NA
 7 Tumor                treated       1   657    NA    NA    NA
 8 Tumor                untreated     1   835    NA    NA    NA
 9 Islets of Langerhans treated       1   116    NA    NA    NA
10 Islets of Langerhans untreated     1   287    NA    NA    NA
11 Endothelial          treated       1   868    NA    NA    NA
12 Endothelial          untreated     1   704    NA    NA    NA
13 B cell               treated       1   832    NA    NA    NA
14 B cell               untreated     1   256    NA    NA    NA

Because in your initial dataset, you only have one value for each cell type in each condition, so you can calculate sd, sem. You need to first merge your replicates into your dataframe in order to be able to calculate your sem.

Then, you will be able to do:

library(ggplot2)
ggplot(my_sum_1, aes(x=Cell_Type, y=mean, fill = Condition)) +
  geom_col(alpha=0.5) +
  geom_errorbar( aes(ymin=mean-ic, ymax=mean+ic), width=0.4, colour="orange", alpha=0.9, size=1.5) +
  ggtitle("Cell Types")

Hope it helps you to figure it out what is your issue with your current data.

$\endgroup$
6
  • $\begingroup$ How do I merge a dataframe? These aren't replicates though. For each cell type, I have two conditions. $\endgroup$
    – mmpp
    Jan 27 '20 at 12:46
  • $\begingroup$ If you made replicates of your experiment, you should have multiple tables similar to DF (one for each replicate) with the count of each cell types per conditions. If so, you can merge all of them (take a look for function ?merge() or ?full_join() from dplyr (dplyr.tidyverse.org/reference/join.html)). If all of your tables have the same format and colnames, you can consider using rbind (and adding first a condition column to each table corresponding to the replicate number). $\endgroup$
    – dc37
    Jan 27 '20 at 16:16
  • $\begingroup$ I uploaded the question with the dataset before I calculated the frequencies and percentages. $\endgroup$
    – mmpp
    Jan 27 '20 at 18:58
  • $\begingroup$ I don't think it changes anything. This table does not represent replicates right ? Do you have replicates for this experiment ? What I mean by replicates is that you run a scRNAseq several times on your biological samples, and you identify the percentage of cell types in each of this runs. How do you design your experiments ? Can you describe it a little bit ? (no need to know exact parameters, just what you are trying to do and what materials did you use at the beginning) $\endgroup$
    – dc37
    Jan 27 '20 at 19:02
  • $\begingroup$ I run one sample on the 10x chromium box. This data set contains samples from treated and untreated patients. 15 treated and 16 untreated samples. I run all the raw files through Seurat. $\endgroup$
    – mmpp
    Jan 27 '20 at 22:32

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.