ggpubr是基于ggplot2的一個作圖包,在畫圖的時候比較省事,用一行代碼可以做幾行代碼的事情。比如:
如果做scatter plot需要計(jì)算correlation及其對應(yīng)p值的話,如果不考慮ggpubr的話,可能需要先用cor()或者cor.test()函數(shù)求出correlation及p值,然后在用如 + annotate("text",x = 0.2, y = 1, label = 'p.value = 0.01', size = 4)等方法注釋上去。這樣屬實(shí)比較麻煩。
如果用ggpubr中的stat_cor()則省事很多:
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
即可生成:

cor
如果不指定位置,那么默認(rèn)會出現(xiàn)在左上角??梢酝ㄟ^label.x和label.y調(diào)整。將最后的stat_cor()改為stat_cor(method = 'pearson', aes(x = Sepal.Width, y = Petal.Width, color = Species), label.x = 3.8):

cor2
p.digits和r.digits可以修改p值和r的小數(shù)保留位數(shù)。默認(rèn)是兩位。
歡迎關(guān)注~