r - R:图例()错误

标签 r plot legend

我正在尝试为图生成图例,以显示颜色与图中群集#的关系。我实际上只需要生成图例,然后将其复制并粘贴到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("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命令对于了解这些情况很有用。

关于r - R:图例()错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28997011/

相关文章:

excel - 如何建立一个矩阵相乘的简单优化模型?

miktex 和 pandoc 的相对系统路径 - 打包为 Windows 桌面应用程序的 Shiny 应用程序

python - 如何在 matplotlib 上为一组子图定义单个图例?

python - 如何绘制多个 Pandas 列

r - 在 R 中增量绘图而不是重置

r - 在单个图例中设置多种文本大小

Matlab:更改图例中条目的顺序

r - 如何在 R 中像在 C# 中一样使用 linq 进行函数链接,我们有方法链接?

r - 如何在R中将flextable保存为png

r - 为 R 图像中的颜色渐变创建图例、键或色带()