a<-read.csv("C:/Users/panda/Desktop/train22.csv",sep=";") #读取分隔符数据,sep按;分列
attach(mtcars)
plot(wt,mpg) #plot(type-直线类型,pch-绘点符号,cex-符号大小,lty-线条类型,lwd-线条宽度x = mtcars$wt,y = mtcars$mpg,type="o",线型main="标题",sub="子标题",xlab="x轴",ylab="y轴")
abline(lm(mpg~wt)) #生成最佳拟合曲线
title("Regression of MPG on Weight")
detach(mtcars)
一、图形
(一)图形排布
par(mfrow=c(2,2)) #图形排列为2x2
hist() #hist为直方图包含默认标题,可用 main="" 禁用
调节分布
attach(mtcars)
layout(matrix(c(1,1,2,3),2,2,byrow = TRUE)) #控制图形分布
hist(wt)
hist(mpg)
hist(disp)
调节长宽占比
layout(matrix(c(1,1,2,3),2,2,byrow = TRUE),widths = c(3,1),heights = c(1,2)) #控制图形分布,调节长宽
hist(wt)
hist(mpg)
hist(disp)
(二)高级图形
opar<-par(no.readonly = TRUE)
par(fig=c(0,0.8,0,0.8)) #fig(x1,x2,y1,y2)绘图区域,
plot(mtcars$wt,mtcars$mpg,xlab="Miles Per Gallon",ylab="Car Weight") #散点图
par(fig=c(0,0.8,0.55,1),new=TRUE) #fig默认新建一幅图,所以设定new=TRUE
boxplot(mtcars$wt,horizontal = TRUE,axes=FALSE) #箱线图
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mtcars$mpg,axes=FALSE)
mtext("标题",side=3,outer=TRUE,line=-3)
par(opar)