R语言 ggplot 2统计柱状图(凋亡/坏死测定)

6yoyoihd  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(150)

我正在使用ggplot构建一个堆叠条形图。下面是我目前用来生成apop、nec和late中的值的总和的图的代码,但是用不同颜色的条,这样就可以知道该类别对总和的贡献有多大。
这是我忽略统计数据后得到的图表。
x1c 0d1x的数据
这是我目前所尝试的
数据表
| 代表|日本电气公司|迟|阿波普| apop |
| --|--|--|--| ------------ |
| rep1| 0.0209| 0.0334| 0.0405| 0.0405 |
| rep2| 0.0013| 0.0402| 0.0541| 0.0541 |
| rep3|零点零零七十六|0.0546| 0.0707| 0.0707 |
| rep1|零点一四七|0.0564| 0.0616| 0.0616 |
| rep2| 0.0233| 0.0596| 0.0762| 0.0762 |
| rep3| 0.0176| 0.0461|零点零五零七| 0.0507 |
| rep1| 0.01210| 0.0976|零点二三七| 0.2370 |
| rep2| 0.00860|零点零九○|零点二四一零| 0.2410 |
| rep3| 0.00760| 0.1110| 0.2890| 0.2890 |
| rep1| 0.00870|零点一一二|0.3020| 0.3020 |
| rep2| 0.01220|零点一三三|0.3270| 0.3270 |
| rep3| 0.00870|零点一一二|0.3020| 0.3020 |
上面的示例数据作为dataframe“the_data”:

the_data <- structure(list(condition = c("37_colo_control", "37_colo_control", 
"37_colo_control", "42_colo_control", "42_colo_control", "42_colo_control", 
"37_colo_mmc", "37_colo_mmc", "37_colo_mmc", "42_colo_mmc", "42_colo_mmc", 
"42_colo_mmc"), rep = c("rep1", "rep2", "rep3", "rep1", "rep2", 
"rep3", "rep1", "rep2", "rep3", "rep1", "rep2", "rep3"), nec = c(0.0209, 
0.0013, 0.0076, 0.0147, 0.0233, 0.0176, 0.0121, 0.0086, 0.0076, 
0.0087, 0.0122, 0.0087), late = c(0.0334, 0.0402, 0.0546, 0.0564, 
0.0596, 0.0461, 0.0976, 0.109, 0.111, 0.112, 0.133, 0.112), apop = c(0.0405, 
0.0541, 0.0707, 0.0616, 0.0762, 0.0507, 0.237, 0.241, 0.289, 
0.302, 0.327, 0.302)), class = "data.frame", row.names = c(NA, 
12L))

字符串
代码

library(ggpubr)
library(ggprism)
library(ggplot2)

the_data <- read.csv(**[[see table for data]]**)

factored_condition <- c("37_colo_control","37_colo_mmc","42_colo_control","42_colo_mmc")
comparisons <- list(c(factored_condition[1],factored_condition[2]),
                    c(factored_condition[1],factored_condition[3]),
                    c(factored_condition[1],factored_condition[4])
                    )

the_data %>%
  group_by(condition)

fig_bar <- ggplot(the_data, aes(x=factor(condition, levels=factored_condition)))+ 
  geom_bar(aes(y=apop+nec+late),position=position_dodge(), stat="summary", fun="mean", fill = "grey65") +
  stat_compare_means(mapping = aes(y=apop), 
                     comparisons = comparisons, paired = TRUE, method = "t.test", label="p.signif",
                     symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), 
                                        symbols = c("****","***", "**", "*", " "))) +

    geom_bar(aes(y=nec+late),position=position_dodge(), stat="summary", fun="mean", fill = "grey45") +
    stat_compare_means(mapping = aes(y=late), 
                     comparisons = comparisons, paired = TRUE, method = "t.test", label="p.signif",
                     symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), 
                                        symbols = c("****","***", "**", "*", " "))) +
  
    geom_bar(aes(y=nec),position=position_dodge(), stat="summary", fun="mean", fill = "grey 15") +
    stat_compare_means(mapping = aes(y=nec), 
                     comparisons = comparisons, paired = TRUE, method = "t.test", label="p.signif",
                     symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), 
                                        symbols = c("****","***", "**", "*", " "))) +
 
  labs(y="Percent of Cells", x="", fill = "") +
  ggtitle("Colo205") +
  scale_y_continuous(expand=c(0,0),limits = c(0,1.0), labels = scales::percent) +
  scale_x_discrete(labels=x.names) +
  theme_prism()

fig_bar


基本上,我尝试做的只是复制粘贴stat-*compare-*means部分到每个单独的条形图。但是我一直收到错误代码我不知道什么是错的,因为我把y=apop//nec//放在aes的后面。

Error in `ggsignif::geom_signif()`:
! Problem while computing stat.
i Error occurred in the 3rd layer.
Caused by error in `compute_layer()`:
! `stat_signif()` requires the following missing aesthetics: y
Backtrace:

qrjkbowd

qrjkbowd1#

有了“整理数据”的概念,事情变得更容易了,在这种情况下,它包括将数据重塑为长格式。这样做,您不必对每一列发出相同的指令,而是对每个组执行一次(从初始列名派生)。
范例:

  • 前奏
library(dplyr)
library(tidyr) ## to reshape

library(ggplot2)
library(ggpubr)
library(ggprism)

factored_condition <- c("37_colo_control","37_colo_mmc","42_colo_control","42_colo_mmc")

comparisons <- list(c(factored_condition[1],factored_condition[2]),
                    c(factored_condition[1],factored_condition[3]),
                    c(factored_condition[1],factored_condition[4])
                    )

the_data <- structure(list(condition = c("37_colo_control", "37_colo_control", 
"37_colo_control", "42_colo_control", "42_colo_control", "42_colo_control", 
"37_colo_mmc", "37_colo_mmc", "37_colo_mmc", "42_colo_mmc", "42_colo_mmc", 
"42_colo_mmc"), rep = c("rep1", "rep2", "rep3", "rep1", "rep2", 
"rep3", "rep1", "rep2", "rep3", "rep1", "rep2", "rep3"), nec = c(0.0209, 
0.0013, 0.0076, 0.0147, 0.0233, 0.0176, 0.0121, 0.0086, 0.0076, 
0.0087, 0.0122, 0.0087), late = c(0.0334, 0.0402, 0.0546, 0.0564, 
0.0596, 0.0461, 0.0976, 0.109, 0.111, 0.112, 0.133, 0.112), apop = c(0.0405, 
0.0541, 0.0707, 0.0616, 0.0762, 0.0507, 0.237, 0.241, 0.289, 
0.302, 0.327, 0.302)), class = "data.frame", row.names = c(NA, 
12L))

字符串

  • 调整形状并计算百分比:
the_data <- 
  the_data |>
  pivot_longer(cols = nec:apop, names_to = 'parameter') |>
  mutate(value_percent = prop.table(value))
> head(the_data, 4)
# A tibble: 6 x 5
  condition       rep   parameter  value value_percent
  <chr>           <chr> <chr>      <dbl>         <dbl>
1 37_colo_control rep1  nec       0.0209      0.00661 
2 37_colo_control rep1  late      0.0334      0.0106  
3 37_colo_control rep1  apop      0.0405      0.0128  
4 37_colo_control rep2  nec       0.0013      0.000411
  • 图:
ggplot(the_data, aes(x = condition, y = value_percent, group = parameter)) +
  geom_col(aes(fill = parameter), alpha = .5) +
  stat_compare_means(comparisons = comparisons,
                     paired = TRUE, method = "t.test", label="p.signif",
                     symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, Inf), 
                                        symbols = sapply(4:0, \(n) substr('****', 0, n))
                                        ),
                     step.increase = .5 ## increase vertical spacing between brackets
                     ) +
  scale_y_continuous(limits = c(0, 1), labels = scales::percent) +
  scale_fill_grey()


的数据

相关问题