【问题标题】:Only display correlation coefficient in ggplot stat_cor仅在 ggplot stat_cor 中显示相关系数
【发布时间】:2023-03-22 07:20:02
【问题描述】:

如何只显示 ggpubr::stat_cor 中的相关系数,而不显示 p 值? stat_cor 中似乎没有一个参数来指定一个或另一个统计数据。还有其他一些创造性的解决方法吗?

【问题讨论】:

  • ggplot2 没有stat_cor。您使用的是哪个附加包?也许ggpubr
  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。
  • 不,我在 ggplot 中使用 geom_point,而不是 ggpubr。
  • 呼应格雷戈,没有ggplot2::stat_cor; ggplot2 包没有这样的功能,而ggpubr 有。
  • 您可以尝试在stat_cor 中包含:aes(label = ..r.label..) 吗?这应该只显示 R 值。 github.com/kassambara/ggpubr/issues/188

标签: r ggplot2 pearson-correlation


【解决方案1】:

遵循 Ben 的解决方案 - 我包括一个可重现的示例。 首先让我们用一个简单的例子:

library('ggplot2')
library('ggpubr')
data(iris)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width  )) +
geom_point() + theme_bw() +
stat_cor(method = "pearson")

现在,您只想显示相关系数,即 R。

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width  )) +
geom_point() + theme_bw() +
stat_cor(method = "pearson", aes(label = ..r.label..))

其实你也可以独立计算R值,并使用annotate在图中添加一个文本框(来自iris数据库的例子):

round(cor(iris$Sepal.Length, iris$Sepal.Width),2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多