相关疑难解决方法(0)

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

我希望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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这几乎是正确的,除了我不想用不同的形状代表点.这似乎group=interaction(treatment, replicate)会有所帮助(例如,基于这个问题,但geom_line()仍然连接不同组中的点:

ggplot(d, aes(x=x, y=y, colour=treatment, group=interaction("treatment", "replicate"))) + 
  geom_point() + geom_line()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

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

ggplot(d, aes(x=x, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

ggplot2 ×1

r ×1