图形展示
代码展示
library(showtext)
showtext_auto()
par(mai = c(0.6,0.6,0.4,0.4),cex = 0.7)
set.seed(2)
x<-rnorm(200)
y<-1+2*x+rnorm(200)
d<-data.frame(x,y)
plot(d,xlab = "x = 自变量",ylab = "y = 因变量")
grid(col = "grey60")
axis(side = 4,col.ticks = "blue",lty = 1)
polygon(d[chull(d),],lty = 6,lwd = 1,col = "lightgreen")
points(d)
points(mean(x),mean(y),pch = 19,cex = 5,col = 2)
abline(v = mean(x),h = mean(y),lty = 2,col = "grey30")
abline(lm(y~x),lwd = 2,col = 2)
lines(lowess(y~x,f = 1/6),col = 4,lwd = 2,lty = 6)
segments(-0.8,0,-1.6,3.3,lty = 6,col = "blue")
arrows(0.45,-2.2,-0.8,-0.6,code = 2,angle = 25,length = 0.06,col = 2)
text(-2.2,3.5,labels = expression("拟合直线"),adj = c(-0.1,0.02),col = 4)
rect(0.4,-1.6,1.8,-3,col = "pink",border = "grey60")
mtext(expression(hat(y)==hat(beta)[0]+hat(beta)[1]*x),cex = 0.9,side = 1,line = -3.5,adj = 0.8)
legend("topleft",inset = 0.02,legend = c("拟合直线", "拟合曲线"),
col = c(2,4), cex = 0.8,fill = c("red","blue"),box.col = "grey60",ncol = 1,
lty = c(1, 6), pch = c(9,8),bg = "snow3",text.width = 0.6)
title("散点图及拟合直线和曲线 \n 并为图形增加新的元素",cex.main = 1,font.main = 4)
box(col = 4,lwd = 2)