图例文字颜色不变

Legend text color doesn't change

我用过这个代码

plot.ecdf(subset(d.pizza, area == "Camden")$delivery_min, 
          col = "red", main = "ECDF for pizza deliveries")
plot.ecdf(subset(d.pizza, area == "Westminster")$delivery_min, 
          add = TRUE, col = "blue")
plot.ecdf(subset(d.pizza, area == "Brent")$delivery_min, 
          add = TRUE, col = "green")
legend(x=50, y=0.4, legend=c("Camden", "Westiminster", "Brent"), col=c("red","blue","green") )

获取此图: enter image description here

但是如您所见,图例文本与我在代码中编写的颜色不匹配。为什么?我该如何解决?

这段代码也一样

plot(density(subset(d.pizza, area == "Camden")$delivery_min), col="red", ylim=c(0,0.06)) 
  lines(density(subset([d.pizza, area == "Westminster")$delivery_min), col="blue") 
  lines(density(subset(d.pizza, area == "Brent")$delivery_min), col="green")
legend(x=50, y=0.05, legend=c("Camden", "Westiminster", "Brent"), col=c("red","blue","green") )

enter image description here

一定是在犯同样的错误.. 提前致谢!

d.pizza id 来自“DescTools”包的数据框

您需要在图例中添加要绘制的符号类型,以便对其进行着色:

legend(x=50, y=0.4, legend=c("Camden", "Westiminster", "Brent"), col=c("red","blue","green"), pch=1) #pch sets the type of point to be drawn

解决方案 1:使用 fill 而不是 col

legend(x=50, y=0.4, legend=c("Camden", "Westiminster", "Brent"), 
   fill=c("red","blue","green") )

方案二:使用pch

legend(x=50, y=0.4, legend=c("Camden", "Westiminster", "Brent"), 
   col=c("red","blue","green"), pch=16 )