R:图例()错误

R : legend() error

我正在尝试为绘图生成图例,以显示颜色与绘图中聚类 # 的关系。我实际上并不需要它在情节中我只是想生成一个图例然后将其复制并粘贴到 powerpoint 幻灯片中。

我在这里找到了我想要的代码:

http://www.statmethods.net/advgraphs/axes.html

# Legend Example
attach(mtcars)
boxplot(mpg~cyl, main="Milage by Car Weight",
    yaxt="n", xlab="Milage", horizontal=TRUE,
   col=terrain.colors(3))
legend("topright", inset=.05, title="Number of Cylinders",
    c("4","6","8"), fill=terrain.colors(3), horiz=TRUE)

但我很难复制它。这是我的代码:

plot(seq(1,7), seq(1,7), col = c(1:7))
legend("topright", inset = .05, title = "Cluster Colors"
    ,fill = c(1:7), horiz=T)

当我 运行 它时,我得到这个错误:

Error in as.graphicsAnnot(legend) : 
  argument "legend" is missing, with no default

有什么建议吗?

原图有一个参数"legend",只是一个未命名的参数。如此处更新:

legend("topright", inset=.05, title="Number of Cylinders",
   legend =c("4","6","8"), fill=terrain.colors(3), horiz=TRUE)

那么,你需要的就是这个

plot(seq(1,7), seq(1,7), col = c(1:7))
legend("topright", inset = .05, title = "Cluster Colors",legend= c(1:7)
       ,fill = c(1:7), horiz=TRUE)

? 命令,如 ?legend,对于了解这些内容很有用。