rbaseline

Correcting the X axis values of a baseline corrected spectrum generated through the baseline package in R


first of all, I would like to note I am new to R and programming in general, so apologies in advance for what is probably a very basic question. Here is what I tried and what happened.

I have acquired an FTIR spectrum that I converted into a csv file, and then pre-processed the data to the format required for the baseline package (to my understanding), with the header row as the wavelength and the intensity as the first row. processed data I then converted the data to a matrix and generated the baseline by using the baseline function in the baseline package:

spectrum_matrix <- as.matrix(IMCD)

spectrum_matrix

bl.IMCD <- baseline(spectrum_matrix, method = "modpolyfit")

plot(bl.IMCD)

Generated spectra using baseline function within baseline package The plot generated by this has X axis values that do not match those in the original spectrum Original spectrum I could manually correct the axis but as I intend to process a lot of FTIR data, a way to fix this would be much appreciated!


Solution

  • I don't know the package but I might be able to help a bit.

    I think the column names are not used at all in the baseline() function. Does the code below help to set the required wavelengths?

    wavelengths <- round(as.numeric(colnames(bl.IMCD)))
    plot(bl.IMCD, labels = as.character(wavelengths))
    

    Explanation: In R it is usually the question where to look to find the right information. In this case, the authors of the package are using S4 methods in their functions. Details can be found here, but most important to know is: when you call the function baseline and the function plot, you are actually calling different "hidden" functions based on the specific class of the object.

    In your case, if you call the function baseline with the argument "modpolyfit" you are actually calling the function baseline.modpolyfit. If you type ?baseline::baseline.modpolyfit in the console, you can find all the options for your specific function call. In addition, if you just enter baseline::baseline.modpolyfit in the console, the actual function code will be printed. Here you can see in the first linw that the column names in the matrix are deleted: dimnames(spectra) <- NULL.

    Furthermore, if you are calling the function plot for an object of class baseline, you are actually calling the function plotBaseline.

    Similarly, if you type ?baseline::plotBaseline in the console, you will find the appropriate help page of the plot function. Here the argument labels is described, which is just what you need. It also shows the default values that you were seeing (the column number, from 1:n).