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([email protected]))
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")