我是R的新手 我正在尝试创建一个密度图,由于之前的问题而没有问题。这里的答案。
我目前的问题是图表的图例。我已经分配了想要的颜色(col = c("red", "orange", "yellow", "green", "blue", "purple")
)但它们没有在图例本身上显示,而是我得到随机颜色。
我认为重要的是要提到运行代码和运行后没有错误。我已经检查过所有这些颜色是否可用here。
这是密度绘图代码,以及图例信息。
plot(density(df$a1), col = "red", xlim = c(0, 1000), ylim = c(0, 0.004))
lines(density(df$a2), col = "orange")
lines(density(df$a3), col = "yellow")
lines(density(df$a4), col = "green")
lines(density(df$a5), col = "blue")
lines(density(df$a6), col = "purple")
legend(x = "topright", legend = names(df), fill = 1:6, col = c("red", "orange", "yellow", "green", "blue", "purple"))
然而,结果是:
谢谢!
答案 0 :(得分:2)
fill
指定框的颜色,而col
指定点和线的颜色。
在您的情况下,代码应为:
legend(x = "topright", legend = names(df), lty=1, col = c("red", "orange", "yellow", "green", "blue", "purple"))
lty=1
表示您希望用实线代替框(lty
代表 l ine ty pe)。