R `geom_line()`连接映射到不同组的点

R `geom_line()`连接映射到不同组的点,r,ggplot2,R,Ggplot2,我想根据两个变量的交互作用对我的数据进行分组,但只将美学映射到其中一个变量。(另一个变量表示复制,从理论上讲,复制应该彼此相等)。我可以找到不雅观的方法来做这件事,但似乎应该有更雅观的方法来做 比如说 # Data frame with two continuous variables and two factors set.seed(0) x <- rep(1:10, 4) y <- c(rep(1:10, 2)+rnorm(20)/5, rep(6:15, 2) + rnorm

我想根据两个变量的交互作用对我的数据进行分组,但只将美学映射到其中一个变量。(另一个变量表示复制,从理论上讲,复制应该彼此相等)。我可以找到不雅观的方法来做这件事,但似乎应该有更雅观的方法来做

比如说

# Data frame with two continuous variables and two factors 
set.seed(0)
x <- rep(1:10, 4)
y <- c(rep(1:10, 2)+rnorm(20)/5, rep(6:15, 2) + rnorm(20)/5)
treatment <- gl(2, 20, 40, labels=letters[1:2])
replicate <- gl(2, 10, 40)
d <- data.frame(x=x, y=y, treatment=treatment, replicate=replicate)

ggplot(d, aes(x=x, y=y, colour=treatment, shape=replicate)) + 
  geom_point() + geom_line()

我可以通过手动创建交互列和
group
ing来解决此问题:

d$interact <- interaction(d$replicate, d$treatment)

ggplot(d, aes(x=x, y=y, colour=treatment, group=interact)) + 
  geom_point() + geom_line()

d$interact如果您执行以下操作,您的代码将正常工作。我认为您遇到了一个问题,因为
aes
“treat”
“replicate”
作为向量处理,因此它相当于
组=1

ggplot(d, aes(x=x, y=y, colour=treatment, group=interaction(treatment, replicate))) + 
  geom_point() + geom_line()

为什么在第二个示例中使用
交互(“处理”、“复制”)
而不是列的实际名称?对我来说,
group=interaction(treatment,replicate)
给出了预期的结果可能重复的区别是,在引用的问题中(我本来想链接,现在已经编辑添加链接),答案确实使用引号,但它也使用
qplot
。我猜
qplot
需要引号中的变量,而
ggplot
直接获取变量。链接的答案似乎不正确<代码>qplot(x,y,d,颜色=治疗,组=互动(“治疗”,“复制”),geom=“行”)
不起作用。我将进行必要的编辑。
ggplot(d, aes(x=x, y=y, colour=treatment, group=interaction(treatment, replicate))) + 
  geom_point() + geom_line()