1.基本图形
library(ggpubr)
ggscatter(mtcars, x = "wt", y = "mpg",
ggtheme = theme_bw())

2.添加点的标签
mtcars$name <- rownames(mtcars)
ggscatter(mtcars, x = "wt", y = "mpg",
color = "cyl", # 按‘cyl’分组
palette = "jco",
label = "name", # 添加点的标签
repel = T) # 避免过度绘制文本标签

ggscatter(mtcars, x = "wt", y = "mpg",
color = "cyl",
palette = "jco",
label = "name",
repel = TRUE,
label.select = c("Toyota Corolla", "Merc 280", "Duster 360"))

ggscatter(mtcars, x = "wt", y = "mpg",
color = "cyl",
palette = "jco",
label = "name",
font.label = c(12,'black'), # 调整标签的大小、颜色
repel = TRUE,
label.select = list(criteria = "`x` > 5 & `y` < 15"))

3.气泡图:四个变量的散点图
p = ggscatter(mtcars, x = "wt", y = "mpg",
color = "cyl",
palette = "jco",
size = "qsec", # 'qsec'的值决定点的大小
alpha = 0.5,
) +
scale_size(range = c(0.5, 13)) # 调整点的尺寸范围
ggpar(p,legend = 'right') # 调整图例位置

4.回归拟合
ggscatter(mtcars, x = "wt", y = "mpg",
size = 1.5, # 点的大小为1.5
add = "reg.line", # 添加回归直线
conf.int = TRUE, # 添加置信区间
add.params = list(color = "black", # 设置回归直线、置信域的颜色
fill = "#FF9900"),
ggtheme = theme_bw()
)+
stat_cor(method = "pearson", # 添加pearson相关系数,以及相应的P值
label.x = 3,
label.y = 30)

ggscatter(mtcars, x = "wt", y = "mpg",
size = 1.5,
add = "reg.line",
conf.int = TRUE,
add.params = list(color = "black",
fill = "#FF9900"),
ggtheme = theme_bw()
) +
stat_cor(method = "spearman",
aes(label = paste(..rr.label.., ..p.label.. ,sep = "~`,`~")), # 添加决定系数
label.x = 3
)

mtcars$cyl = as.factor(mtcars$cyl)
ggscatter(mtcars, x = "wt", y = "mpg",
add = "reg.line",
conf.int = TRUE,
color = "cyl", # 按"cyl"分组
shape = "cyl" , # 按"cyl"组改变点的形状
palette = "jco",
)+
stat_cor(aes(color = cyl), label.x = 3)

ggscatter(df, x = "wt", y = "mpg",
add = "reg.line",
color = "cyl",
shape = "cyl",
palette = "jco",
fullrange = TRUE, # 延长回归线
rug = TRUE # 添加轴须线
)+
stat_cor(aes(color = cyl), label.x = 3)

ggscatter(mtcars, x = "wt", y = "mpg",
add = "reg.line",
color = "cyl",
shape = "cyl",
palette = "jco",
facet.by = 'cyl', # 按‘cyl’分面
fullrange = TRUE, # 延长回归线
) +
stat_cor(aes(color = cyl), label.y = 40)
