如何在R中添加多个组的图例?

时间:2017-10-21 18:13:55

标签: r legend

我有一个散点图,有三种不同的颜色和两个不同的绘图符号。因此,我需要一个包含6个对象/文本的图例。我的数据格式为var1,var2,group1,group2。

如何创建图例?

1 个答案:

答案 0 :(得分:0)

只需为六种可能性中的每一种指定颜色,符号和标签。

## Some bogus data
set.seed(2017)
x = runif(100)
y = runif(100)
COL = rep(1, 100)
COL[x< 1/3] = 2
COL[x> 2/3] = 3
Symb = rep(15, 100)
Symb[y<1/2] = 16

## Plot and legend
plot(x,y, pch=Symb, col=COL, xlim = c(0,1.15))
legend("topright", legend=LETTERS[1:6], col=1:3, pch=15:16)

Plot with legend

相关问题