#矢印を使う関数 fun<-function(what, args, x, y, size=c(1, 1)) { xy<-locator(1) #defalt size <- c(1, 1) #現在のデバイスの情報取得 pars <- par() #縦と横の幅[インチ] fin <- pars$fin #余白の全体の割合[left,right,top,bottom] pltin <- pars$plt * fin[c(1, 1, 2, 2)] #現在の余白[bottom, left, top, right][インチ] mai <- c(pltin[3], pltin[1], fin[2]-pltin[4],fin[1]-pltin[2]) #現在の座標値の情報 usr <- pars$usr #1座標が何インチか計算 uin <- c((fin[1]-(mai[2]+mai[4]))/(usr[2]-usr[1]), (fin[2]-(mai[1]+mai[3]))/(usr[4]-usr[3])) #デバイス左端からxyのx座標までの距離[インチ] xi <- mai[2] + (xy$x - usr[1]) * uin[1] #デバイス下端からxyのy座標までの距離[インチ] yi <- mai[1] + (xy$y - usr[3]) * uin[2] size <- size/2 if(!missing(x) && !missing(y)) { mx <- xy$x + pretty(c( -size, size), 20)/uin[1] my <- xy$y + pretty(c( -size, size), 20)/uin[2] dx <- abs(x-mx) dy <- abs(y-my) arrows(mx[dx == min(dx)],my[dy == min(dy)],x,y,length = 0.1) } #新しく作るデバイスの余白設定 newmai <- c(yi-size[2],xi-size[2],fin[2]-yi-size[2],fin[1]-xi-size[1]) newmex<-sqrt(max(size)/min(fin)) on.exit(par(pars[c("usr","mex","mai")])) par(mex=newmex,mai=newmai) par(new=T) do.call(what, args) par(pars[c("usr","mex","mai")]) } #使用例 plot(rnorm(10), type="l") fun("plot", list(rnorm(5), xlab="", ylab="", sub="test"), 4, 1)