legend
coordinates can be assigned to an object. These coordinates can be used to place the second legend. For instance,
x <-10:1 y <-11:20 plot(x,y,type="h") my.legend.size <- legend("topright",c("Series1","Series2","Series3")) > my.legend.size $rect $rect$w [1] 1.599609 $rect$h [1] 1.506977 $rect$left [1] 8.760391 $rect$top [1] 20.36 $text $text$x [1] 9.266641 9.266641 9.266641 $text$y [1] 19.98326 19.60651 19.22977
Using $ rect $ left, $ rect $ top and $ rect $ h, you can provide new coordinates for the second legend.
#second legend legend(x=my.legend.size$rect$left,y=my.legend.size$rect$top- my.legend.size$rect$h,c("Series4","Series5","Series6"))
I noticed that the width of your second legend is greater than the first. The trick here is to get the coordinates of the second legend, but not print it using ,plot = FALSE
The x coordinate of the second legend my.legend.size2$rect$left
.
x <-10:1 y <-11:20 plot(x,y,type="h") my.legend.size <-legend("topright",c("Series1","Series2","Series3")) my.legend.size2 <-legend("right",c("Long series name","Series5","Series6"),plot = FALSE) legend(x=my.legend.size2$rect$left,y=my.legend.size$rect$top- my.legend.size$rect$h,c("Long series name","Series5","Series6"))
PS: I left the fields around the legends to show that the placement is perfect, but you can delete them using bty = "n"