2015-08-16 51 views

回答

4

?legend

## right-justifying a set of labels: thanks to Uwe Ligges 
x <- 1:5; y1 <- 1/x; y2 <- 2/x 
plot(rep(x, 2), c(y1, y2), type = "n", xlab = "x", ylab = "y") 
lines(x, y1); lines(x, y2, lty = 2) 
temp <- legend("topright", legend = c(" ", " "), 
      text.width = strwidth("1,000,000"), 
      lty = 1:2, xjust = 1, yjust = 1, 
      title = "Line Types") 
text(temp$rect$left + temp$rect$w, temp$text$y, 
    c("1,000", "1,000,000"), pos = 2) 

enter image description here

編輯:

我無法找到一個方法來自動對齊與傳說右邊的線條。但是,您可以使用text展示位置和選項bty="n"來生成圖例以刪除邊框。

## similar to above 
x <- 1:5; y1 <- 1/x; y2 <- 2/x 
plot(rep(x, 2), c(y1, y2), type = "n", xlab = "x", ylab = "y") 
lines(x, y1); lines(x, y2, lty = 2) 
temp <- legend("topright", legend = c(" ", " "), 
      text.width = strwidth("1,000,000"), 
      lty = 1:2, xjust = 1, yjust = 1, bty="n") 

text(4, 2,"lbl1") 
text(3.9, 1.92,"mylbl2") 

enter image description here

+0

是的,在傳說中,我可以使所有標籤對齊嗎?在你的例子中,「1000」和「1000000」對齊,然而,它們的對應線仍然左對齊,我們可以使兩者都對齊嗎?非常感謝!!! –

+0

或者我可以在文本的右側添加行嗎? –

+0

我解決了我提到你的代碼的問題,謝謝! –