[英]Legend appears, but it does not show color
I am using R for plotting.我正在使用 R 进行绘图。 When my graph plots the legend appears where I want it to be but the colors are missing.
当我的图表绘制图例时,图例出现在我想要的位置,但颜色丢失。
mtcars 2
is a modified version of mtcars
(one of the pre-loaded data sets) that adds a model and country of origin to the data set. mtcars 2
是mtcars
(预加载的数据集之一)的修改版本, mtcars
向数据集添加了模型和原产国。 mtcars.pca
is what I named my redundance analysis (rda function under vegan), and mtcars.clust
is titled for hierarchical clustering of the continuous factors of mtcars (hclust function of vegan) Below is the code I am using with mtcars2
. 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")
You need to specify a vector of colours in legend
and also a pch
:您需要在
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)
If you don't add pch
but just use col = cols
legend()
doesn't display any points.如果您不添加
pch
而只是使用col = cols
legend()
则不会显示任何点。 Because you used pch = 19
in your points()
calls, use the same in the legend()
call.因为您在
points()
调用中使用了pch = 19
,所以在legend()
调用中使用相同的值。
Also, note how to plot points of different colours in a single pass.另外,请注意如何在一次通过中绘制不同颜色的点。 I have some examples and explanation that go through the indexing trick I used in my code above to achieve this in a blog post of mine from a few years ago: http://www.fromthebottomoftheheap.net/2012/04/11/customising-vegans-ordination-plots/
我有一些示例和解释,这些示例和解释通过我在上面的代码中使用的索引技巧来实现,这是我几年前的一篇博客文章: http : //www.fromthebottomoftheheap.net/2012/04/11/customising -素食者排序图/
I came to this question having the next problem in xts object: I wanted to plot all time-series in xts object with legend.我遇到了 xts 对象中的下一个问题:我想用图例绘制 xts 对象中的所有时间序列。 Moreover, there were around 20.
此外,大约有20个。
plot(returns_xts)
addLegend(...)
plot(returns_xts, legend.loc = "bottomright", col=1:20, lty = 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.