Use the "lines" command with a vector of objects in R

I am using R to analyze IR spectra. What I am trying to do is create a list or vector of objects, in this case Spectras, and put them in the same "window". I used the command plot(Spectrum1,...)

for the first Spectrum and the command

lines(Spectrum2,...) 
lines(Spectrum3,...) 
...

      

for the following spectra. It worked well, but I was wondering if I could compose a list or vector of Spectras, like

Spectras <- c(Spectrum2, Spectrum3,...)

      

or

Spectras <- list(Spectrum2, Spectrum3,...)

      

and build them in one command line like:

lines(Spectras,..)

      

In the case of a list, R says

'x' is a list but does not contain the 'x' and 'y' components

If I do it with a command c()

, it just displays the Spectras one by one.

Some ideas how to get this to work?

+3


source to share


1 answer


Use functions list

followed by lapply

or plyr::l_ply

. If the list of spectra is named "Spectra" with each component, matrix or data frame with x and y columns:

lapply(Spectra, lines)

      

or



plyr::l_ply(Spectra, lines)

      

Alternatively use ggplot2:

library("ggplot2")
library("plyr")
SpecDf <- as.data.frame(do.call("rbind", Spectra))
SpecDf$SpectrumNumber <- rep(1:length(Spectra), plyr::laply(Spectra, nrow))
ggplot(SpecDf, aes(x = x, y = y, colour = SpectrumNumber)) + geom_line()

      

+2


source







All Articles