R 交互图-重叠顺序

R 交互图-重叠顺序,r,plot,interaction,R,Plot,Interaction,我有一个这样的数据框 "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" 正如您所看到的,我有一个文件(bread.txt),每个环境值(冷、中、热)有2个样本。我想创建一个交互图,其中我有2个样本,

我有一个这样的数据框

"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"
正如您所看到的,我有一个文件(bread.txt),每个环境值(冷、中、热)有2个样本。我想创建一个交互图,其中我有2个样本,每个样本都会根据环境绘制小时数。到目前为止,我最接近的是

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

下面是一个使用
ggplot()
的解决方案:

库(ggplot2)

测试这里有一个带有
ggplot()
的解决方案:

库(ggplot2)
测试
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))