Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve the arrow in geom_segment can‘t display under plotly

Tags:

r

ggplot2

plotly

I draw a picture using ggplot2. I use arrow option in geom_segment because of directed picture. But when I display my pic under plotly using ggplotly, the Arrow disappear. How can I display the arrow ? You can run the following code to debug.

positiox_positiony <- data.frame(x_position=1:2,y_position=3:4)
node_edge_xy <- data.frame(x=1,y=3,xend=2,yend=4)
p <- ggplot(positiox_positiony,aes(x=x_position,y=y_position))+
  geom_point(color="red",size=10,alpha=0.8)+
  geom_segment(data=node_edge_xy,aes(x = x,y = y,xend = xend,yend = yend),
               arrow = arrow(length = unit(0.25,"cm"),ends="last",type="closed"))
p

ggplotly(p)
like image 696
Jiajin.Chen Avatar asked Oct 17 '22 19:10

Jiajin.Chen


1 Answers

An inelegant solution: add the arrow using plotly annotations.

g <- ggplotly(p) %>%
      layout(annotations=list(
         list(
           x=positiox_positiony$x_position[2],
           y=positiox_positiony$y_position[2],
           showarrow=TRUE,
           xref = "x",
           yref = "y",
           arrowhead = 2,
           arrowsize = 1.5,
           ax=-20,
           ay=20
         )
))
g

enter image description here

like image 74
Marco Sandri Avatar answered Oct 21 '22 10:10

Marco Sandri