I'm trying to make a dynamic plot, i.e. that I move the time slider and shows me the values for XVAR
and YVAR
in other time, I already got that. What I additionally need is that those points in the plot have a vertical segment to their respective height, i.e. from this
To this:
I've been told that geom_segment()
could do that but I don't understand how to manage the xend
and yend
parameters such that the positions of the segments are correct.
Here's my code so far:
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)
Any help would be appreciated. Many thanks.
CodePudding user response:
You can use geom_segment
like this.
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)