R 基于特定列值连接ggplot中的点

R 基于特定列值连接ggplot中的点,r,ggplot2,R,Ggplot2,我有以下名为t的数据集: n <- 12 t <- data.frame( V1 = runif(n, 0.12, 0.35), V2 = runif(n, 0.25, 0.39), group = gl(3, 4, labels = c("a1", "a2", "a3")), x = seq_len(n), color = rep(rep.int(c("R", "G"), 2), c(3, 4, 3, 2)) ) n首先:将数据集命名为“t

我有以下名为
t
的数据集:

n <- 12
t <- data.frame(
  V1    = runif(n, 0.12, 0.35),
  V2    = runif(n, 0.25, 0.39),
  group = gl(3, 4, labels = c("a1", "a2", "a3")),
  x     = seq_len(n),
  color = rep(rep.int(c("R", "G"), 2), c(3, 4, 3, 2))
)

n首先:将数据集命名为“t”不是一个好主意,因为它很容易混淆,因为还有一个函数t()

最简单的方法是首先熔化()数据集

Molten <- melt(t, id.vars = c("group", "x", "color"))
ggplot(Molten, aes(x = x, y = value, colour = group, linetype = variable)) + geom_line()

melt如果要在不使用
melt()的情况下绘制图形


p欢迎来到SO。我已经修复了您的数据集,以便其他人更容易复制。:谢谢,但您编写的内容总共有4行,但我有3组和2种颜色。所以应该有6行。因为每组将得到2行代码(对于V1和V2),所以我更新了代码。今后请使用更容易混淆的名称。您有一个名为“color”的变量,但希望通过变量“group”来显示颜色。
Molten <- melt(t, id.vars = c("group", "x", "color"))
ggplot(Molten, aes(x = x, y = value, colour = group, linetype = variable)) + geom_line()
p <-ggplot(t) + geom_line(aes(x,V2,color=group)) + geom_line(aes(x,V1,color=group), linetype = "dashed")