# [R] plot several lines programmaticaly

PIKAL Petr petr.pikal at precheza.cz
Tue Dec 15 13:23:06 CET 2015

```Hi Gerrit,

Thanks, I tried it and it seems to be viable option. I knew I overlooked something pretty simple.

Cheers
Petr

> -----Original Message-----
> From: Gerrit Eichner [mailto:Gerrit.Eichner at math.uni-giessen.de]
> Sent: Tuesday, December 15, 2015 12:31 PM
> To: PIKAL Petr
> Cc: r-help at r-project.org
> Subject: Re: [R] plot several lines programmaticaly
>
> Hi, Petr,
>
> from your example it's not doubtlessly clear to me sure how the
> sequence of ifs should really be continued, but couldn't a nested loop
> help?
> Something like:
>
> for (i in 1:npks) {
>   plot(res[[i]]\$x, res[[i]]\$y, pch=20)
>
>   for (j in 1:i) {
>    lines(res[[i]]\$x, res[[i]]\$fitpk[j,], col=j, lwd=3)}
>    }
>   }
>
> (However, this seems too simple ...)
>
>   Hth  --  Gerrit
>
>
> On Tue, 15 Dec 2015, PIKAL Petr wrote:
>
> > Dear all
> >
> > I am stuck with output of some result. Sorry for not providing
> working
> > example but res is quite a big list and I believe you are able to
> > understand my problem.
> >
> > I have npks variable, which can be anything from 1 to let say 5.
> >
> > I get result called here res as a nested list with several components
> > and I want to plot these components based on npks value. I got this
> > far
> >
> > par(mfrow=c(npks,1)) # slit the device for (i in 1:npks) {
> > plot(res[[i]]\$x, res[[i]]\$y, pch=20)
> >
> > if (i ==1) { #lines in case npks==1
> > lines(res[[i]]\$x, res[[i]]\$fitpk[i,], col=i, lwd=3)}
> >
> > if(i==2) { # lines in case npks ==2
> > lines(res[[i]]\$x, res[[i]]\$fitpk[1,], col=1, lwd=3) lines(res[[i]]\$x,
> > res[[i]]\$fitpk[i,], col=i, lwd=3) }
> >
> > I can follow this further and expand lines plotting with ifs but it
> > seems to me that there must be better solution I overlooked.
> >
> > Here is a structure of res
> >
> >> str(res)
> > List of 2
> > \$ :List of 15
> >  ..\$ intnr  : int 1
> >  ..\$ x      : Named num [1:153] 34.8 34.8 34.9 34.9 34.9 ...
> >  .. ..- attr(*, "names")= chr [1:153] "2484" "2485" "2486" "2487" ...
> >  ..\$ y      : num [1:153] 3.08 -2.91 5.1 12.1 42.11 ...
> >  ..\$ fit    : num [1:153] 15.2 15.3 15.4 15.5 15.7 ...
> >  ..\$ fitpk  : num [1, 1:153] 2.35 2.49 2.65 2.81 2.99 ...
> >  ..\$ basl   : num [1:153] 258 258 258 258 258 ...
> >  ..\$ baslchg: num [1:153] 12.8 12.8 12.7 12.7 12.7 ...
> >  ..\$ rss    : num 1354
> >  ..\$ num.ker: int 1
> >  ..\$ par    : num [1:6] 12.8649 -0.0409 4549.1417 1.9927 66.939 ...
> >  ..\$ parbl  : Named num [1:2] 12.86 -4.09  .. ..- attr(*, "names")=
> > chr [1:2] "" "2485"
> >  ..\$ parpks : num [1, 1:5] 35.494 4549.142 715.324 0.129 1.993  ..
> ..-
> > attr(*, "dimnames")=List of 2  .. .. ..\$ : NULL  .. .. ..\$ : chr
> [1:5]
> > "loc" "height" "intens" "FWHM" ...
> >  ..\$ accept : logi FALSE
> >  ..\$ alpha  : num 0.1
> >  ..\$ thresh : Named num 3.81
> >  .. ..- attr(*, "names")= chr "90%"
> > \$ :List of 15
> >  ..\$ intnr  : int 1
> >  ..\$ x      : Named num [1:153] 34.8 34.8 34.9 34.9 34.9 ...
> >  .. ..- attr(*, "names")= chr [1:153] "2484" "2485" "2486" "2487" ...
> >  ..\$ y      : num [1:153] 3.08 -2.91 5.1 12.1 42.11 ...
> >  ..\$ fit    : num [1:153] 15 15 15.1 15.1 15.2 ...
> >  ..\$ fitpk  : num [1:2, 1:153] 2.1329 0.0859 2.2666 0.0876 2.4109 ...
> >  ..\$ basl   : num [1:153] 258 258 258 258 258 ...
> >  ..\$ baslchg: num [1:153] 12.8 12.7 12.6 12.5 12.4 ...
> >  ..\$ rss    : num 1325
> >  ..\$ num.ker: num 2
> >  ..\$ par    : num [1:10] 12.8649 -0.0942 4540.4742 2.0373 66.9394 ...
> >  ..\$ parbl  : Named num [1:2] 12.86 -9.42  .. ..- attr(*, "names")=
> > chr [1:2] "" "2485"
> >  ..\$ parpks : num [1:2, 1:5] 35.5 35.9 4540.5 39.4 715.3 ...
> >  .. ..- attr(*, "dimnames")=List of 2
> >  .. .. ..\$ : NULL
> >  .. .. ..\$ : chr [1:5] "loc" "height" "intens" "FWHM" ...
> >  ..\$ accept : logi FALSE
> >  ..\$ alpha  : num 0.1
> >  ..\$ thresh : Named num 3.81
> >  .. ..- attr(*, "names")= chr "90%"
> >>
> >
> > Best Regards
> > Petr

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
```