I want to plot two functions in different colors and point styles with a corresponding legend, in plain R.
I have several questions:
I am using
pch=21
andpch=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?Can I get more space between the points without specifying a grid manually? maybe by selecting the number of points to be printed?
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:
To get filled symbols on the graph, you need to specify both col (color) and bg (background). This is true even for
pch=21
andpch=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.it's a very bad idea to use
par(new=TRUE)
withaxes=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.