简体   繁体   English

图例文字颜色不会改变

[英]Legend text color doesn't change

I've used this code我用过这个代码

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") )

to get this plot: enter image description here获取此图:在此处输入图像描述

but as you see legend text doesn't match colors I wrote in the code.但是正如您所看到的,图例文本与我在代码中编写的颜色不匹配。 Why?为什么? How can I fix it?我该如何解决?

Same thing about this code关于这段代码的同样的事情

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在此处输入图片说明

Must be doing the same mistake.. Thanks in advance!一定是犯了同样的错误.. 提前致谢!

d.pizza id a data frame from "DescTools" package 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

Solution 1: use fill instead of col解决方案 1:使用 fill 而不是 col

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

Solution 2: use pch解决方案2:使用pch

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

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

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