Search code examples
rplotlegendtextcolordensity-plot

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


Solution

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