说,我有一个像这样的数据框
"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),每个 environment (冷,中,暖)值都有2个样本。我想创建一个交互图,其中我有2个样本,每个样本都会根据环境绘制小时。我到目前为止最接近的是
r=rep(1:6)
interaction.plot(r,bread$environment,bread$hours)
答案 0 :(得分:1)
以下是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))