R Exercises - Spectroscopic data - Solution


# Place the Rmd file you are working on in the same folder as the one containing the "Data" folder
# If you do so, the path is relative to where your working Rmd file is located :
df <- read_excel("Data/FTIR_rocks.xlsx")
# Otherwise, you need to provide the whole absolute path
# df <- read_excel("/Users/colin/Downloads/Data/FTIR_rocks.xlsx")
names(df) <- c("w","r1","r2","r3")
max1 <- df$w[which.max(df$r1)]
max2 <- df$w[which.max(df$r2)]
max3 <- df$w[which.max(df$r3)]
max1;max2;max3
[1] 2924.912
[1] 1594.092
[1] 2925.876
norm01 <- function(x){
    (x-min(x))/(max(x)-min(x))
}
# check it's working
x <- 1:10
norm01(x)
 [1] 0.0000000 0.1111111 0.2222222 0.3333333 0.4444444 0.5555556 0.6666667
 [8] 0.7777778 0.8888889 1.0000000
# First, let's make the tibble tidy
df_tidy <- df |> pivot_longer(cols=-w,
                    names_to="rock",
                    values_to="intensity")
df_tidy
# A tibble: 14,154 × 3
       w rock  intensity
   <dbl> <chr>     <dbl>
 1 5199. r1      0.00566
 2 5199. r2      0.00225
 3 5199. r3      0.0346 
 4 5198. r1      0.00568
 5 5198. r2      0.00191
 6 5198. r3      0.0346 
 7 5197. r1      0.00570
 8 5197. r2      0.00186
 9 5197. r3      0.0346 
10 5196. r1      0.00581
# ℹ 14,144 more rows
# now add the new column 'intensity_n' containing the normalized intensities
df_tidy <- df_tidy |> 
    group_by(rock) |>
    mutate(intensity_n = norm01(intensity))
df_tidy
# A tibble: 14,154 × 4
# Groups:   rock [3]
       w rock  intensity intensity_n
   <dbl> <chr>     <dbl>       <dbl>
 1 5199. r1      0.00566     0.00687
 2 5199. r2      0.00225     0.00517
 3 5199. r3      0.0346      0.0385 
 4 5198. r1      0.00568     0.00689
 5 5198. r2      0.00191     0.00437
 6 5198. r3      0.0346      0.0385 
 7 5197. r1      0.00570     0.00691
 8 5197. r2      0.00186     0.00427
 9 5197. r3      0.0346      0.0385 
10 5196. r1      0.00581     0.00705
# ℹ 14,144 more rows

P1 <- df_tidy |>
    ggplot(aes(x=w, y=intensity_n, color=rock)) +
        # add the lines with thickness of 1
        geom_line(size=1) +
        # change the axis labels
        labs(x="Wavenumber [1/cm]", y="Intensity [arb. units]") +
        theme_bw() +
        # set the color to the wanted colors and give a name to 
        # the legend if you want to plot the legend
        # if this line is absent, ggplot uses its default colors (blue, red, green)
        scale_color_manual(values = c("black", "royalblue", "red"), name="Rock:") +
        # to remove the legend, set it to "none"
        # if you remove this line, the default is to show the legend on the right
        theme(legend.position = "top")
P2 <- df_tidy |>
    ggplot(aes(x=w, 
               y=intensity_n + as.numeric(factor(rock)) - 1, 
               color=rock)) +
        geom_line(size=1)+
        labs(x="Wavenumber [1/cm]", y="Intensity [arb. units]")+
        theme_bw()+
        theme(legend.position = "none")+
        scale_color_manual(values = c("black", "royalblue", "red"))
library(patchwork)
P1+P2