如何在 R 中制作并排箱线图?
在本文中,我们将讨论如何在 R 编程语言中并排创建箱线图。如果我们想并排创建多个箱线图,那么我们必须在上述语法中指定一个参数,即 beside=T。它将指定 True。这会将第二个、第三个、.、等放置到下一个箱线图
语法:
boxplot(data,beside=T)
在哪里,
- 旁边是并排放置条形图
- data是矢量数据的组合,表示需要绘制的箱线图的数量。
data=cbind(vector1,vector2,.,vector n)
因此,要实现这种方法,首先要初始化创建每个箱线图的数据,然后使用 cbind() 将所有这些数据组合起来。然后将合并的数据与设置为 TRUE 的旁边参数一起传递给 boxplot函数以并排绘制它们。
示例 1 :
R
# college1 vector
college1=c(98,89,89.0,78,98,89)
# college2 vector
college2=c(88,91,100,78,98,80)
# combine two vectors using cbind
# function
college_data=cbind(college1,college2)
# pass this college_data to the
# boxplot
boxplot(college_data,beside=T)
R
# college1 vector
college1=c(98,89,89.0,78,98,89)
# college2 vector
college2=c(88,91,100,78,98,80)
# college3 vector
college3=c(98,89,89.0,100,67,56)
# combine three college vectors using
# cbind function
college_data=cbind(college1,college2,college3)
# pass this college_data to the box plot
boxplot(college_data,beside=T)
R
# college1 vector
college1=c(98,89,89.0,78,98,89)
# college2 vector
college2=c(88,91,100,78,98,80)
# combine two college vectors
# using cbind function
college_data=cbind(college1,college2)
# pass this college_data to the
# box plot
boxplot(college_data,beside=T,horizontal=T)
输出:
示例 2:
电阻
# college1 vector
college1=c(98,89,89.0,78,98,89)
# college2 vector
college2=c(88,91,100,78,98,80)
# college3 vector
college3=c(98,89,89.0,100,67,56)
# combine three college vectors using
# cbind function
college_data=cbind(college1,college2,college3)
# pass this college_data to the box plot
boxplot(college_data,beside=T)
输出:
如果我们想以水平方式并排显示箱线图,我们可以在语法中传递参数水平。
语法:
boxplot(data,beside=T,horizontal=T)
指定水平 = True:水平排列图
如果 F- False,则以垂直方式排列
示例 3:
电阻
# college1 vector
college1=c(98,89,89.0,78,98,89)
# college2 vector
college2=c(88,91,100,78,98,80)
# combine two college vectors
# using cbind function
college_data=cbind(college1,college2)
# pass this college_data to the
# box plot
boxplot(college_data,beside=T,horizontal=T)
输出: