Search code examples
rplotinteraction

Interaction plot - overlapping order


Say, I have a data frame like this

"hours" "environment" "humidity"
"1" 360 "cold" "dry"
"2" 360 "cold" "dry"
"3" 372 "intermediate" "dry"
"4" 420 "intermediate" "wet"
"5" 456 "warm" "wet"
"6" 432 "warm" "wet"

As you can see I have a file (bread.txt) with 2 samples for every value of environment (cold,intermediate,warm). I want to create an interaction plot where I have 2 samples and for each sample it plots the hours against the environment. The closest I've been so far is

r=rep(1:6)
interaction.plot(r,bread$environment,bread$hours)

Solution

  • Here is one solution with ggplot():

    library(ggplot2)
    test <- data.frame(hours=c(360, 360, 372,
                               420, 456, 432),
                       environment=c("cold", "cold", "intermediate",
                                     "intermediate", "warm", "warm"),
                       humidity=c("dry", "dry", "dry",
                                  "wet", "wet", "wet"))
    # Add a factor allowing to distinguish between the first and the second sample
    test$rep <- rep(1:2)
    # plot
    ggplot(data=test) +
        geom_line(aes(x=rep, y=hours, color=environment))
    

    Here is the result:enter image description here