[R] plot several lines programmaticaly

Gerrit Eichner Gerrit.Eichner at math.uni-giessen.de
Tue Dec 15 12:30:41 CET 2015


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



More information about the R-help mailing list