Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R plot arrow head type and size

Tags:

plot

r

draw

I am trying to plot an arrow on a base R plot. I would like the arrow head to be a small equilateral triangle. I hoped the arr.type argument of arrows would allow an option for a small arrow, but that doesn't seem to be the case.

arrows(as.Date('1-Feb-15', "%d-%b-%y"), 50, as.Date('1-May-15', "%d-%b-%y"), 50, col = 'dark orange', lwd=5, arr.type = 'simple', arr.width=3)

Is there a simple way to make the arrow head small?

like image 649
Jim Avatar asked Dec 01 '17 23:12

Jim


2 Answers

I don't know of a way to get a filled arrow head with base arrows. However, you can do it with the Arrows function from the shape package:

library(shape)

plot(NA,NA, xlim=c(0,10), ylim=c(0,10))
Arrows(1,3,4,6,lwd=2, arr.type="triangle")
Arrows(5,7,6,9, arr.type="triangle", arr.width=0.5)

enter image description here

grid graphics also has filled arrows. Here's a ggplot2 example. type="closed" gives filled arrow heads. angle and length set the arrows' size and shape:

library(ggplot2)

n=8
ggplot(mtcars[1:n,][order(mtcars$wt[1:n]),], aes(x=wt, y=mpg, xend=lead(wt), yend=lead(mpg))) +
  geom_segment(arrow=arrow(type="closed", 
                           angle=seq(10,80, length=n), 
                           length=unit(seq(8,4,length=n),"mm"))) 

enter image description here

like image 154
eipi10 Avatar answered Oct 04 '22 22:10

eipi10


In case you want to stick to base R, you can read in the documentation that one can change the "width" and "height" of the head with the parameters "angle" and "length":

https://stat.ethz.ch/R-manual/R-patched/library/grid/html/arrow.html

like image 36
Coenobite Avatar answered Oct 04 '22 20:10

Coenobite