2017-01-15 2 views
-1

下の例の最後の行では、posのいくつかの異なる値を手作業で試してから、前の行に既にプロットされていたlabelsです。適切な値posを自動的に見つける方法はありますか?軸ラベルをプロットするための 'pos'の値を自動的に調べる

dev.off() 
windows(width = 8, height = 6) 
par(mai = c(0.3, 2.5, 0.3, 0.3)) #bottom, left, top and right 
set.seed(42) 
plot(rnorm(15,10,1),rnorm(15,10,1), type = "p", 
    ylim = c(5,15), xlim = c(5,15), xlab = "", ylab = "", 
    xaxt = "n", yaxt = "n", yaxs="i", xaxs="i") 
axis(2, at = c(5,10,15), labels = c("This one", "Particularly long one", "two"), las = 2) 
axis(2, at = 10, pos = 2, labels = "Y Axis Label", font = 2, tick = FALSE, cex.axis = 1.5) 

答えて

1

あなたはpar(usr)[1]をつかむと位置を取得するために、あなたの長いラベルのstrwidthを引くことができます:あなたは、この後に対話的に画像のサイズを変更する場合は、もちろん

dev.off() 
windows(width = 8, height = 6) 
par(mai = c(0.3, 2.5, 0.3, 0.3)) #bottom, left, top and right 
set.seed(42) 
plot(rnorm(15, 10, 1),rnorm(15, 10, 1), type = "p", 
    ylim = c(5, 15), xlim = c(5, 15), xlab = "", ylab = "", 
    xaxt = "n", yaxt = "n", yaxs= "i", xaxs= "i") 
axis(2, at = c(5, 10, 15), 
    labels = c("This one", "Particularly long one", "two"), las = 2) 

# get the position based on the long string width and par('usr')[1] 
pos <- par('usr')[1] - strwidth("Particularly long one") 

axis(2, at = 10, pos = pos, 
    labels = "Y Axis Label", font = 2, tick = FALSE, cex.axis = 1.5) 

enter image description here

を、全ての賭けオフです。

+1

@DarshanBaral確かに、 'max'、' which.max'、または 'which'のいくつかの組み合わせが役に立つはずです。 'max(strwidth(label_variable))'が最大値をとり、 'strwidth(label_variable)== max(strwidth(label_variable))'が最大値を通知します。 – Jota

関連する問題