Add pch symbol in R plot legend

Stat Tistician picture Stat Tistician · Apr 14, 2013 · Viewed 49.5k times · Source

I have one time series which is represented by a black line and one which is represented by a red curve. Then I have single points which have the pch symbol of 8 in R. These are stars. See the following plot: stars

Currently I have the following legend:

legend("bottomleft", 
 legend=c("log loss","daily VaR","exceedance"),
 bty = "n",lwd=2, cex=1.2,y.intersp=1.4, col=c("black","red","blue"), lty=c(1,1,1))

But I don't want to have a blue line in the legend for exceedance, but just the stars in the plot. I have to use the pch=8. I just want to have the stars in the legend, not the stars with a line. So not these solutions: R legend issue, symbols of points are masked by lines

Answer

Roman Luštrik picture Roman Luštrik · Apr 14, 2013

Try this. You set lty to display only first two lines, and pch to display only the last point.

plot(1:10, rnorm(10) * 1:10)
legend("bottomleft", legend = c("entry1", "entry2", "something cpl different"), bty = "n",
       lwd = 2, cex = 1.2, col = c("black", "blue", "red"), lty = c(1, 1, NA), pch = c(NA, NA, 8))

enter image description here