在R中添加行和图例

时间:2013-07-24 19:21:53

标签: r legend

我试图找出如何将点和线(颜色)添加到R中的图例。

我想要绘制的内容是这样的:

x = 1:10
y = sin(x)
z = cos(x)

plot(x,y,type='l',lty=2,col="blue")
points(x,z,pch=19,col="red")

#I know this legend command doesn't work, but I just want to explain what I want
legend("topleft",c("x","y"),pch=19,lty=2,col=c("red","blue")

正如您所看到的,我希望能够在图例中同时拥有点和线(具有正确的类型和颜色)。

1 个答案:

答案 0 :(得分:4)

您将NA值作为pch=lty=的其他参数。

legend("topleft",c("x","y"),pch=c(19,NA),lty=c(NA,2),col=c("red","blue"))