- 箱线图
通过绘制连续型变量的最小值,下四分位数(25%位数)、中位数(50%分位数)、上四分位数(75%分位数)以及最大值,描述连续性变量的分布,箱线图能够显示出可能的离群点(范围+-1.5*IQR以外的值,IQR表示四分位距,即上分位数与下四分位数的差值)的观测值
boxplot(mtcars$mpg,main="Box plot",ylab="Miles per Gallon")
boxplot.stats(mtcars$mpg) #查看统计量,最小值、下分位,中位数,上分位,最大值
boxplot(mtcars$mpg,main="Box plot",ylab="Miles per Gallon")
x
1
boxplot(mtcars$mpg,main="Box plot",ylab="Miles per Gallon")
2
boxplot.stats(mtcars$mpg) #查看统计量,最小值、下分位,中位数,上分位,最大值
- 使用并列箱线图进行跨组比较
boxplot(formula,data = dataframe)
1
1
1
boxplot(formula,data = dataframe)
formula:是一个公式,y ~ A 这将为类别变量 A 的每个值并列生成数值型变量 y 的箱线图
y ~A*B则将为类别型变量 A 和 B所有水平的两两组合生成数值型变量 y 的箱线图 (两个交叉因子的箱线图)
dataframe:代表提供数据的数据框或者列表
varwidth: 为TRUE将使箱线图的宽度与其样本大小的平方根成正比
horizontal:为TRUE可以反转坐标轴的方向
notch:为TRUE可以得到凹槽的箱线图,若两个凹槽互不重叠,则表明它们中位数有显著差异
例
boxplot(mpg~cyl,data=mtcars,
main="Car Milage Data",
xlab="Number of Cylinders",
ylab="Miles Per Gallon")
4
4
1
boxplot(mpg~cyl,data=mtcars,
2
main="Car Milage Data",
3
xlab="Number of Cylinders",
4
ylab="Miles Per Gallon")
例
#含凹槽的箱线图
boxplot(mpg~cyl,data=mtcars,
notch=TRUE,
varwidth=TRUE,
col="red",
main="Car Mileage Data",
xlab="Number of Cylinders",
ylab="Miles Per Gallon")
8
8
1
#含凹槽的箱线图
2
boxplot(mpg~cyl,data=mtcars,
3
notch=TRUE,
4
varwidth=TRUE,
5
col="red",
6
main="Car Mileage Data",
7
xlab="Number of Cylinders",
8
ylab="Miles Per Gallon")
例
#两个交叉因子的箱线图
boxplot(mpg ~ am.f *cyl.f,
data=mtcars,
varwidth=TRUE,
col=c("gold", "darkgreen"),
main="MPG Distribution by Auto Type",
xlab="Auto Type")
7
1
#两个交叉因子的箱线图
2
boxplot(mpg ~ am.f *cyl.f,
3
data=mtcars,
4
varwidth=TRUE,
5
col=c("gold", "darkgreen"),
6
main="MPG Distribution by Auto Type",
7
xlab="Auto Type")