4

Is it possible to create arrows in plotly? I'm trying to plot directions, and the workaround I did was just to plot a straight line, which is just not the same as an arrow.

Basically, I have a data frame and want to create, for every set of points in the data frame, an arrow that goes from the origin axis (0,0) to the point in the data frame.

library(plotly)    
# Create a dataframe with a named point
df <- data.frame(x = 2, y = 2)
row.names(df) <- "individual A"

plot_ly() %>% 
add_annotations(x = df$x, y = df$y, text = row.names(df))

I think it should have something to do with add_annotations, as it creates arrows pretty easily, but I don't know how to make them start at the origin.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • 1
    geom_segment from ggplot2 would have been nice but the arrows are lost after converting into a plotly (using ggplotly) graphic. – MLavoie Jan 24 '18 at 00:04

1 Answers1

1

'ax' and 'ay' can be used in addition to 'x', 'y', so that you can define the coordinates of both ends of the arrow.

library(plotly)    
# Create a dataframe with a named point
df <- data.frame(x = 2, y = 2)
row.names(df) <- "individual A"

plot_ly() %>% 
  add_annotations(ax = 0, ay = 0, axref='x', ayref='y', x = df$x, y = df$y, xref='x', yref='y', text = row.names(df))
aspire
  • 155
  • 1
  • 1
  • 9