r - 使用 ggplot2 中的 geom_segment() 连接多个时间点
问题描述
我有一个看起来像这样的 data.frame
tree=data.frame(time=c(0,0,0,1,1,1,2,2,2), pop.size=c(10,10,10,14,13,10,17,24,13),
parents=c(NA,NA,NA,1,2,3,1,2,3), offspring=c(1,2,3,1,2,4,5,6,7))
我想在每个时间点将后代绘制为带有 geom_point 的点,并使用 geom_segment() 或任何其他可能有用的几何图形将它们与上一个时间点的父母联系起来。
ggplot(tree,aes(x=time, y=offspring))+
geom_point() +
theme_bw()
任何帮助,将不胜感激。虽然我可以将时间点 1 和时间点 2 连接起来,但很难将时间点 2 和时间点 3 连接起来。我不得不告诉你,我最多有 100 个点
编辑:我想看到这样的东西。在这里,我仅将时间点 1 的元素与时间点 2 的元素连接起来。我想将时间点 2 与时间点 3 连接起来,我的情节看起来像一棵家谱树
ggplot(tree,aes(x=time, y=offspring))+
geom_point() +
theme_bw() +
geom_segment(aes(x=time[1], xend=time[5], y=parents, yend=offspring))
解决方案
你可以试试:
ggplot(tree,aes(x=time, y=offspring,group=time))+geom_line()+
geom_point() +
theme_bw()
或者:
ggplot(tree,aes(x=time, y=offspring,group=1))+geom_line()+
geom_point() +
theme_bw()
更新
试试这个代码:
tree %>% group_by(time) %>% mutate(ind = 1:length(time)) -> tree2
ggplot(tree2,aes(x=time, y=offspring,group=ind))+geom_line()+
geom_point() +
theme_bw()
推荐阅读
- java - Docker 容器中的 Wildfly 未启动
- sql - 创建 Postgres 视图出现错误:列“id”指定了多次
- java - 在 actionListener 中获得 JTextFeild 的引用
- javascript - 如何在javascript中检测光标与多个移动对象的碰撞?
- python - 在 Python 中解析简历中的技能部分
- php - 使用 Telegram Bot API 制作注册机器人
- python - 如何打印因子列表的所有非重复乘积
- azure - 无法在服务器上部署具有 nuget 引用的应用程序
- r - RBIND 数值向量 (R)
- visual-studio - 如何在 VS 中为 Razor 页面(cshtml)添加自定义代码片段