簡體   English   中英

出現圖例,但不顯示顏色

[英]Legend appears, but it does not show color

我正在使用 R 進行繪圖。 當我的圖表繪制圖例時,圖例出現在我想要的位置,但顏色丟失。 mtcars 2mtcars (預加載的數據集之一)的修改版本, 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 -素食者排序圖/

我遇到了 xts 對象中的下一個問題:我想用圖例繪制 xts 對象中的所有時間序列。 此外,大約有20個。

  • 我使用(錯誤):
plot(returns_xts)
addLegend(...)
  • 正確版本:
plot(returns_xts, legend.loc = "bottomright", col=1:20, lty = 1)
  • legend.loc參數
  • col = 1:20為您生成顏色

結果: 在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM