[R] Mixing lty specifications in legend

Duncan Murdoch murdoch at stats.uwo.ca
Thu Nov 8 01:15:39 CET 2007


On 07/11/2007 7:01 PM, Peter Dunn wrote:
> Hi all
> 
> I have a plot with lines, one specified as (say) lty=1, 
> using standard line types, and another as (say) my
> own spec: lty="51".
> 
> I can't get  legend  to display both.  Toy example:
> 
> 
>> plot(1~1)
>> legend("topright", lty=c("51",1), legend=c("My own","Standard"))
> Error in segments(x1, y1, x2, y2, ...) : invalid line type: must be
> length 2, 4, 6 or 8
> 
> 
> Any ideas?

Fairly kludgy idea:  plot it twice, once for each style.  For example,

plot(1 ~ 1)
legend("topright", lty=c("55",NA_character_), legend=c("My own","Standard"))
legend("topright", lty=c(0,1), legend=c("My own","Standard"))

The man page says the NA is not allowed, but it does appear to work. 
Perhaps there's another way to get blanks using the manual spec.

Duncan Murdoch



More information about the R-help mailing list