Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add pch symbol in R plot legend

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

like image 448
Stat Tistician Avatar asked Apr 14 '13 08:04

Stat Tistician


People also ask

What is PCH in plot () in R?

The pch stands for plot character. The pch contains numeric values rangers from 0 to 25 or character symbols (“+”, “.”, “;”, etc.) specifying in symbols (or shapes). The pch is the standard argument to set the character plotted in several R functions. The different points symbols commonly used in R are shown below.

How do you change PCH in R?

Change R base plot point shapes The default R plot pch symbol is 1, which is an empty circle. You can change this to pch = 19 (solid circle) or to pch = 21 (filled circle). To change the color and the size of points, use the following arguments: col : color (hexadecimal color code or color name).

How do I plot different symbols in R?

Using special characters as Pch values In the R Language, we can use “+”, “*“,”-“,”. “,”#, “%”, and “o” for plotting different symbols.

How do I add a title to a legend in R?

In case you need to add a title to the legend, in order to add some description of the elements of the legend, you can use the title argument. Note that you can customize the color of the text with the title. col argument and that you can make a horizontal adjustment of the title with the title. adj argument.


1 Answers

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

like image 80
Roman Luštrik Avatar answered Oct 20 '22 06:10

Roman Luštrik