【问题标题】:Legend appears, but it does not show color图例出现,但不显示颜色
【发布时间】:2017-03-17 15:12:02
【问题描述】:

我正在使用 R 进行绘图。当我的图表绘制时,图例出现在我想要的位置,但颜色缺失。 mtcars 2mtcars(预加载的数据集之一)的修改版本,它将模型和原产国添加到数据集中。 mtcars.pca 是我命名的冗余分析(vegan 下的 rda 函数),mtcars.clust 的标题是 mtcars 的连续因子的层次聚类(vegan 的 hclust 函数) 下面是我与 mtcars2 一起使用的代码。

pca.fig = ordiplot(mtcars.pca, type = "none", las=1, xlim=c(-15,15), ylim = c(-20,10))        

points(pca.fig, "sites", pch = 19, col = "green", select = mtcars2$origin =="domestic")  
points(pca.fig, "sites", pch = 19, col = "blue", select = mtcars2$origin =="foreign")  

ordiellipse(mtcars.pca, mtcars2$origin, conf = 0.95, label = FALSE) 
ordicluster(mtcars.pca, mtcars.clust, col = "gray")   

legend("bottomright", title="Car Origin", c("domestic", "foreign"), col = "origin")

【问题讨论】:

  • 请提供一个可重现的例子,什么是 mtcars.pca?什么是 mtcars2?什么是 mtcars.clust?
  • 抱歉,mtcars 2 是 mtcars 的修改版本(预加载的数据集之一)。将模型和原产国添加到数据集中。 mtcars.pca 是我命名的冗余分析(vegan 下的 rda 函数),mtcars.clust 的标题是 mtcars 的连续因子的层次聚类(vegan 的 hclust 函数)
  • 使用col=c("blue", "green"), lty = 1fill=c("blue", "green") [更改以满足您的需要]
  • user20650 非常感谢。

标签: r vegan


【解决方案1】:

我来到这个问题时遇到了 xts 对象中的下一个问题: 我想用图例在 xts 对象中绘制所有时间序列。此外,大约有 20 个。

  • 我用过(错误):
plot(returns_xts)
addLegend(...)
  • 正确版本:
plot(returns_xts, legend.loc = "bottomright", col=1:20, lty = 1)
  • legend.loc参数
  • col = 1:20 为您生成颜色

结果:

【讨论】:

    【解决方案2】:

    您需要在legendpch 中指定颜色向量:

    library("vegan")
    data(dune, dune.env)
    ord <- rda(dune)
    plot(ord, type = "n")
    cols <- c("red","blue","green")
    points(ord, col = cols[dune.env$Use], pch = 19)
    legend("bottomright", legend = levels(dune.env$Use), bty = "n",
           col = cols, pch = 19)
    

    如果您不添加pch 而只是使用col = cols legend() 不会显示任何点。因为您在points() 调用中使用了pch = 19,所以在legend() 调用中使用相同的。

    另外,请注意如何在一次通过中绘制不同颜色的点。我有一些示例和解释通过我在上面的代码中使用的 indexing 技巧在几年前的博客文章中实现这一点:http://www.fromthebottomoftheheap.net/2012/04/11/customising-vegans-ordination-plots/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 2010-12-19
      • 1970-01-01
      • 2021-12-02
      相关资源
      最近更新 更多