调整统计数据在ggpubr中的位置

问题描述

我有以下代码

 library(ggpubr)
 ggscatter(mtcars[mtcars$cyl==4,],x = "drat",y = "wt",add = "reg.line",add.params = list(color = "black",fill = "grey"),conf.int = TRUE,cor.method = "spearman",cor.coef = TRUE)

如何在右侧更改统计信息的位置(“ R = -0.47,p = 0.14”)?一直到右边还是中间?

解决方法

尝试一下。您可以使用cor.coeff.args选项来启用相关标签的位置。这里的代码:

library(ggpubr)
ggscatter(mtcars[mtcars$cyl==4,],x = "drat",y = "wt",add = "reg.line",add.params = list(color = "black",fill = "grey"),conf.int = TRUE,cor.method = "spearman",cor.coef = TRUE,cor.coeff.args = list(label.x = 4.7,label.sep = "\n"))

输出:

enter image description here

或使用此选项保持原始样式:

#Code 2
ggscatter(mtcars[mtcars$cyl==4,cor.coeff.args = list(label.x = 4.5))

输出:

enter image description here

,

我们可以使用tidyverse语法来做filter,然后利用stat_cor

library(dplyr)
library(ggpubr)
mtcars %>% 
  filter(cyl == 4) %>% 
  ggscatter(x = 'drat',y = 'wt',a
   dd.params =  list(color = "black",cor.method = "spearman") +
   stat_cor(label.x.npc = 0.8,label.y.npc = 0.5)