如下:

library(ggplot2)
library(ggpubr)

# 创建数据集
set.seed(123)
df <- data.frame(x = rnorm(100), y = rnorm(100, 5), group = rep(c("A", "B"), 50))

# 绘制散点图并添加cor和p值
p <- ggplot(df, aes(x = x, y = y, color = group)) +
  geom_point() +
  stat_cor(method = "pearson", label.x = 2, label.y = 8)

# 修改cor和p值字体颜色
p + theme(legend.position = "none") +
  ggtitle("Correlation between x and y") +
  theme(plot.title = element_text(hjust = 0.5)) +
  annotate("text", x = 2, y = 7, label = "r = -0.42", size = 4, color = "blue") +
  annotate("text", x = 2, y = 6, label = "p = 0.0002", size = 4, color = "blue")

在这里,我们使用了ggpubr包中的stat_cor函数来添加cor和p值标签。然后,我们使用annotate函数来修改cor和p值的字体颜色为蓝色(blue)


原文地址: https://www.cveoy.top/t/topic/ffiA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录