Reputation: 109232
The legend that R creates when you call legend()
has the symbols (or line types etc) on the left and the labels on the right. I'd like it the other way round, i.e. labels on the left (right-aligned) and the symbols on the right.
I know that I can use adj
to adjust the position of the labels, but with this they are not aligned properly anymore. If I set adj=2
for example, the labels are to the left of the symbols, but the end of the text is not aligned with the symbols.
Any pointers on how to do this using either the standard legend()
function or a package would be appreciated.
Upvotes: 10
Views: 2265
Reputation: 5691
If you set trace = TRUE
and then save the output, you can draw the legend and then add the labels with a call to text()
using the coordinates given by trace
, setting pos = 2
for right alignment. Here's an example:
set.seed(1)
plot(1:10,runif(min=0,max=10,10),type='l',ylim=c(0,10),xlim=c(0,10),col=1)
lines(1:10,runif(min=0,max=10,10),col=2,lty=2)
lines(1:10,runif(min=0,max=10,10),col=3,lty=2)
a <- legend(1,10,lty=1:3,col=1:3,legend=c("","",""),bty="n",trace=TRUE)
text(a$text$x-1,a$text$y,c("line 1","line 2","line 3"),pos=2)
Upvotes: 15