简体   繁体   English

R图例未按点显示颜色

[英]R plot legend not showing colors according to points

So I have made a plot in R, with a lot of different colors indicating which of my 23 categories a point belongs to. 因此,我在R中作了一个绘图,用许多不同的颜色指示该点属于我23个类别中的哪个类别。 The colors of points are added through a vector (stratumcol, which is a factor with 23 levels). 点的颜色通过矢量添加(stratumcol,这是一个具有23个级别的因子)。

When I add the legend, trying to let that show the colors and their category, it seems they do not match (tested using ordihull, see picture below). 当我添加图例时,尝试让其显示颜色及其类别,似乎它们不匹配(使用ordihull测试,请参见下图)。

This is my plot code: 这是我的情节代码:

plot(pca_nmdsscores, type = "n")
points(pca_nmdsscores, col=stratumcol, cex=1.5, pch = 15)
legend("right","top",levels(stratumcol),cex=.8, col = as.numeric(stratumcol), pch =15, lty = 0) # pch = stratumcol
ordihull(pca_nmdsscores, groups = stratumcol,draw = "polygon", col ="purple",label = T, show.groups = "LateMoistRich")
ordihull(pca_nmdsscores, groups = stratumcol,draw = "polygon", col ="blue",label = T, show.groups = "MidWetPoor")

Here my Rplot should be visible. 在这里我的Rplot应该可见。 As you can see, my category "MaleMoistRich" connects the points with the pink-ish color, but in the legend this color is named "MidMoistRich". 如您所见,我的类别“ MaleMoistRich”将点与淡粉色连接起来,但是在图例中,该颜色称为“ MidMoistRich”。

The same for "MidWetPoor", connecting the mid-blue points, in the legend this color refers to "LateMoistPoor". 连接中间的蓝色点的“ MidWetPoor”也是如此,在图例中此颜色表示“ LateMoistPoor”。

How do I solve this problem? 我该如何解决这个问题?

I tried looking for solutions, but didn't come across any that could solve it - including "unique" (which doesn't change anything, since my palette has been defined with 23 colors, so no need to recycle those anyway) 我尝试寻找解决方案,但没有遇到任何可以解决的问题-包括“独特”(不会更改任何内容,因为我的调色板已定义为23种颜色,因此无论如何都无需回收这些颜色)

[Plot from R, showing legend and points color][1] [R中的图,显示图例和点颜色] [1]

Ok - so since I'm a newbie I can't upload an image of my plot... 好的-因为我是新手,所以无法上传自己的地块图片...

But see it here instead: http://i.stack.imgur.com/pzn2y.png 但是请在此处查看: http : //i.stack.imgur.com/pzn2y.png

/thanks /谢谢

Edit: The solution was to not use levels() on my factor! 编辑:解决方案是不使用我的水平! Not in legend = levels(stratumcol), nor in col = levels(stratumcol). 不在图例=级别(stratumcol)中,也不在col =级别(stratumcol)中。 Richard and DeveauP suggested levels might be the problem. Richard和DeveauP建议级别可能是问题。

This created a new problem: my legen displayed the whole factor, not just the levels in it (but the colors corresponded to the correct point colors, which was the original problem). 这就产生了一个新问题:我的腿部显示了整个因素,而不仅仅是其中的水平(而是颜色对应于正确的点颜色,这是最初的问题)。

this new problem was solved by using "unique()" instead of "levels()". 通过使用“ unique()”而不是“ levels()”解决了这个新问题。

legend("Right","top",legend=unique(stratumcol), cex=.8, col = unique(stratumcol), pch=15, lty=0)

I found a solution 我找到了解决方案

尝试

legend("right","top",legend = levels(stratumcol),cex=.8, col = levels(stratumcol), pch =15, lty = 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM