Making a trajectory plot using R

前端 未结 1 1805
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 09:09

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 posit

相关标签:
1条回答
  • 2021-01-28 10:04

    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

    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")
    
    0 讨论(0)
提交回复
热议问题