Having subsections in a legend of a plot in R?

2019-02-14 17:34发布

I generate the following legend:

legend(
  "bottomleft"
  , legend=c(
    expression(bold("Long:" ~ (w==10^2 ~ ";" ~ h==10^5)))
    , expression(q[c] == 0.00 ~ ";" ~ beta == 0)
    , expression(q[c] == 0.05 ~ ";" ~ beta == 2)
    , expression(q[c] == 0.10 ~ ";" ~ beta == 10)
    , expression(q[c] == 0.20 ~ ";" ~ beta == 10)
    , expression(q[c] == 0.40 ~ ";" ~ beta == 10)
    , expression(bold("Wide:" ~ (w==10^3 ~ ";" ~ h==10^3)))
    , expression(q[c] == 0.00 ~ ";" ~ beta == 0)
    , expression(q[c] == 0.05 ~ ";" ~ beta == 2)
    , expression(q[c] == 0.10 ~ ";" ~ beta == 10)
    , expression(q[c] == 0.20 ~ ";" ~ beta == 10)
    , expression(q[c] == 0.40 ~ ";" ~ beta == 10)
  )
  , col=c("n", 1, 2, 3, 4, 5, -1, 1, 2, 3, 4, 5)
  , lty=c(F, 1, 1, 1, 1, 1, -1, 2, 2, 2, 2, 2)
  , cex=.65
)

which gives me:

enter image description here

It would be nicer to have a heading, that also spans the "line+point" sample region:

enter image description here

How can I realize this? (I tried for example to set n or FALSE in the lty-section, but that did not work,...). I also failed with aligning the qc and beta-values, but thats a different story,...

2条回答
祖国的老花朵
2楼-- · 2019-02-14 17:52

Using @DWin's proposed solution method:

Set up an example plot:

plot(1:10,type="n")

Save the legend and phantom hide the titles to be written manually

saveleg <- legend(
  "bottomleft"
  , legend=c(
      expression(phantom(bold("Long:" ~ (w==10^2 ~ ";" ~ h==10^5))))
    , expression(q[c] == 0.00 ~ ";" ~ beta == 0)
    , expression(q[c] == 0.05 ~ ";" ~ beta == 2)
    , expression(q[c] == 0.10 ~ ";" ~ beta == 10)
    , expression(q[c] == 0.20 ~ ";" ~ beta == 10)
    , expression(q[c] == 0.40 ~ ";" ~ beta == 10)
    , expression(phantom(bold("Wide:" ~ (w==10^3 ~ ";" ~ h==10^3))))
    , expression(q[c] == 0.00 ~ ";" ~ beta == 0)
    , expression(q[c] == 0.05 ~ ";" ~ beta == 2)
    , expression(q[c] == 0.10 ~ ";" ~ beta == 10)
    , expression(q[c] == 0.20 ~ ";" ~ beta == 10)
    , expression(q[c] == 0.40 ~ ";" ~ beta == 10)
  )
  , col=c("n", 1, 2, 3, 4, 5, -1, 1, 2, 3, 4, 5)
  , lty=c(F, 1, 1, 1, 1, 1, -1, 2, 2, 2, 2, 2)
  , cex=.65
  , trace=TRUE
)

Then grab the y-axis values of the two bits of text to be replotted from the saved legend with newly specified x-axis values.

text(
    c(0.6,0.6),
    saveleg$text$y[c(1,7)],
    c(
      expression(bold("Long:" ~ (w==10^2 ~ ";" ~ h==10^5))),
      expression(bold("Wide:" ~ (w==10^3 ~ ";" ~ h==10^3)))
     ),
    cex=0.65,
    pos=4
)

Result:

enter image description here

查看更多
Anthone
3楼-- · 2019-02-14 18:04

A similar approach, but using the legend title, and creating two legends (without boxes) before adding the rectangle (box) afterwards

plot(1)
# legend 1
l1 <- legend(
  "topleft"
  , legend=c(expression(q[c] == 0.00 ~ ";" ~ beta == 0)
    , expression(q[c] == 0.05 ~ ";" ~ beta == 2)
    , expression(q[c] == 0.10 ~ ";" ~ beta == 10)
    , expression(q[c] == 0.20 ~ ";" ~ beta == 10)
    , expression(q[c] == 0.40 ~ ";" ~ beta == 10)),
  title = expression(bold("Long:" ~ (w==10^2 ~ ";" ~ h==10^5))),
  , col=c( 1, 2, 3, 4, 5)
  , lty=c(1, 1, 1, 1, 1)
  , cex=.65,
   bty='n')
# legend 2, placed directly below legend 1
l2 <- legend(x = l1$rect$left, y = with(l1$rect, top - h),
             legend =c(expression(q[c] == 0.00 ~ ";" ~ beta == 0)
                       , expression(q[c] == 0.05 ~ ";" ~ beta == 2)
                       , expression(q[c] == 0.10 ~ ";" ~ beta == 10)
                       , expression(q[c] == 0.20 ~ ";" ~ beta == 10)
                       , expression(q[c] == 0.40 ~ ";" ~ beta == 10)),
             title = expression(bold("Wide:" ~ (w==10^3 ~ ";" ~ h==10^3))),
              col=c( 1, 2, 3, 4, 5)
             , lty=c(  2, 2, 2, 2, 2)
             , cex=.65,
             bty='n')
# add the rectangle around the legend
rect(xleft = l1$rect$left, ybottom = with(l2$rect, top - h), 
     xright = l1$rect$left + max(l1$rect$w, l1$rect$w), ytop = l1$rect$top)

enter image description here

查看更多
登录 后发表回答