[英]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") )
獲取此圖:在此處輸入圖像描述
但是正如您所看到的,圖例文本與我在代碼中編寫的顏色不匹配。 為什么? 我該如何解決?
關於這段代碼的同樣的事情
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") )
一定是犯了同樣的錯誤.. 提前致謝!
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") )
解決方案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.