ggpubr is a drawing package based on ggplot2. It is easier to draw a picture. You can do a few lines of code with one line of code. such as:

If scatter plot needs to calculate correlation and its corresponding p value, if ggpubr is not considered, you may need to use cor() or cor.test() function to find correlation and p value first, and then use such as + annotate("text ",x = 0.2, y = 1, label ='p.value = 0.01', size = 4) and other methods are commented on. This is more troublesome.

If you use stat_cor() in ggpubr, it saves a lot of trouble:

library(ggplot2) library(ggpubr) data("iris") p <- ggplot(data = iris, aes(x = Sepal.Width, y = Petal.Width, color = Species)) + geom_point() + geom_smooth(method = lm) + scale_color_manual(values = c('#FF7400','#009999','#3914AF')) + labs(title ='iris') + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + stat_cor(method ='pearson', aes(x = Sepal.Width, y = Petal.Width, color = Species)) p

To generate:

cor

If you do not specify a location, it will appear in the upper left corner by default. It can be adjusted by label.x and label.y. Change the final stat_cor() to stat_cor(method ='pearson', aes(x = Sepal.Width, y = Petal.Width, color = Species), label.x = 3.8):

cor2

p.digits and r.digits can modify the reserved digits of p value and r. The default is two digits.

Welcome to pay attention~

Shengxin programming daily

Reference: https://cloud.tencent.com/developer/article/1607927 ggpubr plus correlation coefficient and p-value-Cloud + Community-Tencent Cloud