如何获得R图中的显著性水平

jjjwad0x  于 2023-05-26  发布在  其他
关注(0)|答案(1)|浏览(104)

我想为图中的每个点添加显著性水平,然后按颜色分隔不同的显著性水平,我有以下数据:
| tframe|修剪斜率|trimSD|
| - -----|- -----|- -----|
| 5个|1.2138196| 0.05958077|
| 十个|1.2118020| 0.05748510|
| 二十个|1.1903467| 0.05208732|
| ......这是什么?|......这是什么?|......这是什么?|
为了容易地再现 Dataframe :

structure(list(tframe = c(5, 10, 15, 20, 30, 40, 50, 60, 90, 
120, 180, 240, 300, 480, 900, 1800, 3600, 28800), species = c("Barbar", 
"Barbar", "Barbar", "Barbar", "Barbar", "Barbar", "Barbar", "Barbar", 
"Barbar", "Barbar", "Barbar", "Barbar", "Barbar", "Barbar", "Barbar", 
"Barbar", "Barbar", "Barbar"), ineqMean = c(0.53104647399313, 
0.529297808367612, 0.525839141416682, 0.523942046846265, 0.52015928420821, 
0.518572691198356, 0.51511657232463, 0.516568872398126, 0.511999139793283, 
0.511939221736915, 0.508740279266857, 0.505224606372793, 0.501518089116363, 
0.499808187352849, 0.491227360987496, 0.4848439609884, 0.469461408463629, 
0.414520902218365), ineqSD = c(0.240454548676913, 0.242024196245511, 
0.243296542621781, 0.244848758571896, 0.246346408286188, 0.246855066968381, 
0.24874025357451, 0.248373465513351, 0.249666296730884, 0.249737396611995, 
0.250617197261608, 0.252642650344299, 0.254532366888185, 0.255779214784082, 
0.258922307090312, 0.261528662624724, 0.271358143507595, 0.31225287692273
), trimSlope = c(1.21381956965976, 1.21180197611039, 1.20025777497667, 
1.19034669771223, 1.18015387908413, 1.16704208709911, 1.15485377092914, 
1.15004735545657, 1.13037958715777, 1.1207384447301, 1.11122218773119, 
1.10897233423084, 1.09343555562548, 1.07597987336347, 1.0552313502822, 
1.02714867784966, 1.01874318671967, 0.997267460326558), trimSD = c(0.0595807674313792, 
0.0574850962929153, 0.0553699352357688, 0.0520873210646193, 0.0511378415143001, 
0.0480448502673866, 0.0466519422226488, 0.0454344459587024, 0.0422792122257447, 
0.0406831265017169, 0.038142436210773, 0.0379194407048026, 0.036217855188957, 
0.0327675134394643, 0.0300492301761473, 0.0279263379108475, 0.0297584207536629, 
0.0321719581188545), trimTrend = c("Strong increase (p<0.05)", 
"Strong increase (p<0.05)", "Strong increase (p<0.05)", "Strong increase (p<0.05)", 
"Strong increase (p<0.05)", "Moderate increase (p<0.05)", "Moderate increase (p<0.05)", 
"Moderate increase (p<0.05)", "Moderate increase (p<0.05)", "Moderate increase (p<0.05)", 
"Moderate increase (p<0.05)", "Moderate increase (p<0.05)", "Moderate increase (p<0.05)", 
"Uncertain", "Uncertain", "Uncertain", "Uncertain", "Uncertain"
), col = c("#006400", "#006400", "#006400", "#006400", "#006400", 
"#00CD00", "#00CD00", "#00CD00", "#00CD00", "#00CD00", "#00CD00", 
"#00CD00", "#00CD00", "#BEBEBE", "#BEBEBE", "#BEBEBE", "#BEBEBE", 
"#BEBEBE")), row.names = c(NA, -18L), class = c("tbl_df", "tbl", 
"data.frame"))

我想要一个这样的结果:

我已经尝试使用geom_signif函数,但不起作用,我有错误消息:
geom_signif()中的错误:!计算统计时出错。第4层出错。由setup_params()中的错误引起:!只能处理具有在x轴上绘制的组的数据我的代码是:使用ggplot 2和ggsignif包

p <-
ggplot(df, aes(x = tframe, y = trimSlope)) + 

geom_smooth(aes(x = tframe, y = trimSlope),
  method = "loess", col = "black"
) +
geom_errorbar(aes(ymin = trimSlope - trimSD, ymax = trimSlope + trimSD),
  width = .2
) +
geom_point(aes(x = tframe, y = trimSlope)) +
# add significance
geom_signif(comparisons = list(c(5, 10)), 
            map_signif_level=TRUE) +

# rtrim horizontal line at 1
geom_hline(yintercept = 1, linetype = "dashed", color = "red") +
# set x scale as log
scale_x_log10() +
labs(
  x = "time frame in seconds (log scale)",
  y = "mean indice values (trim slope) ± SE"
) +
# background and facetting
theme(panel.background = element_rect(
  fill = "antiquewhite1",
  colour = "blue"
))

我精确地说,我的x轴是对数刻度的。那我能做什么

r55awzrz

r55awzrz1#

我想这就是你想要的,使用你的数据中已经存在的显著性水平。如果你想计算哪些组的点是不同的,这是另一个问题。
(我不确定以这种方式对点进行分类是否明智,但这取决于你,而不是这个网站的问题。
如果你已经计算了组(就像在你的数据集中一样),那么你不需要使用geom_signif,你只需要用geom_text添加标签。
我在主要的美学Map中使用了col=col来Map所有的颜色,然后使用scale_col_identity来确保实际使用的是特定的颜色,而不是默认的色标。
为了创建标签,我用"A""B""C"等值创建了一个命名向量,并根据显著性变量的唯一水平对其进行命名。然后在geom_text的美学Map中,我可以使用这个向量将类别转换为字母。

signifLabels <- LETTERS
names(signifLabels) <- unique(df$trimTrend)

ggplot(df, aes(x = tframe, y = trimSlope,col=col)) + 
  
  geom_smooth(aes(x = tframe, y = trimSlope),
              method = "loess", col = "black"
  ) +
  scale_color_identity()+
  geom_errorbar(aes(ymin = trimSlope - trimSD, ymax = trimSlope + trimSD),
                width = .2
  ) +
  geom_point(aes(x = tframe, y = trimSlope)) +
  # add significance 
  geom_text(aes(label=signifLabels[trimTrend], y=trimSlope + trimSD), vjust=-1)+
  # rtrim horizontal line at 1
  geom_hline(yintercept = 1, linetype = "dashed", color = "red") +
  # set x scale as log
  scale_x_log10() +
  labs(
    x = "time frame in seconds (log scale)",
    y = "mean indice values (trim slope) ± SE"
  ) +
  # background and facetting
  theme(panel.background = element_rect(
    fill = "antiquewhite1",
    colour = "blue"
  ))

相关问题