Placing an additional legend at the bottom of an existing legend in R

I have the following legend:

enter image description here

How to find the central text using "Middle and SD" so that it can be placed exactly under "--- emp".?

The code that generates this is:

L = list(bquote(Em.Mean ==.(new_avg)), bquote(Em.SD==.(new_std)), bquote(Th.Mean ==.(theor_avg)), bquote(Th.SD==.(theor_sd))) legend("topright", c(kids,"emp."), cex=0.7, bty="n", col=c(cm.colors(6), "red"), pch=c(rep(19, 6), -5), lty = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), ) # How can I locate this legend("topcenter", cex=0.5, bty="n", legend=sapply(L, as.expression)) 
+4
source share
1 answer

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")) 

enter image description here

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")) 

enter image description here

PS: I left the fields around the legends to show that the placement is perfect, but you can delete them using bty = "n"

+2
source

Source: https://habr.com/ru/post/1412543/


All Articles