Confused_guy
Confused_guy

Reputation: 41

Plotly arrow from origin to certain point

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.

Upvotes: 4

Views: 2038

Answers (1)

aspire
aspire

Reputation: 195

'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))

Upvotes: 1

Related Questions