使用 R 绘制轨迹图 [英] Making a trajectory plot using R
问题描述
我想绘制我的数据集的轨迹图,如 X,Y 的散点图,但数据点使用箭头(箭头指向下一个位置)与一条线连接
I want to make a trajectory plot of my data set, like a scatterplot of X,Y but the data points are connected with a line using an arrow (where the arrow points to the next position)
我的数据如下所示:
A T X Y V GD ND ND2 TID
1 1 3.88 2.7 675.0 27.000 27.000 NA NA t1
2 1 3.92 2.7 677.7 42.691 69.691 2.7 7.29 t1
3 1 3.96 2.7 675.0 55.662 125.353 0.0 0.00 t1
4 1 4.00 2.7 675.0 55.662 181.015 0.0 0.00 t1
5 1 4.04 2.7 675.0 55.662 236.677 0.0 0.00 t1
6 1 4.08 2.7 680.4 42.691 279.368 5.4 29.16 t1
我已经尝试使用 qplot 来制作它:
And I have tried using qplot to make it:
qplot(X, Y, data = sub.data1, color = TID, group = TID)+
geom_line(linetype=5, size=1.5, arrow=arrow(angle=15, ends="both", type="closed"))+
geom_point (shape=19, size=5, fill="black")
没关系,它奏效了.只是我想在我的绘图箭头中指出指向下一个数据点的点.任何帮助都会很棒!谢谢!
This was okay, it worked. Just that I wanted to make the points in my plot arrows that are pointing in the next data point. Any help would be great! Thanks!
推荐答案
我认为您想探索 geom_path 和 geom_segment 属性.请参阅 http://docs.ggplot2.org/current/geom_segment.html和http://docs.ggplot2.org/current/geom_path.html
I think you want to explore the geom_path and geom_segment attributes. See http://docs.ggplot2.org/current/geom_segment.html and http://docs.ggplot2.org/current/geom_path.html
例如,我可以将您的绘图简单地更改为一条路径,以按照它们在表格中的显示顺序而不是 X 轴的顺序连接点:
For example, I can take your plot and simply change the line to a path to connect the dots in the order that they are presented in your table rather than by the X axis:
library("ggplot2")
library(grid) # needed for arrow function
library(data.table)
# see http://docs.ggplot2.org/current/geom_segment.html
df <- data.frame(a=c(1,2,3,4,5,6),T=c(3.88,3.92,3.96,4.00,4.04,4.08),X=c(2.7,2.9,2.7,2.0,2.7,2.0),Y=c(675.0,600.7,675.0,690.0,675.0,680.4),V=c(27.0,42,55,55,55,42),GD=c(27,70,125,181,236,279),ND=c(NA,2.7,0,0,0,5.4),ND2=c(NA,7,0,0,0,29.2),TID="t1")
qplot(X, Y, data = df, color = TID, group = TID)+
geom_path(linetype=5, size=1.5, arrow=arrow(angle=15, ends="both", type="closed"))+
geom_point (shape=19, size=5, fill="black")
这篇关于使用 R 绘制轨迹图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!