您好,欢迎访问三七文档
实验四绘制常用统计图形、对图形进行参数设置【实验类型】验证性【实验学时】2学时【实验目的】1、掌握基本统计图形的绘制方法和所表达的意义;2、掌握图形参数的设置与多图环境设置;3、了解ggplot2绘图包和其它绘图包的使用方法。【实验内容】1、绘制基本的统计图形,包括散点图、箱线图、Q-Q图和协同图等;2、对图形进行参数设置,包括添加图题、标签、点、线和颜色等;3、使用gglplot2和其它绘图包绘制常见统计图形。【实验方法或步骤】第一部分、课件例题:4.1data(cars)#调取数据集,用data()可查看R所有数据集cars#车速与制动距离的数据(R自带)attach(cars)#连接数据集par(mai=c(0.9,0.9,0.6,0.3))#图形边缘空白(边距)的宽度for(iinc(p,l,b,c,o,h,s,S,n))#9种类型{plot(speed,dist,type=i,main=paste(type=\,i,\,sep=))}#\为双引号detach()#取消连接数据集4.2dfdata.frame(Age=c(13,13,14,12,12,15,11,15,14,14,14,15,12,13,12,16,12,11,15),Height=c(144,166,163,143,152,169,130,159,160,175,161,170,146,159,150,183,165,146,169),Weight=c(38.1,44.5,40.8,34.9,38.3,50.8,22.9,51.0,46.5,51.0,46.5,60.3,37.7,38.1,45.1,68.0,58.1,38.6,50.8))#数据框pairs(df)#多组图pairs(~Age+Height+Weight,data=df)#与上述结果相4.3coplot(Weight~Height|Age,data=df)#年龄条件下的协同图4.4点图VADeaths#Virginia州在1940年的人口死亡率数据(R自带)me1-apply(VADeaths,1,mean)#矩阵的行向量的均值me2-apply(VADeaths,2,mean)#矩阵的列向量的均值dotchart(VADeaths,gdata=me2,gpch=19,#按类型分类main=DeathRatesinVirginia-1940)dotchart(t(VADeaths),gdata=me1,gpch=19,#按年龄分类main=DeathRatesinVirginia-1940)4.5饼图pie.sales-c(39,200,42,15,67,276,27,66);names(pie.sales)-c(EUL,PES,EFA,EDD,ELDR,EPP,UNE,other)#各候选人的得票结果##figure1,默认色彩,逆时针pie(pie.sales,radius=0.9,main=Ordinarychart)##figure2,彩虹色彩,顺时针pie(pie.sales,radius=0.9,col=rainbow(8),clockwise=TRUE,main=Rainbowcolours)##figure3,灰度色彩,顺时针pie(pie.sales,radius=0.9,clockwise=TRUE,col=gray(seq(0.4,1.0,length=8)),main=Greycolours)##figure4,阴影色彩,逆时针pie(pie.sales,radius=0.9,density=10,angle=15+15*1:8,main=Thedensityofshadinglines)4.6条形图pie.sales-c(39,200,42,15,67,276,27,66);par(mai=c(0.9,0.9,0.3,0.3))#定义图像边距##figure1,添加一条线rbarplot(pie.sales,space=1,col=rainbow(8));lines(r,pie.sales,type='h',col=1,lwd=2)##figure2,用text()添加平均值mp-barplot(VADeaths);tot-colMeans(VADeaths);text(mp,tot+3,format(tot),xpd=TRUE,col=blue)###figure3,添加条形的颜色barplot(VADeaths,space=0.5,col=c(lightblue,mistyrose,lightcyan,lavender,cornsilk))##figure4,条形平行排列barplot(VADeaths,beside=TRUE,col=c(lightblue,mistyrose,lightcyan,lavender,cornsilk),legend=rownames(VADeaths),ylim=c(0,100))4.7直方图dfdata.frame(Age=c(13,13,14,12,12,15,11,15,14,14,14,15,12,13,12,16,12,11,15),Height=c(144,166,163,143,152,169,130,159,160,175,161,170,146,159,150,183,165,146,169),Weight=c(38.1,44.5,40.8,34.9,38.3,50.8,22.9,51.0,46.5,51.0,46.5,60.3,37.7,38.1,45.1,68.0,58.1,38.6,50.8))#数据框par(mai=c(0.9,0.9,0.6,0.3))#图形边距attach(df)#连接数据框##figure1,增加直方图和外框的颜色,以及相应的频数hist(Height,col=lightblue,border=red,labels=TRUE,ylim=c(0,7.2))##figure2,使用线条阴影并利用text()标出频数,用lines()绘出数据的密度曲线(蓝色)和正态分布密度曲线(红色)r-hist(Height,breaks=12,freq=FALSE,density=10,angle=15+30*1:6);text(r$mids,0,r$counts,adj=c(.5,-.5),cex=1.2);lines(density(Height),col=blue,lwd=2);x-seq(from=130,to=190,by=0.5);lines(x,dnorm(x,mean(Height),sd(Height)),col=red,lwd=2)detach()#取消连接数据框4.8箱线图(1)xc(25,45,50,54,55,61,64,68,72,75,75,78,79,81,83,84,84,84,85,86,86,86,87,89,89,89,90,91,91,92,100)fivenum(x)#上、下四分位数,中位数,最大和最小值boxplot(x)#绘制箱线图(2)InsectSprays#数据框,其中count为昆虫数目,spray为杀虫剂的类型boxplot(count~spray,data=InsectSprays,col=lightgray)#矩形箱线图boxplot(count~spray,data=InsectSprays,notch=TRUE,col=2:7,add=TRUE)4.9QQ图df-data.frame(Age=c(13,13,14,12,12,15,11,15,14,14,14,15,12,13,12,16,12,11,15),Height=c(144,166,163,143,152,169,130,159,160,175,161,170,146,159,150,183,165,146,169),Weight=c(38.1,44.5,40.8,34.9,38.3,50.8,22.9,51.0,46.5,51.0,46.5,60.3,37.7,38.1,45.1,68.0,58.1,38.6,50.8))#数据框par(mai=c(0.9,0.9,0.6,0.3))attach(df)qqnorm(Weight)#数据的正态Q-Q图qqline(Weight)#在Q-Q图上增加一条理论直线y=σx+μqqnorm(Height)qqline(Height)detach()4.10三维透视图—perspy-x-seq(-7.5,7.5,by=0.5)#定义域f-function(x,y){r-sqrt(x^2+y^2)+2^{-52}#加上一个很小的量2^{-52}是为了避免在下一行运算时分母为零z-sin(r)/r};z-outer(x,y,f)#对f作外积运算形成网格par(mai=c(0.0,0.2,0.0,0.1))#图像边距persp(x,y,z,theta=30,phi=15,expand=.7,col=lightblue,xlab=X,ylab=Y,zlab=Z)#绘制三维图4.11等值线—contoury-x-seq(-3,3,by=0.125)#定义域f-function(x,y){z-3*(1-x)^2*exp(-x^2-(y+1)^2)-10*(x/5-x^3-y^5)*exp(-x^2-y^2)-1/3*exp(-(x+1)^2-y^2)};z-outer(x,y,f)#对函数f作外积运算形成网格par(mai=c(0.8,0.8,0.2,0.2))#图像边距contour(x,y,z,levels=seq(-6.5,8,by=0.75),xlab=X,ylab=Y,col=blue)#绘制等值线4.12添加点、线、文字或符号data(iris)#调用数据op-par(mai=c(1,1,0.3,0.3),cex=1.1)#定义图形参数x-iris$Petal.Length;y-iris$Petal.Widthplot(x,y,type=n,xlab=PetalLength,ylab=PetalWidth,cex.lab=1.3)Species-c(setosa,versicolor,virginica)pch-c(24,22,25)#图中点的形状for(iin1:3){index-iris$Species==Species[i];points(x[index],y[index],pc=pch[i],col=i+1,bg=i+1)}#添加点par(op)#访问当前图形参数设置text(c(3,2.5,4),c(0.25,1.5,2.25),labels=Species,font=2,col=c(2,3,4),cex=1.5)#添加文字说明4.13添加直线、线段和图例data(cars)Q1-function(beta,data)sum(abs(data[,2]-beta[1]-beta[2]*data[,1]))#偏差的绝对值之和Qinf-function(beta,data)max(abs(data[,2]-beta[1]-beta[2]*data[,1]))z1-optim(c(-17,4),Q1,data=cars);zinf-optim(c(-17,4),Qinf,data=cars);lm.sol-lm(dist~speed,data=cars)#线性回归op-par(mai=c(.9,.9,.5,0.1),cex=1.1)#绘图参数plot(cars,main=StoppingDistanceversusSpeed,ylim=c(0,140),xlab=Speed(mph),ylab=Distance(ft),pch=19,col=magenta,cex.lab=1.2)abline(lm.sol,lwd=2,col=b
本文标题:R语言实验四
链接地址:https://www.777doc.com/doc-4476002 .html