17 ene 2020

Tidyverse and Chemometrics (part 7)

This time practicing to to draw a spectra plot with "ggplot2" (part of the tidyverse package). I found some useful information on stackoverflow, but I have to adapt it to my data and try to understand the process. Finally the result is quite nice, but still a lot of things to learn about this complete package.

Some preparation of the dataframe is need first:

Unclass "nir" from "asis"  to a variable per wavelength:
FMraw <- cbind(fish1_df, as.data.frame(unclass(fish1_df$nir)))
We dont need "nir" so we make it NULL
FMraw$nir <- NULL

Now we pivot the dataframe using the wavelength columns as X and the absorbances values as Y, and for that we use the packages:
library(dplyr)
library(tidyr)   
FMraw_spec <- FMraw %>%

mutate(row = row_number()) %>%
gather(nm, value,-sample_id,-month,

       -year,-dm,-protein,-fat,-ash,-row)

Now we can plot the fishmeal spectra with ggplot2:
library(ggplot2)
ggplot(FMraw_spec, aes(x = nm, y = value)) +
    geom_line(color="red",linetype="dotted")+
              xlab("nm")+
              ylab("log 1/R")+
              ggtitle("Fish meal raw spectra")


And get the plot spectra result:

No hay comentarios:

Publicar un comentario