[R] Vector lty argrument for lines or plot

baptiste auguie ba208 at exeter.ac.uk
Thu Nov 20 21:47:01 CET 2008


If you wish to connect each point to the next with a different  
linetype, I think your best bet is to use segments()

x <- stats::runif(12); y <- stats::rnorm(12)
i <- order(x,y); x <- x[i]; y <- y[i]
plot(x, y)
s <- seq(length(x)-1)
segments(x[s], y[s], x[s+1], y[s+1], lty=1:10)

If, however, you wish to plot several groups of lines with different  
linetypes, then matlines() should do the job. Both of these make  
actual use of lty as a vector, while polygon(), abline(), plot(),  
lines() will only use the first value (as far as i can see).

Hope this helps,


On 20 Nov 2008, at 20:24, Brian Diggs wrote:

> I am confused by the behavior of the lines function when the lty  
> argument is a vector.  ?lines indicates that lty is a valid  
> parameter, but says nothing else about it.  ?plot.xy (which I think  
> is what gets called) refers back to ?lines.  ?plot.default says to  
> see ?par.  In ?par, about lty it says "Some functions such as lines  
> accept a vector of values which are recycled. Other uses will take  
> just the first value if a vector of length greater than one is  
> supplied."  However, I cannot get lines to use more than one type of  
> line.  Some example code:
> pt <- runif(10)
> plot(pt)
> lines(pt, type="c", lty=1:10)
> I expected each subsequent line segment to be in a different style.   
> Only the first seems to be used.  The same is true for plot:
> plot(pt, type="b", lty=1:10)
> uses only one style of line segment (although no documentation says  
> explicitly that the others would be used).  It doesn't matter the  
> order or manner of specification, only the first is used.
> plot(pt)
> lines(pt, type="c", lty=c("dashed","solid"))
> plot(pt)
> lines(pt, type="c", lty=c("FF", "11"))
> I have used a vector of lty before (in legend) and it cycled through  
> all the values.  Am I misunderstanding what a vector lty to lines  
> means, or is this a bug?
> I'm running on Windows XP Pro, if that might matter.
>> sessionInfo()
> R version 2.8.0 (2008-10-20)
> i386-pc-mingw32
> locale:
> LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States. 
> 1252;LC_MONETARY=English_United States. 
> 1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> --
> Brian Diggs, Ph.D.
> Senior Research Associate, Department of Surgery, Oregon Health &  
> Science University
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,

Phone: +44 1392 264187


More information about the R-help mailing list