【问题标题】:Add vertical segments to a dynamic barplot ggplot-plotly将垂直线段添加到动态条形图 ggplot-plotly
【发布时间】:2021-12-08 16:17:35
【问题描述】:

我正在尝试制作动态图,即我移动时间滑块并在其他时间向我显示 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)

任何帮助将不胜感激。非常感谢。

【问题讨论】:

    标签: r ggplot2 plotly ggplotly


    【解决方案1】:

    你可以像这样使用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)
    

    【讨论】: