gpt4 book ai didi

r - 在 ggplot2/ggpubr 中显示成对箱线图之间的 Spearman 相关性

转载 作者:行者123 更新时间:2023-12-02 14:09:44 24 4
gpt4 key购买 nike

我有一个包含三个箱线图的图,我需要使用 ggplot2 或 ggpubr 显示箱线图对之间的 Spearman 相关性。

很容易绘制类似的图,但具有成对的 p 值。例如,

library(ggpubr)
data("ToothGrowth")
df <- ToothGrowth
p <- ggboxplot(df, x = "dose", y = "len",
color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
add = "jitter", shape = "dose")
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p + stat_compare_means(comparisons = my_comparisons)

enter image description here

如何制作类似的图,但使用 Spearman 相关性而不是 p 值?

感谢任何帮助

最佳答案

dose_0.5 <- df[df$dose == 0.5,]
dose_1 <- df[df$dose == 1,]
dose_2 <- df[df$dose == 2,]

cor.test(dose_1$len,dose_2$len)
cor.test(dose_0.5$len,dose_2$len)
cor.test(dose_1$len,dose_0.5$len)

p <- ggboxplot(df, x = "dose", y = "len",
color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
add = "jitter", shape = "dose")
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p + geom_signif(test = "wilcox.test",
comparisons = list( c("0.5", "1"), c("1", "2"), c("0.5", "2") ),
vjust = 0,
textsize = 4,
size = .5,
step_increase = .08,
annotations = c(cor.test(dose_1$len,dose_0.5$len)$estimate,
cor.test(dose_1$len,dose_2$len)$estimate,
cor.test(dose_0.5$len,dose_2$len)$estimate))

enter image description here

dfToothGrowth 数据集。如果你真的想要这样的东西,只需用相关系数替换p.value,也许你需要编辑图例或标题来解释注释是相关系数。

顺便说一下,你可以在annotation = ...中添加round(),这样相关系数就不会那么长了。

关于r - 在 ggplot2/ggpubr 中显示成对箱线图之间的 Spearman 相关性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51810586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com