问题描述
我已经使用了这段代码
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,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,"green") )
必须犯同样的错误。 预先感谢!
d.pizza标识了“ 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,fill=c("red","green") )
解决方案2:使用pch
legend(x=50,pch=16 )