R语言统计入门第四章描述性统计和图形——4.4分组数据作图

4.4分组数据作图

4.4.1直方图

library(ISwR)
attach(energy)
#将expend变量根据stature因子的值分割成两个向量
expend.lean<-expend[stature=="lean"]
expend.obese<-expend[stature=="obese"]
par(mfrow=c(2,1))#按行绘制2*1图
hist(expend.lean,breaks = 10,xlim = c(5,13),ylim = c(0,4),col="white")#breaks区间数设置参数
hist(expend.obese,breaks = 10,xlim = c(5,13),ylim = c(0,4),col="grey")

R语言统计入门第四章描述性统计和图形——4.4分组数据作图

par(mfrow=c(1,1))#还原绘图设置

4.4.2并联箱式图

boxplot(expend~stature)#按stature分类绘图

R语言统计入门第四章描述性统计和图形——4.4分组数据作图

boxplot(expend.lean,expend.obese)#与上条绘制代码同图

R语言统计入门第四章描述性统计和图形——4.4分组数据作图

4.4.3带状图


opar<-par(mfrow=c(2,2),mex=0.8,mar=c(3,3,2,1)+.1)#间距设置
stripchart(expend~stature)
stripchart(expend~stature,method="stack")
stripchart(expend~stature,method="jitter")#点垂直偏置随剂量,默认0.1
stripchart(expend~stature,method="jitter",jitter=.03)

R语言统计入门第四章描述性统计和图形——4.4分组数据作图

par(opar)
stripchart(list(lean=expend.lean,obese=expend.obese))#同上代码

R语言统计入门第四章描述性统计和图形——4.4分组数据作图