我想group
我的数据基于两个变量的相互作用,但仅将美学映射到其中一个变量。 (另一个变量代表重复,理论上,它们应该彼此相等)。我可以找到不优雅的方法来做到这一点,但似乎应该有更优雅的方法来做到这一点。
例如
# 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=interaction(treatment, replicate)
会有所帮助(例如基于这个问题 https://stackoverflow.com/questions/9968976/group-by-two-columns-in-ggplot2, but geom_line()
仍然连接不同组中的点:
ggplot(d, aes(x=x, y=y, colour=treatment, group=interaction("treatment", "replicate"))) +
geom_point() + geom_line()
我可以通过手动创建交互栏来解决问题group
荷兰国际集团:
d$interact <- interaction(d$replicate, d$treatment)
ggplot(d, aes(x=x, y=y, colour=treatment, group=interact)) +
geom_point() + geom_line()
但似乎应该有更多ggplot2
- 原生获取方式geom_line
仅连接同一组中的点。