Reputation: 1353
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?
Upvotes: 0
Views: 2831
Reputation: 887691
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)
Upvotes: 2
Reputation: 39613
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:
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:
Upvotes: 3