How do I make a boxplot with two categorical variables in R? [closed]

耗尽温柔 提交于 2019-12-04 21:18:02

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

Then you can do

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

or you can get the exact layout you requested with

boxplot(Alert~interaction(Period, Vis.Level, lex.order=T), dd)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!