win.graph()
dev.cur()
dev.list()
dev.set(n)
dev.off()
dev.off(n)
pdf("xx.pdf")
win.metafile("xx.wmf")
fng("xx.png")
jpeg("xx.jpg")
bmp("xx.bmp")
postscript("xx.ps")
h<-c(rep(1.5,5),rep(1.4,5),rep(1.3,5),rep(1.2,5),rep(1.1,5))
l<-rep(seq(from=1,to=5,by=1),5)
c<-0:24
plot(x=l,y=h,pch=c,xlim = c(1,5),ylim = c(1,2))
points(x=1.6,y=2.00,pch=10)
par(mfrow=c(2,2),mar=c(1,2,3,4))
mylayout<-matrix(c(1,1,0,2),nrow = 2,ncol = 2,byrow = TRUE)
DrawLayout<-layout(mylayout,widths = c(1,1),heights = c(1,2),respect = TRUE)
layout.show(DrawLayout)
i<-1.8
l<-c(1,1.2)
k<-1
while(i>=1.5){
h<-rep(i,2)
i<-i-0.05
lines(x=l,y=h,lty=k,col=3)
k<-k+1
}
t<-c("p","l","b")
k<-0
a<-1.2
for(i in 1:3){
l<-c(1.4,1.6,1.8)
a<-a+k
h<-c(a,a,a)
k<-k+0.05
lines(x=l,y=h,type=t[i])
}
Drawp<-par()
par(pch=3,lty=2,mar=c(1,0.5,1,2))
par(DrawP)
claimdata<-read.table(file="车险数据.txt",header = TRUE)
Drawp<-par()
par(mfrow=c(2,1),mar=c(1,2,3,4))
hist(claimdata$nclaims,xlab = "理赔次数",ylab="频率",main="车险理赔次数直方图",cex.lab=0.7,freq=FALSE,ylim=c(0,0.1))
meantmp<-mean(claimdata$nclaims,na.rm = TRUE)
sdtmp<-sd(claimdata$nclaims)
d=seq(from=min(claimdata$nclaims),to=max(claimdata$nclaims),by=0.1)
lines(x=d,y=dnorm(d,meantmp,sdtmp),lty=2,col=3)
lines(density(ClaimData$nclaims),lty=4,col=4)
plot(density(ClaimData$nclaims),main="核密度图",type="l",xlab = "理赔次数",ylab="密度")
rug(jitter(ClaimData$nclaims),side=1,col=2)
par(DrawL)
install.packages("vioplot")
library("vioplot")
claimdata<-read.table(file="车险数据.txt",header = TRUE)
Drawp<-par()
par(mfrow=c(2,1),mar=c(4,6,4,4))
vioplot(claimdata$nclaims,horizontal=TRUE)
title(main = "车险理赔次数小提琴图",cex.main=0.8,xlab = "理赔次数",ylab = "全部观测",cex.lab=0.7)
tmpA<-claimdata$nclaims[claimdata$vehiclegroup=="A"]
tmpB<-claimdata$nclaims[claimdata$vehiclegroup=="B"]
tmpC<-claimdata$nclaims[claimdata$vehiclegroup=="C"]
tmpD<-claimdata$nclaims[claimdata$vehiclegroup=="D"]
LabX<-c("A","B","C","D")
Lo<-vioplot(tmpA,tmpB,tmpC,tmpD,names = LabX)
title(main="各车型理赔次数小提琴图",cex.main=0.8,xlab="车型",ylab="理赔次数",cex.lab=0.7)
text(x=1:4,y=Lo$upper,labels = c(length(tmpA),length(tmpB),length(tmpC),length(tmpD)),srt=90)
par(Drawp)
claimdata<-read.table(file="车险数据.txt",header = TRUE)
Drawp<-par()
par(mfrow=c(2,1),mar=c(4,6,4,4))
dotchart(claimdata$nclaims,main = "车险理赔次数克利夫兰点图",cex.main=0.8,xlab = "理赔次数",ylab = "观测编号",cex.lab=0.8)
Avg<-tapply(claimdata$nclaims, INDEX=claimdata$vehiclegroup, FUN=mean)
dotchart(claimdata$nclaims[order(claimdata$vehiclegroup)],main="各车型的克利夫兰点图",cex.main=0.8,xlab="理赔次数",ylab = "车型",groups =claimdata$vehiclegroup,gdata = Avg,gpch = 17 )
legend("bottomright",title = "点型说明",c("观测值","均值"),pch=c(1,17),bg="white",cex=0.5)
par(Drawp)
mu1<-0
mu2<-0
ss1<-10
ss2<-10
rho<-0.7
MyDen<-function(x,y) #计算联合分布
{
t1<-1/(2*pi*sqrt(ss1*ss2*(1-rho^2)))
t2<
t3<-(x-mu1)^2/ss1
t4<-(y-mu2)^2/ss2
t5<
return(t1*exp(t2*(t3+t4-t5)))
}
x<-seq(-10,10,length=50)
y<-x
z<-outer(x,y,FUN=MyDen)
par(mfrow=c(2,2),mar=c(6,4,4,1))
persp(x,y,z,main="二元正态分布密度曲面图",theta=30,phi=20,expand=0.5,shade = 0.5,xlab="X",ylab="Y",zlab="f(x,y)")
contour(x,y,z,main="二元正态分布密度等高线图")
Myf<-function(x,y) {
r<-sqrt(x^2+y^2)
r<-10*sin(r)/r
return(r)
}
x<-seq(-10,10,length=30)
y<-x
z<-outer(x,y,Myf)
z[is.na(z)]<-1
persp(x,y,z,main="曲面图",theta=30,phi=30,expand=0.5)
contour(x,y,z,nlevels=10,main="等高线图")
install.packages("fmsb")
library("fmsb")
forest<-read.table(file="森林数据.txt",header =TRUE)
head(forest)
avy<-aggregate(forest[,5:9],by=list(forest[,2]),FUN=mean)
radarchart(df=avy[,2:6],axistype = 0,seg = 5,maxmin=FALSE,
vlabels = c("温度","湿度","风力","雨量","过火面积"),title="不同纬度地区气候平均值的雷达图")
library("vcd")
ClaimData<-read.table(file="车险数据.txt",header=TRUE)
mosaic(~vehiclegroup+vehicleage,data=ClaimData,shade=TRUE,legend=TRUE)
with(ClaimData,{table(vehiclegroup,vehicleage)})
forest<-read.table(file="森林数据.txt",header = TRUE)
Drawp<-par()
par(mfrow=c(1,2),mar=c(6,6,4,1))
plot(x=forest$temp,y=forest$RH,main="温度和相对湿度散点图",xlab = "温度",ylab="相对湿度",cex.main=0.8,cex.lab=0.8)
mo<-lm(RH~temp,data = forest)
abline(mo$coefficients)
M.Loess<-loess(RH~temp,data=forest)
Ord<-order(forest$temp)
lines(forest$temp[Ord],y=M.Loess$fitted[Ord],lwd=1,lty=1,col=2)
smoothScatter(x=Forest$temp,y=Forest$RH,main="森林地区温度和相对湿度的高密度处理散点图",xlab="温度",ylab="相对湿度",cex.main=0.8,cex.lab=0.8)
par(DrawL)
install.packages("hexbin")
library("hexbin")
bin<-hexbin(x=Forest$temp,y=Forest$RH,xbins=30)
plot(bin,main="森林地区温度和相对湿度的高密度处理散点图",xlab="温度",ylab="相对湿度")
install.packages("scatterplot3d")
library("scatterplot3d")
DrawL<-par()
mylayout<-matrix(c(1,2,3,3),nrow = 2,ncol = 2,byrow = TRUE)
DrawLayout<-layout(mylayout,widths=c(1,1),height= c(1,1),respect = FALSE)
layout.show(DrawLayout)
s3d<-with(Forest,scatterplot3d(temp,RH,wind,main="森林地区温度、相对湿度和风力的三维散点图",xlab="温度",ylab="相对湿度",zlab="风力",cex.main=0.7,cex.lab=0.7,cex.axis=0.7))
fit<-lm(wind~temp+RH,data=Forest)
s3d$plane3d(fit,col="blue")
with(Forest,symbols(temp,RH,circle=wind,inches=0.1,main="森林地区温度、相对湿度和风力的汽包图",xlab="温度",ylab="相对湿度",cex.main=0.7,cex.lab=0.7,cex.axis=0.7))
with(Forest,symbols(temp,RH,circle=wind,inches=0.1,main="森林地区温度、相对湿度和风力的汽包图",xlab="温度",ylab="相对湿度",
cex.main=0.7,cex.lab=0.7,cex.axis=0.7,fg="white",bg="lightblue"))
par(DrawL)
pairs(~temp+RH+wind,data=forest,main="森林地区温度、相对湿度和风力的矩阵散点图")
install.packages("car")
library("car")
scatterplotMatrix(~temp+RH+wind,data=Forest,main="森林地区温度、相对湿度和风力的矩阵散点图",lty.smooth=2,spread=TRUE)
forest<-read.table(file = "森林数据.txt",header = TRUE)
forest$month<-factor(forest$month,levels = c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))
coplot(RH~temp|month, pch=9,data=forest,xlab="温度",ylab="相对湿度")
coplot(RH~temp|wind,pch=1,number = 6,data=forest,xlab="温度",ylab="相对湿度")
Mypanel.lm<-function(x,y,...){
Tmp<-lm(y~x)
abline(Tmp$coefficients)
points(x,y,pch=1)}
coplot(RH~temp|wind,number=6,panel=Mypanel.lm,data=Forest,pch=1,xlab="温度",ylab="相对湿度")