[R] Vector lty argrument for lines or plot

Brian Diggs diggsb at ohsu.edu
Fri Nov 21 19:46:56 CET 2008


Thank you for the suggestion, baptiste.  segments() does do exactly what I was wanting and matplot()/matlines() is probably a better solution to what I was trying to do.

However, I am still concerned about the discrepancy between the documentation in ?par and the behavior of lines().  Should lines() be changed to cycle over a vector of lty (so that it agrees with the documentation in ?par)?  Should the documentation of par be changed to use a different example of a function that cycles over a vector of lty (segments() being a good candidate)?  Or are both lines() and ?par correct and there is a situation which lines() does cycle over a vector of lty that I (and at least baptiste as well) do not understand?  The middle option is certainly the easiest, and I think the correct one, but I wanted to rule out the last one before filing a bug report.

-- 
Brian Diggs, Ph.D.
Senior Research Associate, Department of Surgery, 
Oregon Health & Science University

baptiste auguie wrote:
> Hi,
> 
> 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,
> 
> baptiste
> 
> 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,
> EX4 4QL, UK
> 
> Phone: +44 1392 264187
> 
> http://newton.ex.ac.uk/research/emag
> 
> ______________________________________________
> 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.
> 



More information about the R-help mailing list