在 R 中的一张图中绘制多个箱线图
在本文中,我们将学习如何使用 R 编程语言在一张图中绘制多个箱线图。这可以通过使用 boxplot()函数来完成,我们也可以向它传递一个列表、数据框或多个向量。为此,我们需要将数据名称作为输入放入 boxplot()函数中。
Syntax: boxplot(x, data, notch, varwidth, names, main)
Parameters:
- x: This parameter sets as a vector or a formula.
- data: This parameter sets the data frame.
- notch: This parameter is the label for horizontal axis.
- varwidth: This parameter is a logical value. Set as true to draw width of the box proportionate to the sample size.
- main: This parameter is the title of the chart.
- names: This parameter are the group labels that will be showed under each boxplot.
一帧中的多个垂直箱线图
为此,需要箱线图表示的单个数据基于函数。默认情况下,箱线图的方向是垂直的,因此这里不需要做任何额外的事情。
示例 1:
R
set.seed(20000)
data <- data.frame( A = rpois(900, 3),
B = rnorm(900),
C = runif(900)
)
# Applying boxplot function
boxplot(data)
R
# Multiple boxplot using dataset
# ToothGrowth dataset
boxplot( len~dose,
data=ToothGrowth,
main="Different boxplots for per day growth",
xlab="Tooth length",
ylab=" numeric Dose in milligrams/day",
col="blue",
border="black"
)
R
# Multiple boxplot using dataset
# ToothGrowth dataset
boxplot(len~dose,
data=ToothGrowth,
main="Different boxplots for per day growth",
xlab="Tooth length",
ylab=" numeric Dose in milligrams/day",
col="yellow",
border="brown",
horizontal=TRUE
)
输出:
示例 2:
电阻
# Multiple boxplot using dataset
# ToothGrowth dataset
boxplot( len~dose,
data=ToothGrowth,
main="Different boxplots for per day growth",
xlab="Tooth length",
ylab=" numeric Dose in milligrams/day",
col="blue",
border="black"
)
输出:
一帧中的多个水平箱线图
该方法与垂直箱线图相同,但要使它们水平工作,必须将 boxplot()函数的水平参数设置为 TRUE。
例子:
电阻
# Multiple boxplot using dataset
# ToothGrowth dataset
boxplot(len~dose,
data=ToothGrowth,
main="Different boxplots for per day growth",
xlab="Tooth length",
ylab=" numeric Dose in milligrams/day",
col="yellow",
border="brown",
horizontal=TRUE
)
输出: