为每个图例标签显示两个符号

Gee*_*cid 6 plot symbols r data-visualization legend

使用pch我可以绘制任何符号并在图例中指定任何标签.但是如何为每个图例标签绘制两个符号?例如,在下面的图中,我想要■▲●与红色版本配对,所以我只有三个标签'a','b','c'显示在图例中,这六个符号.目前,基本的情节图例似乎只允许我为每个标签绘制一个符号:

plot(rnorm(50),pch=c(15:17),col=1:2)
legend('topleft',pch=c(15:17),col=1:2,legend=c("a","b","c"),cex=1.5)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Jos*_*ien 8

这并不太难lattice,因为它的key =参数需要在图例中包含任意数量的列.

library(lattice)

myPCH <- 15:17
Data  <- rnorm(50)
Index <- seq(length(Data))

xyplot(Data ~ Index, 
       pch = myPCH, col=1:2,
       key = list(space = "right", adj=1,
                  text = list(c("a", "b", "c"), cex=1.5),
                  points = list(pch = myPCH),
                  points = list(pch = myPCH,col=2)))
Run Code Online (Sandbox Code Playgroud)

我不知道如何在绘图区内包含传说,但是有了这种情节,最好还是把它放在外面.(编辑: @chl在评论中指出了几种方法来做到这一点.例如,要在图的左下方绘制关键字,请space = "right"在上面用其中一个替换corner = c(0,0)或者x = 0, y=0.2)

在此输入图像描述


Tee*_*ala 5

像chl指出的那样,也可以构建一个自定义的图例."图例"功能无形地返回图例框的边界以及图例文本的坐标.可以在没有符号的情况下绘制图例文本,然后使用"点"手动添加符号到返回的坐标.这不需要任何额外的图形包:

plot(rnorm(50), pch=c(15:17), col=1:2)
# Plot legend text, inset could be used to shift legend text to the right
pos <- legend('topleft', legend=c("a","b","c"), cex=1.5)
# Plot symbols in two columns, shifted to the left by 3 and 1 respectively
points(x=rep(pos$text$x, times=2) - c(3,1), 
    y=rep(pos$text$y, times=2), 
    pch=rep(c(15:17), times=2), col=rep(1:2, times=3))
Run Code Online (Sandbox Code Playgroud)

代码生成的图像