开发者

Change arrowhead of arrows()

开发者 https://www.devze.com 2023-02-27 16:49 出处:网络
i wo开发者_如何学运维nder if it is possible to change the arrowhead of an arrow drawn with arrows(). I looked through the documentation but all I found is that I can change the end of a line but not o

i wo开发者_如何学运维nder if it is possible to change the arrowhead of an arrow drawn with arrows(). I looked through the documentation but all I found is that I can change the end of a line but not of an arrow?

plot(c(1:10))
arrows(0,0,10,10)

Any help is appreciated :)


as explained in ?arrow , you can use length and angle to change the appearance of standard arrows. With lwd you can change the thickness, exactly like in lines(). Also lty works, although the result is often not exactly nice.

A whole set of examples :

plot(c(0:10),type="n")

arrows(1,0,2,1,length=0.2,angle=20)
arrows(1,1,2,2,length=0.1,angle=40,lwd=3)

invisible(mapply(arrows,
        rep(c(3,6),each=4),rep(3:6,2),
        rep(c(5,8),each=4),rep(5:8,2),
        angle=seq(10,40,length.out=8),
        length=rep(seq(0.1,0.3,length.out=4),2),
        lwd=rep(1:4,each=2))
)


Karline Soetaert's package shape is useful for this purpose:

library(shape)
plot(c(0,2),c(-2,2), col=NA)
Arrows(c(0,1.7),c(1.3,-1.8),c(0.8,1.1),c(1.2,-1), lwd=2)

The default is a filled, curved arrowhead that's pleasant to look at:

Change arrowhead of arrows()

Gotta love CRAN!


If the other answers don't give you enough control of your arrows using the arrows function, you can use the function my.symbols, from TeachingDemos package. This allows you to create your own custom arrows and plot them.

The function ms.arrows, in the same package, shows one method of doing this: you can modify that function (or other ms.* functions) to create any type of arrow you want, and later use my.symbols to plot them.

Edit

Here is an example as requested:

library(TeachingDemos)

ms.arrowhead <- function(angle, ...) {
  xy <- cbind( c(-1, -0.75, -1, 0), 
               c(-0.5, 0, 0.5, 0) )
  xy <- xy %*% matrix(c(cos(angle),-sin(angle),sin(angle),cos(angle)), 2)
  xspline(xy, shape=c(0, -1, 0, 0), open=FALSE, ...)
}

plot(1:10, 1:10)
my.symbols(1:10, 1:10, ms.arrows, angle=seq(pi, 0, length=10),
           col='blue', adj=1, length=0, symb.plots=TRUE)
my.symbols(1:10, 1:10, ms.arrowhead, angle= seq(pi, 0, length=10), 
           col='green', inches=0.5, lwd=2, symb.plots = TRUE)

This uses the xspline function to draw a triangle with a curved back as the arrowhead. The points can be modified to get a different shaped arrowhead, or another method can be used for the shape.

0

精彩评论

暂无评论...
验证码 换一张
取 消