2015-04-17 31 views
9

我想在我的图上做一个图例,它是由plot()函数生成的。原始的legend()函数将生成一个只有1列的列表。我如何制作一个有2列的图例?如何用2列制作R图例?

Wanted legend

+0

'NcoI位= 2'在'legend'? – Victorp

+1

但是我只需要每行1个文本标签 –

+0

@FelixChan每行一个文本标签 – RUser

回答

8

我无法找到一个方法来做到这一点,以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) 

enter image description here

3

检查:

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

enter image description here

+0

我想OP是要求2x2行+ 2x1的字符串。 –

+0

@帕斯卡编辑了答案 – RUser