Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a double line / double arrow in R ggplot2

Tags:

r

ggplot2

I am trying to create arrows with a double line. I couldn't find a linetype with two parallel lines. This is what i have so far:

library(ggplot2)
library(grid)

df_dblarrow <- data.frame(x0=1,y0=1,x1=3,y1=1)
br <- 0.02

p <- ggplot(df_dblarrow, aes(x=x0,y=y0))+
  geom_point(size=5)+
  ylim(0,2)

#create a single arrow
(p <- p + geom_segment(aes(xend=x1,yend=y1), 
                 arrow = arrow(angle=30,length=unit(0.1,"npc"), type = "open")))

#create a two parallel lines 0.02 higher and lower (and a bit shorter)
(p <- p + geom_segment(aes(x=x0,y=(y0+br),xend=(x1-0.015),yend=(y1+br))) +
     geom_segment(aes(x=x0,y=(y0-br),xend=(x1-0.015),yend=(y1-br))))

This gives a graph with tree lines, in stead of the 2 i want, so i have to remove the middle one. enter image description here This is becoming too hacky. I've thought of using a wide black line, with a thinner white line on top, but than my arrowhead would be wide as well. Does anyone have a better idea?

like image 658
RHA Avatar asked Mar 24 '26 01:03

RHA


1 Answers

You could make the middle line a tiny length (can't be zero or arrow direction is indeterminate)

#create a single arrow
p + 
  geom_segment(aes(x= x1 -(x1-x0)/100, xend=x1, yend=y1), 
               arrow = arrow(angle=30, length=unit(0.1,"npc"), 
                             type = "open")) +
#create a two parallel lines 0.02 higher and lower (and a bit shorter)
  geom_segment(aes(x=x0,y=(y0+br),xend=(x1-0.015),yend=(y1+br))) +
  geom_segment(aes(x=x0,y=(y0-br),xend=(x1-0.015),yend=(y1-br)))

enter image description here

like image 131
arvi1000 Avatar answered Mar 26 '26 15:03

arvi1000



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!