简体   繁体   English

更改图例边框和图例标题之间的间距

[英]Change spacing between legend border and legend title

I am attempting to save a plot as a metafile (*.emf) using RStudio. 我试图使用RStudio将图表保存为图元文件(* .emf)。 The formatting of the plot looks okay in the *.emf file except that there is no gap between the top border of the legend and the legend title. 除了图例的顶部边框和图例标题之间没有间隙之外,图表的格式在* .emf文件中看起来没问题。 How can I add such a gap? 我该如何添加这样的差距? I am using base R . 我正在使用基地R

Here is the code. 这是代码。 I cannot upload an *.emf file to Stack Overflow. 我无法将* .emf文件上传到Stack Overflow。

cov       <- 1:20

B0.1      <-  0.2
B1.1      <-  0.2
B0.2      <- -0.2
B1.2      <-  0.1
B0.3      <- -0.6
B1.3      <- -0.02

y.1 <- exp(B0.1 + B1.1 * cov) / (1 + exp(B0.1 + B1.1 * cov))
y.2 <- exp(B0.2 + B1.2 * cov) / (1 + exp(B0.2 + B1.2 * cov))
y.3 <- exp(B0.3 + B1.3 * cov) / (1 + exp(B0.3 + B1.3 * cov))

par(mfrow=c(1,1), pty="s")

plot(cov, y.1, bty = "l", type = 'l', col = 'black', lwd = 2, lty = 1, 
     xlab = 'Cov', ylab = 'Probability', ylim=c(0,1))

     lines(cov, y.2, type = 'l', col = 'black', lwd = 2, lty = 2)
     lines(cov, y.3, type = 'l', col = 'black', lwd = 2, lty = 3)

     abline(v = 10, lwd = 2, lty = 6)

     title('My Plot')

     op <- par(cex = .67)

     legend("bottomright",          c('Prob 1', 'Prob 2', 'Prob 3'), 
                              col = c("black", "black", "black"), 
                              lty = c(1, 2, 3),
                              lwd = c(2, 2, 2),

                              y.intersp = c(1.5, 1.5, 1.5),

                              title = "Legend", cex = 1.00, text.width = 2.50)

Print the border of the legend afterwards with your own positions: 然后使用您自己的位置打印图例的边框:

First a legend without a border (set bty to "n" ), save the positions in "a": 首先是没有边框的传奇(将bty设置为“n”),将位置保存在“a”中:

a <- legend("bottomright",c('Prob 1', 'Prob 2', 'Prob 3'), 
       col = c("black", "black", "black"), 
       lty = c(1, 2, 3),bty="n",
       lwd = c(2, 2, 2),
       y.intersp = c(1.5, 1.5, 1.5),
       title = "Legend", cex = 1.00, text.width = 2.50)

Define your own positions of the rectangle: 定义您自己的矩形位置:

rect(a$rect$left, a$rect$top-a$rect$h[1], a$rect$left+a$rect$w, a$rect$h[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM