ggplot2 中的 stat_cor 函数:在两行上打印 R 值和 p 值
Posted
技术标签:
【中文标题】ggplot2 中的 stat_cor 函数:在两行上打印 R 值和 p 值【英文标题】:stat_cor function in ggplot2: Print R and p-values on two lines 【发布时间】:2020-01-11 02:23:09 【问题描述】:是否可以将相关值和 p 值放在两行而不是默认的逗号分隔:
default:
R=0.8, p=0.004
want:
R=0.8
p=0.004
【问题讨论】:
【参考方案1】:stat_cor
函数来自ggpubr
库(不是基础ggplot2
)。不管怎样,the documentation for the function 有你的答案,那就是在stat_cor
中使用label.sep=
参数。您可以将其设置为 "\n"
以添加一个新的行字符作为分隔符并在两行上获取标签。请参阅文档中的示例进行调整:
library(ggplot2)
library(ggpubr)
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)
sp <- ggscatter(df, x = "wt", y = "mpg",
add = "reg.line", # Add regressin line
add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line
conf.int = TRUE # Add confidence interval
)
# Add correlation coefficient
sp + stat_cor(method = "pearson", label.x = 3, label.y = 30, label.sep='\n')
【讨论】:
以上是关于ggplot2 中的 stat_cor 函数:在两行上打印 R 值和 p 值的主要内容,如果未能解决你的问题,请参考以下文章