image.png
1. 作出散点图
1.1 数据准备和读取:
-
数据准备(存于excel表里):
x y
0.5 0.8
0.6 1.2
0.5 1.5
0.6 2.19
0.5 1.21
-
读取数据
library(readxl)
d<-read_xlsx(file.choose())
head(d)
image.png
-
转为dataframe:
df<-as.data.frame(d)
head(df)
image.png
-
1.2 绘制散点图:
library(ggplot2)
p <- ggplot(data = df, aes(x = x, y = y)) + geom_point()
p
image.png
2. 作出拟合曲线
-
绘制对应的拟合曲线:
p1<-p+geom_smooth(method = "lm", se=FALSE, color="red", formula = y ~ x)
p1
image.png
3. 显示公式
-
显示公式:
p2<-p1+ geom_text(x = 75, y = 90, label = labels[1], parse = TRUE, check_overlap = TRUE )
p2
image.png
-
显示R-2(即R平方值):
p3<-p2+ geom_text(x = 75, y = 70, label = labels[2], parse = TRUE, check_overlap = TRUE )
p3
image.png
4. 修饰格式:
-
去掉灰色背景
p4<-p3+ theme_bw()
p4
image.png
-
在图片上添加文字等:
p5<-p4+annotate(geom="text", x=20, y=280, label="Out-of-sample R^2=0.045 ", color="Black",fontface="bold")
p5
image.png
-
更改横纵坐标轴名称:
p6<-p5+ggtitle("a") + xlab("mShannon diversity") + ylab("Observed Shannon diversity")
p6
image.png
-
删除ticks:
p7<-p6+theme(axis.ticks = element_blank())
p7
image.png
-
删除grids网格线:
p8<-p7+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = element_line(colour = "black"))
p8
image.png
-
删除边界线,但是留着坐标轴线:
p9<-p8+ theme(panel.border = element_blank())
p9
image.png
-
图上的文字的补充:
p10<-p9+ annotate(geom="text", x=20, y=255, label="Pearson's R=0.68", color="Black",fontface="bold")+annotate(geom="text", x=20, y=230, label="P=3.21×10^-56", color="Black",fontface="bold")
p10
image.png
- 调整ticks:
p11<-p10+scale_x_continuous( breaks = seq(2,102,by=10))+scale_y_continuous( breaks = seq(2.0,352,by=50) )
p11
image.png
或者:
p11<-p10+scale_x_continuous( breaks = seq(2,102,by=10.5))+scale_y_continuous( breaks = seq(2,342,by=20.5) )
p11
image.png
-
用ggplotGrob函数将小图放到大图中:
pg<-ggplotGrob(p)
pnew<-p7+annotation_custom(pg,xmin = 3,xmax = 30,ymin = 100,ymax = 320)#小图
pnew
image.png
-
输入R-2,即R平方标注:
p10<-p3+annotate(geom="text", x=20, y=300, label= "Out-of-sample ")+annotate(geom="text", x=32, y=300, label=expression(R^{2}))+annotate(geom="text", x=38, y=298, label= "=0.45")
p10
image.png
-
小图置于大图,输入平方:
pnew1<-pnew+annotate(geom="text", x=50, y=320, label= "Out-of-sample ")+annotate(geom="text", x=56, y=320, label=expression(R^{2}))+annotate(geom="text", x=59, y=318, label= "=0.45")
pnew1
得到:
image.png
目标参考:
a, A plot of out-of-sample mShannon diversity versus observed Shannon diversity
https://www.nature.com/articles/s41587-019-0233-9/figures/1