R语言的ggpubr包功能还是很强大的,使用起来也没有ggplot那么复杂。ggpubr包含有十几种函数,约30种图形。
今天我们先学习使用R语言的ggpubr包绘制SCI发表级的各种散点图。我们使用R自带的数据集mtcars。
View(mtcars)#查看数据
class(mtcars$wt)#查看数据类型
class(mtcars$mpg)
wt与mpg都是连续型数据(numeric)
install.packages("ggpubr")#安装包
library(ggpubr)#加载包
ggscatter(mtcars, x = "wt", y = "mpg",
color = "black", size = 3,
add = "reg.line",
add.params = list(color = "red", fill = "lightgray"),
conf.int = TRUE,
cor.coef = TRUE,
cor.coeff.args = list(method = "pearson", label.x = 3, label.sep = "\n")
2. 分组绘制并计算相关系数,用杂志nature的配色(按照cyl分组)
mtcars$cyl1=as.factor(mtcars$cyl)#将原来数据类型是数值型的cyl转换生成类型是因子的
cyl1
class(mtcars$cyl1)
现在cyl1已经变成了分类数据(factor)
ggscatter(mtcars, x = "wt", y = "mpg",
color = "cyl1", palette = "nature",
add = "reg.line", conf.int = TRUE)+
stat_cor(aes(color = cyl), label.x = 3)
3. 在散点图添加分组椭圆,均值点,以及辐射线,用杂志jama的配色(按照cyl分组)
ggscatter(mtcars, x = "wt", y = "mpg",
color = "cyl1", shape = "cyl1",
palette ="jama",
ellipse = TRUE,
mean.point = TRUE,
star.plot = TRUE
)
4. 添加文本注释(按照cyl分组)
mtcars$name <- rownames(mtcars)
ggscatter(mtcars, x = "wt", y = "mpg",size=1,
color = "cyl1", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
label = "name",
repel = TRUE,
mean.point = FALSE,star.plot = FALSE)
5. 散点图边缘添加密度图(按照cyl分组)
ggscatterhist(
mtcars, x = "wt", y = "mpg",
color = "cyl1", size = 3, alpha = 0.6,
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
margin.params = list(fill ="cyl1", color = "black", size = 0.2)
)
6. 散点图边缘添加箱图,用杂志lancet的配色(按照cyl分组)
ggscatterhist(
mtcars, x = "wt", y = "mpg",
color = "cyl1",size = 3, alpha = 0.6,
palette = "lancet",
margin.plot = "boxplot",
ggtheme = theme_bw()
)
参考资料:
ggscatter帮助文件
ggscatterhist帮助文件
欢迎大家关注 R语言与SPSS学习笔记 公众号
网友评论