admin 管理员组文章数量: 1086019
统计学
2.1 用R自带的Titanic绘制图形
数据和包准备
library(DescTools)
dataframeT = Untable(Titanic) #转为数据框形式
1. Sex 和 Survived 并列条形图 和 堆叠条形图,并添加频数标签
代码里没有调整 Male Femal 图例的大小,只做了 grey80 的框
par(mfrow=c(2,2),cex=1,mgp=c(2,1,0),cex.axis=0.8,cex.main=0.7,font.main=1) #页面布局
tb1 <- table(dataframeT$Sex,dataframeT$Survived) #生成频数表b1 <- barplot(tb1,beside=TRUE,xlab="Survived",ylab = "人数",main="2.1.1 (a)垂直并列图",col=c("#66C2A5","#FC8D62"),legend=rownames(tb1),args.legend = list(box.col="grey80")) #垂直并列条形图
BarText(tb1,b=b1,beside=TRUE,cex=1,pos=c("mid")) #t加频数标签b2 <- barplot(tb1,beside=TRUE,horiz=TRUE,xlab="人数",ylab = "Survived",main="2.1.1 (b)水平并列图",col=c("#66C2A5","#FC8D62"),legend=rownames(tb1),args.legend = list(box.col="grey80")) #水平并列条形图
BarText(tb1,b=b2,beside=TRUE,horiz = TRUE,cex=1,pos=c("mid"))b3 <- barplot(tb1,xlab="Survived",ylab = "人数",main="2.1.1 (c)垂直堆叠图",col=c("#66C2A5","#FC8D62"),legend=rownames(tb1),args.legend = list(box.col="grey80")) #垂直堆叠条形图
BarText(tb1,b=b3,cex=1,pos=c("mid"))b4 <- barplot(tb1,horiz=TRUE,xlab="人数",ylab = "Survived",main="2.1.1 (d)水平堆叠图",col=c("#66C2A5","#FC8D62"),legend=rownames(tb1),args.legend = list(box.col="grey80")) #水平堆叠条形图
BarText(tb1,b=b4,horiz=TRUE,cex=1,pos=c("mid"))
BarText() 的参数 pos 可以决定 bartext 所处的 位置。
> pos
one of “topout”, “topin”, “mid”, “bottomin”, “bottomout”, defining if the labels should be placed on top of the bars (inside or outside) or at the bottom of the bars (inside or outside).
2. Class帕累托图
加载/安装 调色板 RcolorBrewer
par(mai=c(0.7,0.7,0.2,0.7),cex=0.7) #页面布局x <-sort(table(dataframeT$Class),decreasing=TRUE) #生成一维表并将频数降序排列
palette1 <- RColorBrewer::brewer.pal(4,"Blues") #设置调色板bar <- barplot(x,xlab="Class",ylab="频数",main="Class的帕累托图",col=rainbow(4),ylim=c(0,1.2*max(x)))text(bar,x,labels=x,pos=3,col="black") #添加条形图的频数标签
y <- cumsum(x)/sum(x) #计算累计频数
par(new=T)
plot(y,type="b",pch=15,axes=FALSE,xlab='',ylab='',main='')#绘制累积频数折线
axis(side=4) #第四边增加坐标轴
mtext("累计频率",side=4,line=3,cex=0.8) #添加坐标轴标签
text(labels="累积分布曲线",x=3,y=0.93,cex=1) #添加注释文本
3. Class 和 Survived的脊形图
library(DescTools)
par(mai=c(0.5,0.5,0.7,0.7),cex=1) #页面布局
palette2 <- rev(RColorBrewer::brewer.pal(4,'Blues')) #设置调色板s1<-spineplot(dataframeT$Class~dataframeT$Survived,col=palette2,xlab="Class",ylab="Survived",main="Class与Survived脊形图")
4. Class, Sex, Age, Survived 的马赛克图
par(mai=c(0.3,0.3,0.2,0.1),cex=0.7,cex.main=0.8) #页面布局
mosaicplot(~Class+Sex+Age+Survived,data=dataframeT,color=TRUE,cex.axis=0.8,off=5,shade=TRUE,main="Class, Sex, Age, Survived 马赛克图")
5. 选择适当变量绘制饼图,扇形图, 环形图 和饼环图
饼图
par(mfrow=c(1,2),mai=c(0.1,0.4,0.1,0.4),cex=0.8)#页面布局
tb2 <- table(dataframeT$Class) #生成频数表
name1 <- names(tb2) #设置名称向量
percent <- round(prop.table(tb2)*100,digit=2) #计算百分比
labs <- paste(name1," ",percent,"%",sep="") #设置标签向量
pie(table2,labels = labs, init.angle = 90, radius=1,main="Class普通饼图")
扇形图
library(plotrix) #扇形图需要此包
library(RColorBrewer)
tb3 <- table(dataframeT$Class)
palette3 <- rev(RColorBrewer::brewer.pal(4,'Blues')) #设置调色板
labs <- paste(names(tb3)," ",round(prop.table(tb3)*100,digits = 2),"%",sep="") #设置标签
fan.plot(tb3,labels = labs,max.span = 0.9*pi,shrink = 0.06, radius = 1.2,label.radius = 1.4,ticks = 200,col = palette3, main="Class扇形图")
环状图,饼环图
library(ggiraphExtra)
library(gridExtra)
require(ggplot2)p1<-ggDonut(dataframeT,aes(donuts=Class),colour="white",xmin=2,xmax=4,title="Class环形图")
p2<-ggPieDonut(data=dataframeT,aes(donuts=Sex,pies=Class),title="Class & Sex 饼环图")
grid.arrange(p1,p2,ncol=2)
2.2 自带的faithful绘制并分析数据分布特征
1. eruptions 直方图 并添加扰动点,核密度曲线
attach(faithful)
par(mai=c(0.7,0.7,0.5,0.5),cex=0.7,font.main=1)
hist(eruptions,prob=TRUE,col="grey",labels=TRUE,xlab="eruptions",ylab="密度",main="eruption直方图")
rug(jitter(eruptions))
lines(density(eruptions),col="red",lwd=1)
2. eruptions, waiting 的核密度比较曲线
library(reshape2);library(DescTools)
par(mai=c(0.7,0.7,0.5,0.5),cex=0.7,font.main=1)
PlotMultiDens(faithful,main="核密度比较曲线",xlab="数据")
3. eruptions, waiting 箱线图,小提琴图
par(mai=c(0.7,0.7,0.5,0.5),cex=0.7,font.main=1)palette<- RColorBrewer::brewer.pal(6,"Set2")
boxplot(faithful,xlab="eruptions",col=palette,ylab="waiting",main="箱线图")
install.packages("vioplot")
library(vioplot)
vioplot(faithful,col=palette,xlab="eruptions",ylab="waiting",main="小提琴图")
4. eruptions 茎叶图和点图
stem.leaf(faithful$eruptions)
stem.leaf.backback(faithful$eruptions,back.to.back=TRUE)
dotchart(faithful$eruptions,xlab="eruptions",main = "Eruptions点图")
本文标签: 统计学
版权声明:本文标题:统计学 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1686607135a16368.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论