在ggplot2中按线连接分组点

Tho*_*mas 2 r ggplot2

我试图将每组的每个蓝点与其相应的红点连接起来。但是,我在使用时遇到了麻烦geom_segment。谢谢帮助。

repl <- data.frame(title = c("A", "B", "C", "A", "B", "C"), diff = c(10.06, -1.89, 12.79, 10.06, -1.89, 12.79), id = c(1:6), acc= c(43, 50, 44, 43, 50, 44), variable= c(rep("A", 3), rep("B", 3)), value=c(43,50,44,53,48,56))


 ggplot(repl, aes(value, title, y=reorder(title, diff), group=variable, color=variable)) + 
     geom_point(size=2, shape=8)+
     geom_segment(aes(xend=value, x=value, y=title, yend=title), col='gray')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

对于每个组,应该有一条线连接水平线上的两个点,我应该怎么做?

And*_*din 5

这应该可以。

ggplot(repl, aes(x = value, y = reorder(title, diff),
                 group = title, color = variable)) + 
    geom_point(size = 2, shape = 8)+
    geom_line(col = 'gray')
Run Code Online (Sandbox Code Playgroud)

编辑:

你想要的是将点连接起来,title同时给它们着色,从而variable达到group = title审美color = variable目的。