其他
跟着Nature学绘图——散点图+拟合曲线+分面!
点击上方
“科研后花园”
关注我们
代码如下:
1、设置工作目录并加载R包:
rm(list=ls())#clear Global Environment
setwd('D:/散点图+拟合曲线+分面')#设置工作路径
#加载R包
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(ggpmisc) # Miscellaneous Extensions to 'ggplot2'
library(ggpubr) # 'ggplot2' Based Publication Ready Plots
library(RColorBrewer) # ColorBrewer Palettes
library(grid) # The Grid Graphics Package
library(scales) # Scale Functions for Visualization
2、加载绘图数据
df <- read.table(file="data.txt",sep="\t",header=T,check.names=FALSE)
3、自定义颜色:
#自定义颜色
col<-c("#1e90ff", "#cb78a6","#e6cf00","#fe0076","#28e5a1")
#构建背景色
color1 <- colorRampPalette(brewer.pal(11,"PiYG"))(30)
4、绘制散点图并添加拟合曲线:
p1 <- ggplot()+
geom_point(df,mapping=aes(x,y,color=group),size=2,alpha=0.7)+
geom_smooth(df,mapping=aes(x,y),method = "lm",
formula = y ~ x,se=F,
linetype=1,alpha=0.5)+
stat_cor(df,mapping=aes(x,y),method = "pearson",label.x = 0, label.y = 15,color="black",size=4)+
stat_poly_eq(df,mapping=aes(x,y,label = ..eq.label..),
formula = y ~ x, parse = T,color="black",
geom = "text",label.x = 0,label.y = 10, hjust = 0,size=4)+
scale_color_manual(values = col)+
theme_classic()+
theme(axis.text=element_text(color='black',size=12),
legend.text = element_text(color='black',size=12),
legend.title = element_blank(),
legend.position = c(0.95,0.85),
legend.background = element_blank())+
labs(x=NULL,y=NULL)
p1
5、分面:
p2 <- ggplot()+
geom_point(df,mapping=aes(x,y,color=group),size=2,alpha=0.5)+
geom_smooth(df,mapping=aes(x,y,color=group),method = "lm",
formula = y ~ x,se=F,
linetype=1)+
stat_cor(df,mapping=aes(x,y),color="black",method = "pearson",label.x = 0, label.y = 15,size=4)+
stat_poly_eq(df,mapping=aes(x,y,label = ..eq.label..),color="black",
formula = y ~ x, parse = T,
geom = "text",label.x = 0,label.y = 10, hjust = 0,size=4)+
scale_color_manual(values = col)+
theme_classic()+
theme(axis.text=element_text(color='black',size=12),
legend.text = element_text(color='black',size=12),
legend.title = element_blank())+
labs(x=NULL,y=NULL)+
facet_grid(~group, scales = "fixed")+
theme(legend.position = "none",
strip.background=element_blank(),
strip.text = element_blank())
p2
6、合并图形:
#拼图
cowplot::plot_grid(p2,p1,ncol = 1)
#添加背景
grid.raster(alpha(color1, 0.1),
width = unit(1, "npc"),
height = unit(1,"npc"),
interpolate = T)
温馨提示
如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。