如何在R中制作带有两个分类变量的箱线图?

时间:2014-08-18 18:34:53

标签: r boxplot categorical-data

我想制作一个箱线图,显示行为(警报)花费的时间如何受两个变量的影响(期间=上午/下午和访客级别=高/低)。

Alert ~ Period + Vis.Level

'警报'是一组12个数字,显示用其他两个作为重要分类变量唤醒的时间。我看过其他例子,但似乎都没有适合这类问题。

我知道我要找的图表上会有4个箱图......据说是

  • PeriodMorning + Vis.LevelHigh
  • PeriodMorning + Vis.LevelLow
  • PeriodAfternoon + Vis.LevelHigh
  • PeriodAfternoon + Vis.LevelLow
在x轴上。

任何帮助都会很棒!

   Alert Vis.Level    Period
1    0.0       Low   Morning
2    1.0       Low   Morning
3    0.0       Low   Morning
4   11.5       Low Afternoon
5    6.0       Low Afternoon
6   11.5       Low Afternoon
7    0.0      High   Morning
8    0.0      High   Morning
9    0.0      High   Morning
10   0.0      High Afternoon
11   2.5      High Afternoon
12   7.5      High Afternoon

1 个答案:

答案 0 :(得分:3)

假设您的数据看起来像这样

dd <- structure(list(Alert = c(0, 1, 0, 11.5, 6, 11.5, 0, 0, 0, 0, 
2.5, 7.5), Vis.Level = c("Low", "Low", "Low", "Low", "Low", "Low", 
"High", "High", "High", "High", "High", "High"), Period = c("Morning", 
"Morning", "Morning", "Afternoon", "Afternoon", "Afternoon", 
"Morning", "Morning", "Morning", "Afternoon", "Afternoon", "Afternoon"
)), .Names = c("Alert", "Vis.Level", "Period"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))

然后,您要确保您的因素的顺序正确

dd$Period<-factor(dd$Period, levels=c("Morning","Afternoon"))
dd$Vis.Level<-factor(dd$Vis.Level, levels=c("Low","High"))

然后你可以做

boxplot(Alert~Period+Vis.Level, dd)

或者您可以使用

获得所需的确切布局
boxplot(Alert~interaction(Period, Vis.Level, lex.order=T), dd)

enter image description here