Add pch symbol in R plot legend

2020-05-31 11:49发布

问题:

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:

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

回答1:

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