title: plot functions with filled point symbols and legend
body: <p>I want to plot two functions in different colors and point styles with a corresponding legend, <strong>in plain R</strong>.</p>
  
  <p>I have several questions:</p>
  
  <ol>
  <li><p>I am using <code>pch=21</code> and <code>pch=22</code>. My understanding is that they are "filled" symbols. They do appear filled as expected in the legend, but they appear hollow on the graph itself. What's wrong?</p></li>
  <li><p>Can I get more space between the points without specifying a grid manually? maybe by selecting the number of points to be printed?</p></li>
  <li><p>Feel free to add any kind of advice you'd like. I'm very new to R. In particular, is there a better way to plot two functions? e.g. by defining vectors of functions? and wouldn't there be a way for the legend to be automagically generated without having to specify colors and shapes, in plain R? </p></li>
  </ol>
  
  <p>Here's my code:</p>
  
  <pre><code>par(new=TRUE)
  p1 &lt;- plot(function(x){ x^(2)/2 }
         , 0, 100
         , xlab = "x"
         , ylab = "y"
         , ylim = c(0,5000)
         , las = 1
         , type = "p"
         , cex = 0.8
         , pch = 21
         , col = "red"
  )
  par(new=TRUE)
  p2 &lt;- plot(function(x){ (1-x^(2))/2 }
         , 0, 100
         , xlab = ""
         , ylab = ""
         , axes = FALSE
         , type = "p"
         , cex = 0.8
         , pch = 22
         , col = "blue"
  )
  par(new=TRUE)
  l &lt;- legend( "topleft"
           , inset = c(0,0.4) 
           , cex = 1.5
           , bty = "n"
           , legend = c("A", "B")
           , text.col = c("red", "blue")
           , pt.bg = c("red","blue")
           , pch = c(21,22)
  )
  </code></pre>
  
  <p>After various explorations, I opted to use the <code>par(new=TRUE)</code> "trick" to superimpose the two functions (rather than, say, using matplot or a combination of plot and points or layout). Was that a bad move to begin with? (Edit: yes, very bad, see below) +1 if you don't ask me to read the manual ;-)</p>
  
  <p><img src="https://i.stack.imgur.com/ZqVxt.png" alt="enter image description here"></p>
  
  <p><strong>Edit: Summary of Solutions</strong></p>
  
  <p>Thanks to joran and Didzis Elferts, I got a solution to several of my problems. For the record, I'd like to summarize here:</p>
  
  <ol>
  <li><p>To get filled symbols on the graph, you need to specify both col (color) and bg (background). This is true even for <code>pch=21</code> and <code>pch=22</code>, which do not automatically get filled by the color specified. To get filled symbols in the legend, you need to specify both col and pt.bg. Here, bg alone is not good enough.</p></li>
  <li><p>it's a very bad idea to use <code>par(new=TRUE)</code> with <code>axes=FALSE</code>, as I had done initially, because, the overlaid plots do not necessarily use the same coordinate system. The intended function for the second plot was <code>(100^2-x^2)/2</code> but I inadvertently wrote <code>(1-x^2)/2</code> and did not realize it because I had set axes=FALSE. </p></li>
  </ol>
  
  <p>All in all, here is my preferred solution:</p>
  
  <pre><code>curve( x^2/2
    , from = 0
    , to = 100
    , n = 30
    , type = "p"
    , pch = 21 # alternatively pch=15 is a solid symbol
    , col = "red" # colors the outline of hollow symbol pch=21
    , bg = "red" # fills hollow symbol pch=21 with color
    , xlab = "x"
    , ylab = "y"
  )
  curve( (100^2-x^2)/2
    , from = 0
    , to = 100
    , n = 30
    , type = "p"
    , pch = 22  # alternative pch=16
    , col = "blue"
    , bg = "blue"
    , add = TRUE
  )
  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"), 
    , pt.bg = c("red","blue")
    , pch = c(21,22)
  )
  </code></pre>
  
  <p>This yields a plot like the one shown by joran. Thanks a lot to both of you for your help.</p>