plot functions with filled point symbols and legend

后端 未结 2 482
花落未央
花落未央 2021-02-01 16:25

I want to plot two functions in different colors and point styles with a corresponding legend, in plain R.

I have several questions:

  1. I

相关标签:
2条回答
  • 2021-02-01 16:53

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

    enter image description here

    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.

    0 讨论(0)
  • 2021-02-01 17:03

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

    0 讨论(0)
提交回复
热议问题