繁体   English   中英

如何用 2 列制作 R 图例?

[英]How to make R legend with 2 columns?

我想在我的图表上制作一个图例,它是由plot() function 生成的。原来的legend() function 将生成一个只有 1 列的列表。 我怎样才能制作一个有 2 列的图例?

通缉传奇

对于标准图,我无法在一次调用legend找到方法。

这是一个选项,绘制两个单独的图例:一个带有线和点,一个带有标签。 x.intersp可用于调整标签和线条之间的距离。

plot(cumsum(runif(n = 100)))

# draw legend with lines and point but without labels and box. x.intersp controls horizontal distance between lines
L = legend(x = 'bottom', legend = rep(NA,4), col=1:2, lty=c(1,1,2,2), ncol=2, bty='n', x.intersp=0.5, pch=c(1,2,1,2), inset=0.02)

# use position data of previous legend to draw legend with invisble lines and points but with labels and box. x.intersp controls distance between lines and labels
legend(x = L$rect$left, y = L$rect$top, legend = c('Group A', 'Group B'), col=rep(NA,2), lty=c(1,1), ncol=1, x.intersp = 3, bg = NA)

在此输入图像描述

检查一下:

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)))

在此输入图像描述

看起来 Victorp 在原始帖子的评论中回答了这个问题。 图例 function 中的 ncol 参数对我有用:

legend(locator(1), legend=c("name1","name2", "name3", "name4"), lty=2, col=c("black", "blue", "dark green", "orange"), ncol=2)

在此处输入图像描述

暂无
暂无

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

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