[英]Legend appears, but it does not show color
我正在使用 R 進行繪圖。 當我的圖表繪制圖例時,圖例出現在我想要的位置,但顏色丟失。 mtcars 2
是mtcars
(預加載的數據集之一)的修改版本, mtcars
向數據集添加了模型和原產國。 mtcars.pca
是我命名我的冗余分析(vegan 下的mtcars.clust
函數),而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")
您需要在legend
指定一個顏色向量以及一個pch
:
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()
調用中使用相同的值。
另外,請注意如何在一次通過中繪制不同顏色的點。 我有一些示例和解釋,這些示例和解釋通過我在上面的代碼中使用的索引技巧來實現,這是我幾年前的一篇博客文章: http : //www.fromthebottomoftheheap.net/2012/04/11/customising -素食者排序圖/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.