R 如何制作移动对象的速度剖面?

R 如何制作移动对象的速度剖面?,r,performance,plot,profile,R,Performance,Plot,Profile,我是一个R初学者,我面临以下问题。我有以下数据框: distance speed 1 61.0 36.4 2 51.4 35.3 3 42.2 34.2 4 33.4 32.8 5 24.9 31.3 6 17.5 28.4 7 11.5 24.1 8 7.1 19.4 9 3.3 16.9 10 0.5 15.5 11 4.4 15.1 12

我是一个R初学者,我面临以下问题。我有以下数据框:

       distance speed
1      61.0  36.4
2      51.4  35.3
3      42.2  34.2
4      33.4  32.8
5      24.9  31.3
6      17.5  28.4
7      11.5  24.1
8       7.1  19.4
9       3.3  16.9
10      0.5  15.5
11      4.4  15.1
12      8.5  15.5
13     13.1  17.3
14     18.8  20.5
15     25.7  24.1
16     33.3  26.3
17     41.0  27.0
18     48.7  27.7
19     56.6  28.4
20     64.8  29.2
21     73.6  31.7
22     83.3  34.2
23     93.4  35.3
列距离表示后续对象在特定点上的距离,列速度表示对象的速度。正如你所看到的,物体越来越接近这个点,然后它就离开了。我正在尝试制作它的速度剖面图。我尝试了下面的代码,但它没有给出我想要的绘图(因为我想显示当移动对象靠近并超过参考点时,它的速度是如何变化的)

该图如下所示:

然后,我尝试手动将前10个距离设置为负,这些距离早于零(0)。所以我得到了一个更接近我想要的图:

但有一个问题。距离不能定义为负。 总而言之,预期的情节如下(我对质量感到抱歉)

你对如何解决这个问题有什么想法吗?
提前谢谢你

您可以执行类似操作来自动计算更改点(以了解距离何时应为负值),然后将轴标签设置为正值

您的数据(以防有人需要它来回答):

现在,计算“实际”距离(接近为负值,后退为正值):

speed\u profile$real\u距离
    ggplot(speedprofile, aes(x = distance, y = speed)) +  #speedprofile is the data frame
  geom_line(color = "red") +
  geom_smooth() + 
  geom_vline(xintercept = 0) # the vline is the reference line
read.table(text="distance speed
61.0  36.4
51.4  35.3
42.2  34.2
33.4  32.8
24.9  31.3
17.5  28.4
11.5  24.1
7.1  19.4
3.3  16.9
0.5  15.5
4.4  15.1
8.5  15.5
13.1  17.3
18.8  20.5
25.7  24.1
33.3  26.3
41.0  27.0
48.7  27.7
56.6  28.4
64.8  29.2
73.6  31.7
83.3  34.2
93.4  35.3", stringsAsFactors=FALSE, header=TRUE) -> speed_profile
speed_profile$real_distance <- c(-1, sign(diff(speed_profile$distance))) * speed_profile$distance
breaks <- scales::pretty_breaks(10)(range(speed_profile$real_distance))

ggplot(speed_profile, aes(real_distance, speed)) +
  geom_smooth(linetype = "dashed") +
  geom_line(color = "#cb181d", size = 1) +
  scale_x_continuous(
    name = "distance",
    breaks = breaks,
    labels = abs(breaks) # make all the labels for the axis positive
  )
labels <- abs(breaks)
labels[(!breaks == 0)] <- sprintf("%s\n→", labels[(!breaks == 0)])

ggplot(speed_profile, aes(real_distance, speed)) +
  geom_smooth(linetype = "dashed") +
  geom_line(color = "#cb181d", size = 1) +
  scale_x_continuous(
    name = "distance",
    breaks = breaks,
    labels = labels,
  )