3

我有一个包含三个箱线图的图,我需要使用 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)

在此处输入图像描述

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

任何帮助表示赞赏

4

1 回答 1

1
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))

在此处输入图像描述

dfToothGrowth数据集。如果您真的想要这样的东西,只需将 p.value 替换为相关人员,也许您需要编辑图例或标题以解释注释上的相关系数。

顺便说一句,您可以添加round()annotation = ...因此相关系数不会那么长。

于 2018-10-01T07:42:52.320 回答