如何在R中使用两个分类变量制作箱形图? [英] How do I make a boxplot with two categorical variables in R?

查看:691
本文介绍了如何在R中使用两个分类变量制作箱形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个箱线图,以显示花在做行为(警报)上的时间如何受到两个变量(时段=早晨/下午和访问者级别=高/低)的影响。

I would like to make a boxplot that shows how time spent doing a behaviour(Alert) is affected by two variables (Period= Morning/Afternoon and Visitor Level= High/Low).

Alert ~ Period + Vis.Level

警报是一组由12个数字组成的集合,这些数字显示了醒来所花费的时间,而其他两个则作为重要的分类变量。我看过其他示例,但似乎没有一个适合此类问题。

'Alert' is a set of 12 numbers that show the amount of time spent awake with the other two as the significant categorical variables. I have looked at other examples but none seem to fit this type of question.

我知道我要查找的图形上有4个箱形图...据推测是

I know the graph I am looking for would have 4 boxplots on it... supposedly with


  • PeriodMorning + Vis.LevelHigh

  • PeriodMorning + Vis.LevelLow

  • PeriodAfternoon + Vis.LevelHigh

  • PeriodAfternoon + Vis.LevelLow

  • 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


推荐答案

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

Assuming your data looks like this

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"))

然后,您要确保因子顺序正确

Then you'll want to make sure your factors are in the correct order

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)

这篇关于如何在R中使用两个分类变量制作箱形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆