# 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) }) }
線だけの矢印なら
# load the function source('arrows_on_axes_function.R') svg('arrows_on_axes.svg', height=3, width=3) par(plt=c(25, 92.5, 17.5, 92.5)/100, tck=0.02, xpd=NA) plot(x=c(-5, 5.25), y=c(-1, 1.25), type='n', xlab='', ylab='', axes=F) usr = par('usr') axis(1, col=NA, col.ticks=1, mgp=c(0,2,0)/10) axis(2, col=NA, col.ticks=1, mgp=c(0,3,0)/10, las=1) mtext(1, text='x', line=1.5) mtext(2, text='y', line=2.5, las=1) curve(add=T, expr=cos, from=usr[1], to=usr[2], lty=2) lines(x=c(usr[c(1,1,2)]), y=c(usr[c(4,3,3)])) # use the function axis_arrow(0.05) # example using other parameters # axis_arrow(0.05, 1, color='gray') graphics.off()
使用例は、適当にプロットしたグラフを
使用例の結果を
↓ オプションの