在ggplot2中按行连接分组的点 [英] Connect grouped points by line in ggplot2

查看:34
本文介绍了在ggplot2中按行连接分组的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将每个蓝点与其对应的每组红点相连.但是,我在使用 geom_segment 时遇到了麻烦.谢谢.

I'm trying to connect each blue point with its corresponding red point per group. However, I have trouble using geom_segment. Thx for help.

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')

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

For each group there should be a line connecting the two points on the horizontal, how should I do that?

推荐答案

应该这样做.

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 = title color = variable美观.

What you want is to connect points by title while coloring them by variable hence the group = title and color = variable aesthetic.

这篇关于在ggplot2中按行连接分组的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆