习题 2、1
(1)简单频数分布表:
> load(\"D:\\\\工作总结\\\\\\\\R语言\\\\《统计学—基于R》(第3版)—例题与习题数据(公开资源)\\\\exercise\\\\ch2\\\\exercise2_1、RData\") > summary(exercise2_1) 行业 性别 满意度 电信业:38 男:58 不满意:75 航空业:19 女:62 满意 :45 金融业:26 旅游业:37
二维列联表:
> mytable1<-table(exercise2_1$行业,exercise2_1$满意度) > addmargins(mytable1) # 增加边界与 不满意 满意 Sum 电信业 25 13 38 航空业 12 7 19 金融业 11 15 26 旅游业 27 10 37 Sum 75 45 120
三维列联表:
> mytable1<-ftable(exercise2_1, row、vars = c(\"性别\满意度\"), col、var=\"行业\");mytable1 行业 电信业 航空业 金融业 旅游业 性别 满意度 男 不满意 11 7 7 11 满意 6 3 7 6 女 不满意 14 5 4 16 满意 7 4 8 4
(2)
条形图:
> count1<-table(exercise2_1$行业) > count2<-table(exercise2_1$性别) > count3<-table(exercise2_1$满意度)
> par(mfrow=c(1,3),mai=c(0、7,0、7,0、6,0、1),cex=0、7,cex、main=0、8) > barplot(count1,xlab=\"行业\频数\") > barplot(count2,xlab=\"性别\频数\") > barplot(count3,xlab=\"满意度\频数\")
帕累托图:
> count1<-table(exercise2_1$行业)
> par(mai=c(0、7,0、7,0、1,0、8),cex=0、8) > x<-sort(count1,decreasing = T)
> bar<-barplot(x,xlab=\"行业\频数\、2*max(count1)),col=2:5) > text(bar,x,labels = x,pos=3) # 条形图增加数值 > y<-cumsum(x)/sum(x) # cumsum累计求与 > par(new=T)
> plot(y,type=\"b\、5,pch=15,axes=F) > axis(4) # 右Y轴
> mtext(\"累积频率\
> mtext(\"累积分布曲线\、5,cex=0、8,adj=0、75)
复式条形图:
> mytable1<-table(exercise2_1$满意度,exercise2_1$行业)
> barplot(mytable1,xlab=\"行业\频数\、legend=list(x=13), beside = T)
脊形图:
> library(vcd)
> spine(行业~满意度,data=exercise2_1,xlab=\"满意度\行业\、5,1,2、5))
马赛克图:
> mosaicplot(~性别+行业+满意度,data=exercise2_1,col=2:3)
(3) 饼图:
> count1<-table(exercise2_1$行业) > name<-names(count1)
> percent<-prop、table(count1)*100
> label1<-paste(name,\" \
> par(pin=c(3,3),mai=c(0、1,0、4,0、1,0、4),cex=0、8) # 圆得大小 > pie(count1,labels=label1,init、angle = 90)
扇形图:
> count1<-table(exercise2_1$行业) > name<-names(count1)
> percent<-count1/sum(count1)*100
> labs<-paste(name,\" \> library(plotrix)
> fan、plot(count1,labels=labs,ticks=200)
2、2
(1)分10组,绘制频数分布表
> load(\"D:\\\\工作总结\\\\\\\\R语言\\\\《统计学—基于R》(第3版)—例题与习题数据(公开资源)\\\\exercise\\\\ch2\\\\exercise2_2、RData\") > library(actuar)
> v<-as、vector(exercise2_2$灯泡寿命)
> gd1<-grouped、data(v, breaks = 10, right = FALSE) > table1<-data、frame(gd1);table1 Var、1 v 1 [2600, 2800) 1 2 [2800, 3000) 4 3 [3000, 3200) 12 4 [3200, 3400) 13 5 [3400, 3600) 27 6 [3600, 3800) 20 7 [3800, 4000) 19 8 [4000, 4200) 4
(2)直方图
> d<-exercise2_2$灯泡寿命
> hist(d,breaks=10,xlab=\"寿命\频数\")
茎叶图:
> stem(exercise2_2$灯泡寿命)
The decimal point is 2 digit(s) to the right of the | 26 | 0 28 | 968 30 | 99
32 | 780134569 34 | 112223344455559 36 | 4445567788
38 | 22455566670055669 40 | 001017
2、3
(1)箱线图:
> load(\"D:\\\\工作总结\\\\\\\\R语言\\\\《统计学—基于R》(第3版)—例题与习题数据(公开资源)\\\\exercise\\\\ch2\\\\exercise2_3、RData\")
> boxplot(exercise2_3[,-1],xlab=\"城市\气温\、lab=0、8,cex、axis=0、6) # 从第二列开始,到最后
小提琴图:
> library(vioplot) > x1<-exercise2_3$北京 > x2<-exercise2_3$沈阳 > x3<-exercise2_3$上海 > x4<-exercise2_3$南昌
> vioplot(x1,x2,x3,x4,names=c(\"北京\沈阳\上海\南昌\"))
(2)点图:
> library(reshape)
> table1_1<-melt(exercise2_3,id、vars=c(\"月份\"),variable_name=\"城市\") > table1_1<-rename(table1_1,c(value=\"温度\"))
> dotchart(table1_1$温度,groups=table1_1$城市,xlab=\"温度\
> library(lattice)
> dotplot(温度~城市,data=table1_1,pch=19)
核密度图:
> library(lattice)
> dp1<-densityplot(~温度,group=城市,data=table1_1,auto、key=list(columns=1,x=0、01,y=0、95,cex=0、6),cex=0、5) > plot(dp1)
> library(sm)
> sm、density、pare(table1_1$温度,table1_1$城市,lty=1:6,col=1:6) > legend(\"topleft\城市),lty=1:6,col=1:6)
(3)轮廓图
> matplot(t(exercise2_3[,-1]),type=\"b\城市\温度\
> axis(side=1,at=1:10,labels = c(\"北京\沈阳\上海\南昌\郑州\武汉\广州\海口\重庆\昆明\"))
> legend(\"bottomright\ names(exercise2_3[,-1])) # 取列名
雷达图:
> library(fmsb)
> table1<-data、frame(t(exercise2_3[,2:11])) #行列进行转换,并数据框
> radarchart(table1,axistype=0,seg=4,maxmin=F,vlabels=exercise2_3[,1])
> legend(x=\"topleft\图例
(4)星图:
> matrix1<-as、matrix(exercise2_3[,2:11]) > rownames(matrix1)<-exercise2_3[,1] > stars(matrix1,key、loc=c(7,2,5),cex=0、8)
脸谱图:
> library(aplpack)
> faces(t(matrix1),nrow、plot = 5,ncol、plot = 2,face、type = 0) effect of variables: modified item Var \"height of face \" \"1月\" \"width of face \" \"2月\" \"structure of face\" \"3月\" \"height of mouth \" \"4月\" \"width of mouth \" \"5月\" \"smiling \" \"6月\" \"height of eyes \" \"7月\" \"width of eyes \" \"8月\" \"height of hair \" \"9月\" \"width of hair \" \"10月\" \"style of hair \" \"11月\" \"height of nose \" \"12月\" \"width of nose \" \"1月\" \"width of ear \" \"2月\" \"height of ear \" \"3月\"
2、4
(1)散点图:
> plot(地区生产总值,最终消费支出,xlab=\"\最终消费支出') > abline(lm(最终消费支出~地区生产总值,data=exercise2_4))
> points(固定资产投资,最终消费支出,ylab='最终消费支出',pch=2,col=\"blue\")
> abline(lm(最终消费支出~固定资产投资,data=exercise2_4),col=\"blue\")
气泡图:
> r<-sqrt(最终消费支出/pi)
> symbols(最终消费支出,地区生产总值,circles=r, inches=0、3, fg=\"white\最终消费支出\地区生产总值\")
> text(最终消费支出,地区生产总值,rownames(exercise2_4)) > mtext(\"气泡大小=最终消费支出\、5,adj=0、1)
(2)星图:
> matrix1<-as、matrix(exercise2_4[,2:4]) > rownames(matrix1)<-exercise2_4[,1] > stars(matrix1,key、loc=c(7,2,5),cex=0、8)
脸谱图:
> library(aplpack)
> faces(matrix1,nrow、plot = 6,ncol、plot = 6,face、type = 0)
2、5
时序图:
> load(\"D:\\\\工作总结\\\\\\\\R语言\\\\《统计学—基于R》(第3版)—例题与习题数据(公开资源)\\\\exercise\\\\ch2\\\\exercise2_5、RData\")
> table1<-ts(exercise2_5,start=2004)
> plot(table1[,2],xlab=\"年份\价格指数\> points(table1[,2],type=\"o\年份\城镇价格指数\") > lines(table1[,3],type=\"b\")
2、6
洛伦茨曲线:
> load(\"D:\\\\工作总结\\\\\\\\R语言\\\\《统计学—基于R》(第3版)—例题与习题数据(公开资源)\\\\exercise\\\\ch2\\\\exercise2_6、RData\") > library(DescTools)
> Lc(exercise2_6$不同阶层人口数得收入额*10000/exercise2_6$不同收入阶层得人口数,exercise2_6$不同收入阶层得人口数) # 标红为组中值,收入/人数 $p
[1] 0、0000000 0、3478261 0、6086957 0、8260870 0、9565217 1、0000000 $L
[1] 0、00000000 0、06060606 0、15151515 0、33333333 0、636363 1、00000000 $L、general
[1] 0 20000 50000 110000 210000 330000 $Gini
[1] 0、6232632 $x
[1] 1250、00 2500、00 6000、00 16666、67 60000、00 $n
[1] 80 60 50 30 10 attr(,\"class\") [1] \"Lc\"
> plot(Lc(exercise2_6$不同阶层人口数得收入额*10000/exercise2_6$不同收入阶层得人口数,exercise2_6$不同收入阶层得人口数),xlab=\"人数比例\收入比例\、first=grid(10,10,col=\"gray70\"))
结论:>0、4, 收入差距巨大