I want to plot two functions in different colors and point styles with a corresponding legend, in plain R.
I have several questions:
I
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.