如何在一个情节的图例中将线条置于线条顶部?

use*_*627 8 plot r legend

我想在图例中的线条顶部添加透明框.

一个小例子:

xdata <- 1:7
y1 <- c(1,4,9,16,25,36,49)
y2 <- c(1, 5, 12, 21, 34, 51, 72)
y3 <- c(1, 6, 14, 28, 47, 73, 106 )
y4 <- c(1, 7, 17, 35, 60, 95, 140 )

plot(xdata, y1, type = 'l', col = "red")
lines(xdata, y2, type = 'l', col = "red", lty = 3)
lines(xdata, y3, type = 'l', col = "blue") 
lines(xdata, y4, type = 'l', col = "blue",  lty = 3)

# add legend with lines
legend("topleft", legend = c("Plot 1", "Plot 2", "Plot 3", "Plot 4"),
      lty = c(1,3,1,3), lwd = rep(1.3 ,4),
      col = c("blue", "blue", "red", "red") ,
      pch = rep(NA, 4), cex = 0.8, 
      x.intersp = 0.7, y.intersp = 1.2, bty = 'n')

# add boxes
legend("topleft", legend = c("", "", "", ""), lty = rep(0, 4), 
       col = c(adjustcolor(blues9[3], alpha.f = 0.6), 
               adjustcolor(blues9[3], alpha.f = 0.6), 
               adjustcolor("red", alpha.f = 0.1), 
               adjustcolor("red", alpha.f = 0.1)),
        pch = rep(15, 4), cex = 0.8, pt.cex = rep(2, 4),
        x.intersp = 0.7, y.intersp = 1.2, bty = 'n')
Run Code Online (Sandbox Code Playgroud)

产生:

在此输入图像描述

如您所见,这些框沿着图例中的线条向左移动.

如何设置框的对齐方式,使它们在图例中的线符号顶部水平居中?谢谢!

Mau*_*ers 8

由于您明确欢迎其他解决方案,因此可以使用 ggplot2

library(tidyverse);
data.frame(xdata, y1, y2, y3, y4) %>%
    gather(key, y, -xdata) %>%
    mutate(key = sub("y", "Plot ", key)) %>%
    ggplot(aes(xdata, y, colour = key, linetype = key)) +
    geom_line() +
    geom_ribbon(aes(ymin = y, ymax = y, fill = key), alpha = 0.2, colour = NA) +
    scale_colour_manual(
        values = c("Plot 1" = "red", "Plot 2" = "red", "Plot 3" = "blue", "Plot 4" = "blue"),
        name = "Legend") +
    scale_linetype_manual(
        values = c("Plot 1" = "solid", "Plot 2" = "dashed", "Plot 3" = "solid", "Plot 4" = "dashed"),
        name = "Legend") +
    scale_fill_manual(
        values = c("Plot 1" = "red", "Plot 2" = "red", "Plot 3" = "blue", "Plot 4" = "blue"),
        name = "Legend") +
    theme_bw() +
    theme(
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.justification = c(0, 1),
        legend.position = c(0.02, 0.98),
        legend.spacing.x = unit(0.5, 'cm'),
        legend.title = element_blank())
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

以下是主要调整的摘要:

  1. fill通过零宽度添加美感geom_ribbon.这将为我们提供图例中的彩色框.我们alpha = 0.2用来确保透明度.
  2. 我们手动给所有美学尺度命名相同.这防止三种不同的图例,并结合colour,linetypefill在一个单一的图例.
  3. 我们通过调整传奇位置
    • legend.positionlegend.justification确保图例位于左上角的绘图区域内.这些参数需要进行一些微调,具体取决于图例文字,
    • 删除图例标题legend.title = element_blank(),
    • 增加图例符号和文本之间的距离legend.spacing.x; 此参数可能需要进行一些手动微调,具体取决于您的个人喜好.

最后,我们做了一些小的调整,通过删除网格线(theme_bwpanel.grid.major = element_blank()and 结合panel.grid.minor = element_blank())来增加与基础R图的美学相似性.


Hen*_*rik 8

这里的线索是lwd在两个legend调用中指定相同的,即也在调用框中的地方lty = 0.

这是一个更简单的示例,只有与您的实际问题相关的参数:

plot(1)

# lines
legend(x = "topleft",
       legend = c("Plot 1", "Plot 2", "Plot 3", "Plot 4"), bty = 'n',
       lty = c(1, 3), lwd = 1,
       pch = NA,
       col = rep(c("blue", "red"), each = 2))

# boxes
legend(x = "topleft", 
       legend = rep("", 4), bty = "n",
       lty = 0, lwd = 1, # <- lwd = 1  
       pch = 15, pt.cex = 2,
       col = c(rep(adjustcolor(blues9[3], alpha.f = 0.6), 2),
               rep(adjustcolor("red", alpha.f = 0.1), 2)))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


如果你对盒子的彩色轮廓很好,一个legend电话就足够了.只需使用一个pch接受pt.bg(这里:22):

plot(1)
legend(x = "topleft",
       legend = c("Plot 1", "Plot 2", "Plot 3", "Plot 4"), bty = 'n',
       lty = c(1, 3), col = rep(c("blue", "red"), each = 2),
       pch = 22, pt.cex = 2, 
       pt.bg = c(rep(adjustcolor(blues9[3], alpha.f = 0.6), 2),
                 rep(adjustcolor("red", alpha.f = 0.1), 2)))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @ user321627我注意到你没有放置你的200赏金,也没有接受任何答案(这很好 - 两者都不是必需的).不过,我很好奇提供的答案有什么问题(如果有的话)? (2认同)

PKu*_*mar 5

这是我使用基础 R所做的

以下是您可以在最后进行的一些设置,以获得所需的结果。

seg.len :它基本上适用于基本 R 绘图图形中图例线的长度,我将此值设为 1.25

pt.cex :rep(4.4,4),所以我们把box作为正方形,我用4.4调整了这些正方形的值

x.intersp : 它处理图例文本和图例形状之间的空间

text.col :如果您对其进行评论,这是图例文本颜色的可选设置,您将获得图例文本为黑色(就像您的答案一样)

xjust: 值为 0 时,图例左对齐,0,5 时居中对齐,1 时右对齐

我已经反复更改了这些设置,以将这些设置作为最终结果。我希望这会对你有所帮助。我已经在机器上运行了很多次来解决这个问题。

最重要的是我玩过的所有设置,并得出了以下图表:

plot(xdata, y1, type = 'l', col = "red")
lines(xdata, y2, type = 'l', col = "red", lty = 3)
lines(xdata, y3, type = 'l', col = "blue") 
lines(xdata, y4, type = 'l', col = "blue",  lty = 3)
legend("topleft", legend = c("Plot 1", "Plot 2", "Plot 3", "Plot 4"), 
       lty = c(1,3,1,3), lwd = rep(1.3 ,4), 
       col = c("blue", "blue", "red", "red") , 
       pch = rep(NA,4), cex = 0.8, 
       text.col = c("blue", "blue", "red","red"),
       y.intersp=1.2, bty = 'n', x.intersp = .5,
       seg.len = 1.265, xjust =1)
legend("topleft", legend = c("", "", "", ""), lty = rep(0, 4),
       col = c(adjustcolor(blues9[3],alpha.f=0.6),
               adjustcolor(blues9[3],alpha.f=0.6),
               adjustcolor("red",alpha.f=0.1),
               adjustcolor("red",alpha.f=0.1)),
       pch = rep(15,4), bty ='n', cex = 0.8,
       pt.cex = rep(4.3, 4), y.intersp = 1.2, x.intersp =  0.1,
       xjust = 0)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

注意: 您已缩放图形以查看 R Studio 中的正确效果。