箱线图还有密度图大家应该都很熟悉,那么结合起来的是什么样呢,我们今天给大家介绍一个包beanplot(豆荚图)。下面我们看下包的安装:
install.packages("beanplot")
其中主要的函数是beanplot,其函数构成如下:
其中主要的参数:
Kernel 主要是指数据分布形式,主要包括"gaussian", "rectangular","triangular", "epanechnikov", "biweight","cosine" 和"optcosine",默认值是 "gaussian"。
Col是指的颜色的设置。其是包括四个值的向量:bean的面积(没有边框,使用边框表示该颜色)、bean内部的线条、bean外部的线条和每个bean的平均线条。
Overallline 总体数据的线的值,可以是mean或者median。
Beanlines 每一个bean的中线的数值,mean(默认),median,quantiles。
Beanlinewd bean中线的宽度。
What 由四个布尔值组成的向量,描述要绘制的内容。按照以下顺序,这些布尔值代表总平均线、豆子线、豆子平均线和豆线。例如,what=c(0,0,0,1)生成一个条形图。
Side 指的bean的形状。包括"first", "second" 和"both"。默认是“no”。
我们直接看一个简单的例子:
beanplot(rnorm(22),rnorm(22),rnorm(22),main="Test!",rnorm(3))
接下来,我们看下boxplot和beanplot的区别:
par(mfrow = c(1,2))boxplot(count ~ spray, data = InsectSprays,col = "lightgray")beanplot(count ~ spray, data =InsectSprays, col = "lightgray", border = "grey", cutmin =0)
对数据进行log处理,并设置相应的ylim:
par(mfrow = c(1,2))boxplot(decrease ~ treatment, data =OrchardSprays, log = "y", col = "bisque", ylim = c(1,200))beanplot(decrease ~ treatment, data =OrchardSprays, col = "bisque", ylim = c(1,200))
接下来我们看下多组数据进行绘制,需要用到参数add=T和side进行设置。我们直接看下实例:
par(mfrow = c(1,2))boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1:3 - 0.2, subset = supp == "VC", col = "yellow", main = "Guinea Pigs' Tooth Growth", xlab = "Vitamin C dose mg", ylab = "tooth length", ylim = c(-1, 40), yaxs = "i")boxplot(len ~ dose, data = ToothGrowth, add= TRUE, boxwex = 0.25, at = 1:3 + 0.2, subset = supp == "OJ", col = "orange")legend("bottomright",bty="n", c("Ascorbic acid", "Orange juice"), fill = c("yellow", "orange"))allplot what=c(TRUE,FALSE,FALSE,FALSE),show.names=FALSE,ylim=c(-1,40), yaxs ="i")beanplot(len ~ dose, data = ToothGrowth,add=TRUE, boxwex = 0.6, at = 1:3*2 - 0.9, subset = supp == "VC", col ="yellow",border="yellow2", main = "Guinea Pigs' Tooth Growth", xlab = "Vitamin C dose mg", ylab = "tooth length", ylim = c(3, 40), yaxs = "i", bw = allplot$bw, wd = allplot$wd, what = c(FALSE,TRUE,TRUE,TRUE))beanplot(len ~ dose, data = ToothGrowth, add= TRUE, boxwex = 0.6, at = 1:3*2-0.1, subset = supp == "OJ", col ="orange",border="darkorange", bw = allplot$bw, wd = allplot$wd, what = c(FALSE,TRUE,TRUE,TRUE))legend("bottomright",bty="n", c("Ascorbic acid", "Orange juice"), fill = c("yellow", "orange"))
那么我们如何对每个bean合并起来绘制,需要设置col为一个list。并且包括两种颜色。我们看下实例:
par(mfrow = c(1,2))boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1:3 - 0.2, subset = supp == "VC", col = "yellow", main = "Guinea Pigs' Tooth Growth", xlab = "Vitamin C dose mg", ylab = "tooth length", ylim = c(-1, 40), yaxs = "i")boxplot(len ~ dose, data = ToothGrowth, add= TRUE, boxwex = 0.25, at = 1:3 + 0.2, subset = supp == "OJ", col = "orange")legend("bottomright",bty="n",c("Ascorbic acid", "Orange juice"), fill = c("yellow", "orange")) beanplot(len ~ reorder(supp, len, mean) *dose, ToothGrowth, side = "b", col = list("yellow","orange"), border = c("yellow2", "darkorange"), main = "Guinea Pigs' Tooth Growth", xlab = "Vitamin C dose mg", ylab = "tooth length",ylim = c(-1, 40), yaxs = "i")legend("bottomright",bty="n",c("Ascorbic acid", "Orange juice"), fill = c("yellow", "orange"))
最后我们看一下benplot的更高级应用,那就是变量之间关系的绘制:
最后我们总结下,beanplot图的构造:
欢迎大家互相学习交流!