Adjust position of stats in ggpubr

2.3k views Asked by At

I have the following code:

 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)

How can I change the position of the stats ("R=-0.47, p=0.14") to the right? All the way to the right or to the middle?

2

There are 2 answers

0
Duck On BEST ANSWER

Try this. You can play around cor.coeff.args option to enable the position of your correlation label. Here the code:

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"))

Output:

enter image description here

Or this option to keep same original style:

#Code 2
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.5))

Output:

enter image description here

0
akrun On

We can use tidyverse syntax to do the filter and then make use of the stat_cor

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