How do I make a boxplot with two categorical variables in R? -


i make boxplot shows how time spent doing behaviour(alert) affected 2 variables (period= morning/afternoon , visitor level= high/low).

alert ~ period + vis.level 

'alert' set of 12 numbers show amount of time spent awake other 2 significant categorical variables. have looked @ other examples none seem fit type of question.

i know graph looking have 4 boxplots on it... supposedly

  • periodmorning+vis.levelhigh
  • periodmorning+vis.levellow
  • periodafternoon+vis.levelhigh
  • periodafternoon+vis.levellow

on x axis.

any @ fantastic!

   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 data looks 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 make sure factors in correct order

dd$period<-factor(dd$period, levels=c("morning","afternoon")) dd$vis.level<-factor(dd$vis.level, levels=c("low","high")) 

then can do

boxplot(alert~period+vis.level, dd) 

or can exact layout requested with

boxplot(alert~interaction(period, vis.level, lex.order=t), dd) 

enter image description here


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -