rplotlyarrows

Adding arrow segments to a scatter plot in plotly


I have XY data I want to plot in a scatter plot using R's plotly package. For some of the points I have arrows, defined by their X and Y start and end coordinates, which I also want to plot.

Here are the data:

set.seed(1)

df <- data.frame(x=rnorm(100),y=rnorm(100),
                 arrow.x.start=NA,arrow.y.start=NA,
                 arrow.x.end=NA,arrow.y.end=NA)

arrow.idx <- sample(100,20,replace = F)

df$arrow.x.start[arrow.idx] <- df$x[arrow.idx]
df$arrow.x.end[arrow.idx] <- df$arrow.x.start[arrow.idx]+runif(length(arrow.idx),-0.5,0.5)
df$arrow.y.start[arrow.idx] <- df$y[arrow.idx]
df$arrow.y.end[arrow.idx] <- df$arrow.y.start[arrow.idx]+runif(length(arrow.idx),-0.5,0.5)

Using ggplot2 this is achieved using:

library(ggplot2)

ggplot(df,aes(x=x,y=y))+geom_point()+theme_minimal()+
  geom_segment(aes(x=arrow.x.start,y=arrow.y.start,xend=arrow.x.end,yend=arrow.y.end),arrow=arrow())

Which gives: enter image description here

In plotly this will plot the points:

plotly::plot_ly(marker=list(size=5,color="black"),type='scatter',mode="markers",x=df$x,y=df$y,showlegend=F) %>%
  plotly::layout(xaxis=list(title="x",zeroline=F,showticklabels=F,showgrid=F,showgrid=F),yaxis=list(title="y",zeroline=F,showticklabels=F,showgrid=F,showgrid=F))

So I'm trying to figure out how to add the arrows.

The add_segments has the x, xend, y, and yend arguments and adding that:

plotly::plot_ly(marker=list(size=5,color="black"),type='scatter',mode="markers",x=df$x,y=df$y,showlegend=F) %>%
  plotly::layout(xaxis=list(title="x",zeroline=F,showticklabels=F,showgrid=F,showgrid=F),yaxis=list(title="y",zeroline=F,showticklabels=F,showgrid=F,showgrid=F)) %>%
  plotly::add_segments(x=df$arrow.x.start,xend=df$arrow.x.end,y=df$arrow.y.start,yend=df$arrow.y.end,line=list(color="blue"))

Seems to add a point at the end of the line:

enter image description here

And I couldn't find in its documentation an argument that will add arrow head at the end of the line.

Any idea?


Solution

  • You can use annotations

    plot_ly(df) %>%
      add_markers(~x, ~y) %>%
      add_annotations( x = ~arrow.x.end,
                       y = ~arrow.y.end,
                       xref = "x", yref = "y",
                       axref = "x", ayref = "y",
                       text = "",
                       showarrow = T,
                       ax = ~arrow.x.start,
                       ay = ~arrow.y.start,
                       data = df[!is.na(df$arrow.x.start),])
    

    enter image description here