R语言数据可视化

##########数据可视化
win.graph()#手动创建一个图形设备,为当前图形设备
dev.cur()#显示当前图形设备编号
dev.list()#显示当前有几个打开的图形设备
dev.set(n)#指定编号为n的设备为当前图形设备
dev.off()#关闭当前
dev.off(n)#关闭编号为n的图形设备
#图形文件,后续图片将保存到指定格式的文件中
pdf("xx.pdf")
win.metafile("xx.wmf")
fng("xx.png")
jpeg("xx.jpg")
bmp("xx.bmp")
postscript("xx.ps")
##########图形布局,图形参数
#25种符号
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)
#2*2布局
par(mfrow=c(2,2),mar=c(1,2,3,4))
#layout布局设置
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)

#lty的取值(1~6)(在已有坐标系上画,不然lines不会)
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)#绿色3黑色1红色2蓝色4
  k<-k+1
}
#type
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)#概率密度曲线(步长0.1)
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) #数据地毯side=1/3在下/上方
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)#画图并将位置数据存在Lo(一个列表)中
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#xy相关系数
MyDen<-function(x,y) #计算联合分布
{
  t1<-1/(2*pi*sqrt(ss1*ss2*(1-rho^2)))
  t2<--1/(2*(1-rho^2))
  t3<-(x-mu1)^2/ss1
  t4<-(y-mu2)^2/ss2
  t5<--2*rho*((x-mu1)*(y-mu2))/(sqrt(ss1)*sqrt(ss2))
  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) #shade以灰度深浅表示观测频数与期望的差值,legend显示图例
with(ClaimData,{table(vehiclegroup,vehicleage)}) #(域名一+域名二,data=数据框,shade=true)
########散点图
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)#lm函数一元线性回归
abline(mo$coefficients)#abline添加直线,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)
#smoothScatter(x=横坐标向量,y=纵坐标向量)
par(DrawL)
#高密度处理散点图,hexbin函数首先对数据箱处理
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)#lty.smooth局部散点加权平滑的线型,spread添加离散程度的曲线
##分组散点图
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="相对湿度")#panel指定面板函数









你可能感兴趣的:(r语言)