Legend text color doesn't change

1.1k Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

Solution 1: use fill instead of col

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

Solution 2: use pch

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

You need to add a type of symbol to be drawn in the legend, so that it can be colored:

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