plot functions with filled point symbols and legen

2019-03-09 17:51发布

问题:

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 am using pch=21 and pch=22. 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?

  2. Can I get more space between the points without specifying a grid manually? maybe by selecting the number of points to be printed?

  3. 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?

Here's my code:

par(new=TRUE)
p1 <- 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 <- 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 <- 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)
)

After various explorations, I opted to use the par(new=TRUE) "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 ;-)

Edit: Summary of Solutions

Thanks to joran and Didzis Elferts, I got a solution to several of my problems. For the record, I'd like to summarize here:

  1. To get filled symbols on the graph, you need to specify both col (color) and bg (background). This is true even for pch=21 and pch=22, 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.

  2. it's a very bad idea to use par(new=TRUE) with axes=FALSE, 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 (100^2-x^2)/2 but I inadvertently wrote (1-x^2)/2 and did not realize it because I had set axes=FALSE.

All in all, here is my preferred solution:

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

This yields a plot like the one shown by joran. Thanks a lot to both of you for your help.

回答1:

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.



回答2:

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



标签: r plot legend