使用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)
答案 0 :(得分: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)))
我不知道如何在绘图区内包含传说,但是有了这种情节,最好还是把它放在外面。 (编辑: @chl在评论中指出了几种方法来执行此操作。例如,要绘制图中左下方的键,请替换上面的space = "right"
使用corner = c(0,0)
或x = 0, y=0.2
)
答案 1 :(得分: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))