0

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

'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.

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

on the x axis.

Any help at all would be 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
MrFlick
  • 195,160
  • 17
  • 277
  • 295
Jess.Erin
  • 13
  • 1
  • 1
  • 5
  • What does your input data look like? What does your desired output look like? Please create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we might actually be able to offer specific advice. – MrFlick Aug 18 '14 at 18:36
  • Sorry, seems I forgot to paste that over from R. I have included the data now. It is not too complicated, I just can't figure it out! – Jess.Erin Aug 18 '14 at 18:42
  • Have you tried `boxplot(df$Alert ~ df$Period + df$Vis.level)`, where `df`is your `dataframe`? – Bernardo Aug 18 '14 at 18:46
  • @Bernardo When I do that, I get an error: > df <-mydata > boxplot(df$Alert ~ df$Period + df$Vis.level) Error in model.frame.default(formula = df$Alert ~ df$Period + df$Vis.level) : invalid type (NULL) for variable 'df$Alert' – Jess.Erin Aug 18 '14 at 18:51

1 Answers1

5

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)

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    That seems to be exactly what I am looking for! Thank you so much! – Jess.Erin Aug 18 '14 at 18:54
  • @MrFlick, if you don't mind me asking, is there any particular reason to use `structure(..., class = "dataframe")` instead of using the `data.frame` function? I've seen this a lot lately here on stackoverflow and it got my attention. – Bernardo Aug 18 '14 at 19:00
  • @Bernardo If i were to have typed that out by hand, I would have used `data.frame`, but that's too much work so i usually copy data then use `read.table()` in R from the `clipboard` to create the object. Then I can use `dput` to create a text version of the object. In this case `dput` creates a structure object for data.frames. It's not as "pretty" looking but it works well. It's always nicer when the OP supplies the data like that in the first place because it makes it much easier to load sample data into R. – MrFlick Aug 18 '14 at 19:03
  • @MrFlick I didn't know it was possible to use ´read.table()´ to read data directly from the ´clipboard´, that's brilliant! Thanks! – Bernardo Aug 18 '14 at 19:10