-2

... when specifying "topright" & put symbols on right side of legend rather than to the left of the labels.

Basically I want my legend to be in a box on the top right corner of my plot, and have all my text right-aligned, not left aligned.

My code is in a loop and applied to over 100 graphs, so I'm trying to keep the legend in the same spot in each graph.

I tried using just and adj which did not work for me.

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")
Jaap
  • 81,064
  • 34
  • 182
  • 193
draj01100
  • 13
  • 5
  • 1
    Possible duplicate of [How to make labels in the legend align right in R?](http://stackoverflow.com/questions/32031530/how-to-make-labels-in-the-legend-align-right-in-r) – Yannis Vassiliadis May 01 '17 at 23:07
  • I've tried adj as well, but the result is that although text is right-aligned, all the text in the legend moves to the center as if i selected "center" instead of "topright." It also moves out of the box which stays on the "topright." The symbols also stay on the "topright." It also doesn't switch the position of the symbols and the text. – draj01100 May 02 '17 at 03:08

1 Answers1

1

Example from ?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)
Vandenman
  • 3,046
  • 20
  • 33