plot functions with filled point symbols and legend

房东的猫 提交于 2019-12-02 18:57:50

I think maybe you'd have better luck using curve:

curve(x^(2) / 2,from = 0,to = 100,col = 'red',type = 'p',pch = 16,n = 20)
curve((1-x^(2))/2 + 5000,from = 0,to = 100,col = 'blue',type = 'p',pch = 15,add = TRUE,n = 20)
legend("topleft", 
        inset = c(0,0.4), 
        cex = 1.5, 
        bty = "n", 
        legend = c("A", "B"), 
        text.col = c("red", "blue"),
        col = c("red", "blue"), 
        pch = c(16,15))

Note that I had to tweak your functions slightly, to get output that matched your image.

To avoid specifying color and fill separately (which in general is how things are done in R) I used some older "legacy" symbols. Using curve is often much simpler for plotting functions or expressions. It also gives you a more convenient way to specify the grid of points to evaluate on. It also has an add argument that allows you to skip the awkward par hacking you engaged in.

You should add argument bg="red" or bg="blue" inside plot() to get the filled symbols with particular color.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!