簡體   English   中英

如何使圖例中的標簽在R中對齊?

[英]How to make labels in the legend align right in R?

在此輸入圖像描述

如圖所示,如何使這些標簽對齊?

我在R中使用傳奇

來自?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)

在此輸入圖像描述

編輯:

我找不到一種方法來自動將圖例與右側的線對齊。 但是您可以使用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")

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM