# function for adding arrows to axes.
# use when the plot area already exists.
axis_arrow = function(width, height=0.35, color=1) {

    # width : ratio to minimum length of axes
    # height : ratio to width

    # prepare coordinates
    usr = par('usr')
    xlen = diff(usr[1:2]) * width
    ylen = diff(usr[3:4]) * width
    mat = rbind(
        # X and Y coordinates of an arrow on the horizontal axis
        c(rep(usr[2], 3) + c(0, 0, xlen),
          rep(usr[3], 3) + rep(ylen * height, 3) * c(1, -1, 0)),

        # X and Y coordinates of an arrow on the vertical axis
        c(rep(usr[1], 3) + rep(xlen * height, 3) * c(1, -1, 0),
          rep(usr[4], 3) + c(0, 0, ylen))
    )

    # draw arrows
    apply(mat, 1, function(r) {
        polygon(x=r[1:3], y=r[4:6], xpd=NA, border=NA, col=color)
    })
}