在ggplot2中逐行连接分组

时间:2017-07-26 12:09:15

标签: 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')

enter image description here

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

1 个答案:

答案 0 :(得分:1)

应该这样做。

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

修改

您想要的是按title连接点,同时按variable对其进行着色,从而group = titlecolor = variable美学。