[R] How do I get a character and a symbol in a legend

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Wed Jan 23 19:10:55 CET 2008


steve wrote:
> In the following snippet
>
> plot(1:10,1:10,type="n")
> points(1:5,1:5,pch="+")
> points(6:10,6:10,pch=20)
> legend(5,5, c("A","B"), pch=c("+",20))
>
> I want to get a legend with a "+" and a solid circle (pch=20).
> However, what I get in the legend is "+" and "2". How can I get a "+" 
> and a solid circle?
>
>   
Hmm... There seems to be no way around having all pch's of the same
type, because it has to be a single vector. So what you need to do is to
find the integer code for "+". This happens to be the same as its ASCII
code which is 43. One slightly sneaky way of finding that out is

> as.integer(charToRaw("+"))
[1] 43

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list