将垂直线段添加到动态条形图 ggplot-plotly

Add vertical segments to a dynamic barplot ggplot-plotly

我正在尝试制作一个动态图,即我移动时间滑块并在其他时间显示 XVARYVAR 的值,我已经知道了。我还需要的是图中的那些点有一个垂直段到它们各自的高度,即从这个

为此:

有人告诉我 geom_segment() 可以做到这一点,但我不明白如何管理 xendyend 参数以使段的位置正确.

到目前为止,这是我的代码:

library(plotly)
library(tidyverse)

XVAR<-seq(from=1,to=10)
Time<-seq(from=1,to=10)
dat2<-expand_grid(XVAR,Time)
set.seed(1)
dat2$YVAR<-runif(100,0,10)

pl <- 
ggplot(dat2, aes(x=XVAR,y=YVAR, frame = Time)) +
geom_point()

ggplotly(pl)

如有任何帮助,我们将不胜感激。非常感谢。

你可以这样使用geom_segment

pl <- ggplot(dat2, aes(x=XVAR,y=YVAR, frame = Time)) +
  geom_point() +
  geom_segment(aes(x = XVAR, xend = XVAR, y = 0, yend = YVAR), color = "red")

ggplotly(pl)