2017-05-01 53 views
-2

...指定“topright”时&将符号放在图例右侧而不是标签的左侧。如何在我的图例框中对齐文本和符号

基本上我希望我的图例出现在我的情节右上角的一个框中,并且所有文本都是右对齐的,而不是左对齐。

我的代码在一个循环中并应用于超过100个图表,因此我试图将图例保留在每个图形中的相同位置。

我尝试使用justadj这不适合我。

legend("topright", legend=c("data", "data", "data", "data", "data", 
       "data", "data", "data", "data", "data", "data"), 
     ncol = 1, 
     lty = c(1, NA, NA, NA, NA, 1, 1, 1, 2, 2, 2), 
     pch = c(NA, sym[1], sym[2], sym[2], sym[3], 19, 17, 15, 19, 17, 15), 
     col = c(blue, aa, aa, aa, aa, gr, bl, pu, gr, bl, pu), 
     pt.bg = c(NA, a, b, c, d, NA, NA, NA, NA, NA, NA), 
     pt.cex = c(NA, 1, 1, 1, 1, .5, .5, .5, .5, .5, .5), 
     cex = .75, 
     lwd = c(2, NA, NA, NA, NA, 1, 1, 1, 1, 1, 1), 
     text.col = "black") 
+1

的可能的复制[如何使在图例标签中的R右对齐?](http://stackoverflow.com/questions/32031530/how-to-使用标签在传奇对齐在右边r) –

+0

我试过调整,但结果是,虽然文本是右对齐,图例中的所有文本移动到中心如果我选择了“中心”而不是“topright”。它也移出了保留在“正面”的框。这些符号也停留在“正面”。它也不会切换符号和文本的位置。 – draj01100

回答

1

实施例从?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) 
+0

这有效,但符号仍然在文本的左侧。我仍然无法解决这个问题。 – draj01100

+0

你想要左侧的标签和右侧的线? – Vandenman

+0

是的 - 左侧的标签和右侧的线条/符号。 – draj01100