将相关性检验结果添加到ggplot图中

9

我正在尝试创建一个ggplot图表,并添加我已经完成的相关性测试的结果。大概是这样的:

p+annotate("text",x=12.5,y=15.25,label=c(cor.test$estimate,cor.test$p.value))

无论我尝试什么,都会收到错误消息。有任何想法吗?


不是解决您问题的方法,但您是否看过corrplot/ggcorr包? - Vlo
你的错误具体是什么?如果你展示更多的代码(例如数据和完整的绘图函数),也许有人会找到解决方案。 - Linus
我已经有了一个图表,只需要添加注释。希望能够直接将它们链接到相关的测试结果。 - B.Shermeister
iriscor<-cor.test(iris$Sepal.Length,iris$Sepal.Width) ggplot(iris)+ geom_point(aes(Sepal.Length,Sepal.Width,color=Species))+ labs(title="花萼",y="宽度",x=expression(paste(bold("长度"))),col="种类")+ theme(legend.position = "none")+ theme(axis.title = element_text(face = "bold"))+ theme(axis.text = element_text(face = "bold"))+ annotate("text",x=12.5,y=15.25,label=c(iriscor$estimate,iriscor$p.value),parse=TRUE) - B.Shermeister
2个回答

20

我实际上使用ggpubr包中的stat_cor(),成功地向图表添加了统计数据细节。

library(ggpubr)
p+stat_cor(method="pearson")

4

有一个正在开发中的软件包可以帮助您完成这个任务(ggstatsplot 已经在CRAN上发布)。

以下是创建相关性图表的示例:

    ggstatsplot::ggscatterstats(data = iris, x = Sepal.Length, y = Sepal.Width)

这将生成一个类似于以下图片的图形(同样可以使用Spearman's rho(type = 'spearman')或健壮的相关性测试(type = 'robust')获取结果):

enter image description here

查看函数文档获取更多信息。

1
@B.Shermeister,您是否对这个问题的答案满意?如果是,请接受答案(stackoverflow.com/help/someone-answers),以便关闭此线程。 - Indrajeet Patil
1
我正在尝试创建一个ggplot,它将在图表上显示ttest(或corr test)的结果。 我现在使用行annotate(“text”,x = 1,y = 10,label ='atop(bold(“P-value = 0.286”))',cex = 7,parse = TRUE)。 唯一的问题是每次都必须手动更改该值。 我想在标签中插入ttest $ p.value,而不是数字。 - B.Shermeister
@B.Shermeister 使用 label='atop(bold(paste("P-value = ", ttest$p.value)))) - Farid Huseynov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接