微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

图例文字颜色不变

如何解决图例文字颜色不变

我已经使用了这段代码

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

enter image description here

必须犯同样的错误。 预先感谢!

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 )

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。