R语言编程
20220325_散点图拟合曲线绘制
Song Wei
2023年3月12日 05:43
294
20220325_散点图拟合曲线绘制
散点图上绘制拟合曲线的作用是用一条曲线来描述散点图的整体趋势,从而更好地理解和分析数据。具体来说,散点图通常用于展示两个变量之间的关系,拟合曲线则可以更好地揭示这种关系的大致趋势和线性程度。通过绘制拟合曲线,可以更好地理解两个变量之间的关系是正相关、负相关还是无关。同时,拟合曲线还可以帮助预测未来的数据趋势,从而帮助做出更准确的决策。要在ggplot中绘制散点图拟合曲线,可以使用ggplot的geom_smooth函数。这个函数可以在散点图上绘制平滑的拟合曲线。
绘制方法:
install.packages("ggplot2")
library(ggplot2)
data1<-read.table("./test_data.txt",header=T,sep="\t",check.names=F)
############ result1 ############
ggplot(data1,aes(x=time,y=value,colour=method)) +
theme_classic() +
geom_smooth(aes(colour=method),span = 0.6,level = 0.7,alpha = 0.2) +
scale_color_manual(values = c('#000000','#FB02FE')) +
theme(axis.line = element_line(size = 0.8, colour = "black")) +
theme(axis.title.x = element_text(size = 20, color = "black", face = "bold")) +
theme(axis.text.x= element_text(size=15, color="black", face= "bold")) +
xlab("Zeitgeber time") +
theme(axis.title.y = element_text(size = 20, color = "black", face = "bold")) +
theme(axis.text.y= element_text(size=15, color="black", face= "bold")) +
ylab("per Luc") +
scale_x_continuous(limits=c(0,48),breaks=seq(0,48,4)) +
#scale_y_continuous(limits=c(0.5,1.5),breaks=seq(0.5,1.5,0.25))
theme(legend.title=element_blank()) +
theme(legend.text=element_text(size=18)) +
theme(legend.position=c(0.19,0.98),legend.justification=c(1,1)) +
#theme(legend.background=element_rect(colour = 'black',size = 1)) +
theme(axis.ticks = element_line(size = 1),axis.ticks.length.y = unit(.25, "cm"), axis.ticks.length.x = unit(.25, "cm"),axis.text.x = element_text(angle=0, vjust=0.5)) +
theme(legend.title=element_blank())
############ result2 ############
ggplot(data1,aes(x=time,y=value,linetype=method)) +
theme_classic() +
geom_smooth(aes(colour=method),span = 0.6,level = 0.7,alpha = 0.2) +
scale_color_manual(values = c('#000000','#7F7F7F')) +
theme(axis.line = element_line(size = 0.8, colour = "black")) +
theme(axis.title.x = element_text(size = 20, color = "black", face = "bold")) +
theme(axis.text.x= element_text(size=15, color="black", face= "bold")) +
xlab("Zeitgeber time") +
theme(axis.title.y = element_text(size = 20, color = "black", face = "bold")) +
theme(axis.text.y= element_text(size=15, color="black", face= "bold")) +
ylab("per Luc") +
scale_x_continuous(limits=c(0,48),breaks=seq(0,48,4)) +
theme(legend.text=element_text(size=18)) +
theme(legend.position=c(0.19,0.98),legend.justification=c(1,1)) +
#theme(legend.background=element_rect(colour = 'black',size = 1)) +
theme(legend.title=element_blank()) +
theme(axis.ticks = element_line(size = 1),axis.ticks.length.y = unit(.25, "cm"), axis.ticks.length.x = unit(.25, "cm"),axis.text.x = element_text(angle=0, vjust=0.5))
result1:
result2:
测试数据:test_data.txt
method time value
siCtrl 0 1
siCtrl 0 1
siCtrl 0 1
siCtrl 0 1
siCtrl 0 1
SiTUT 0 0.839921967
SiTUT 0 0.889884905
SiTUT 0 0.762835278
SiTUT 0 0.417008604
SiTUT 0 0.248
siCtrl 2 1.080020612
siCtrl 2 1.11332448
siCtrl 2 0.999678035
siCtrl 2 1.021133907
siCtrl 2 1.002926829
SiTUT 2 0.859982332
SiTUT 2 0.935185185
SiTUT 2 0.795774822
标签:
rstudio
北京 天气
晴
3℃