在 R 中使用 ggplot2 创建关于两个因素的 Boxplot
多变量分布可以用箱线图进行可视化。 ggplot2 允许我们快速创建漂亮的箱线图。一个感兴趣的变量可能有多个子组。在这些情况下,使用“分组箱线图”进行可视化非常有用。 R 编程语言中的 ggplot2 包提供了许多用于可视化此类分组箱线图的选项。
现在谈论箱线图,然后为该因子中的每个类别或级别创建每个箱线图,该因子由一个因子和一个数字列表示。 geom_boxplot 还允许我们在有两个因素时使用 fill 参数绘制两个因素。 geom_boxplot() 是关键函数
Syntax:
geom_boxplot(width,notch,color,size,linetype, fill,outliner.color, outliner.size, outliner.shape)
Parameter:
- width: width of the boxplot
- notch: if it is true then it will create a notched boxplot and notches are used to compare boxplots.
- color, size,line type: borderline, color, size and shape.
- fill: used to fill box plot areas.
- outlier.colour, outlier.shape, outlier.size: The color, the shape and the size for outlying points.
现在让我们看看几个实现。
示例 1:
R
# create a Data Frame
Gender<-sample(c("Male","Female"),20,replace=TRUE)
Values<-rnorm(20,mean=0,sd=1)
Group<-sample(letters[1:5],20,replace=TRUE)
df<-data.frame(Gender,Values,Group)
library(ggplot2)
# creating a boxplot
ggplot(df,aes(Gender,Values))+geom_boxplot(aes(fill=Group))
R
# load ggplot2 package if already installed
library(ggplot2)
# create a data frame with two factors
df <- data.frame(Factor1=factor(rbinom(30, 1, 0.55),
label=c("male","female")),
Factor2=factor(rbinom(30, 1, 0.45),
label=c("young","old")),
Values=rnorm(30,mean=5,sd=2))
# Now make a interaction between two factors
# on x axis
df$Factor1Factor2 <- interaction(df$Factor1, df$Factor2)
# now Plot Boxplot with fill color according
# to factor1 and factor2
ggplot(aes(y = Values, x = Factor1Factor2), data = df) +
geom_boxplot(aes(fill=Factor1Factor2))
输出:
示例 2:
电阻
# load ggplot2 package if already installed
library(ggplot2)
# create a data frame with two factors
df <- data.frame(Factor1=factor(rbinom(30, 1, 0.55),
label=c("male","female")),
Factor2=factor(rbinom(30, 1, 0.45),
label=c("young","old")),
Values=rnorm(30,mean=5,sd=2))
# Now make a interaction between two factors
# on x axis
df$Factor1Factor2 <- interaction(df$Factor1, df$Factor2)
# now Plot Boxplot with fill color according
# to factor1 and factor2
ggplot(aes(y = Values, x = Factor1Factor2), data = df) +
geom_boxplot(aes(fill=Factor1Factor2))
输出: