# Re: [R] Vector lty argrument for lines or plot

From: Brian Diggs <diggsb_at_ohsu.edu>
Date: Fri, 21 Nov 2008 10:46:56 -0800

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.

```--
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
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

>
> _____________________________

>
> Baptiste Auguié
>
> School of Physics
> University of Exeter
> 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
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help