generated from r-hyperspec/pkg-skeleton
/
hySpc-chondro.Rmd
789 lines (564 loc) · 25.8 KB
/
hySpc-chondro.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
---
# For bookdown ---------------------------------------------------------------
bookdown_file_name: chondro
bookdown_titles: |
# (PART\*) II Case studies {-}
# `chondro`: Raman Spectra of Chondrocytes in Cartilage {#chondro}
# For vignette ---------------------------------------------------------------
title: 'Raman Spectra of Chondrocytes in Cartilage'
subtitle: 'Example Workflow for 2D Raman Spectra Dataset `chondro`'
description: 'chondro: Example workflow for 2D Raman spectra dataset `chondro`.'
# Authors --------------------------------------------------------------------
author:
- name: Claudia Beleites^1,2,3,4,5^, Vilmantas Gegzna
email: chemometrie@beleites.de
corresponding : yes
affiliation : |
1. DIA Raman Spectroscopy Group, University of Trieste/Italy (2005--2008)
2. Spectroscopy $\cdot$ Imaging, IPHT, Jena/Germany (2008--2017)
3. ÖPV, JKI, Berlin/Germany (2017--2019)
4. Arbeitskreis Lebensmittelmikrobiologie und Biotechnologie, Hamburg University, Hamburg/Germany (2019 -- 2020)
5. Chemometric Consulting and Chemometrix GmbH, Wölfersheim/Germany (since 2016)
# Document -------------------------------------------------------------------
date: "`r Sys.Date()`"
output:
bookdown::html_document2:
base_format: rmarkdown::html_vignette
toc: yes
fig_caption: yes
number_sections: true
css:
- vignette.css
- style.css
vignette: >
% \VignetteIndexEntry{Raman Spectra of Chondrocytes in Cartilage}
% \VignettePackage{hySpc.chondro}
% \VignetteKeyword{chondro}
% \VignetteKeyword{cluster analysis}
% \VignetteKeyword{preprocessing}
% \VignetteKeyword{Raman}
% \VignetteKeyword{principal component analysis}
% \VignetteKeyword{PCA}
% \VignetteKeyword{hyperSpec}
% \VignetteEngine{knitr::rmarkdown}
% \VignetteEncoding{UTF-8}
# Citations/References -------------------------------------------------------
link-citations: yes
bibliography: resources/chondro-pkg.bib
biblio-style: plain
csl: elsevier-with-titles.csl
# Pkgdown --------------------------------------------------------------------
pkgdown:
as_is: true
set_null_theme: no
---
```{r cleanup-chondro, include = FALSE}
# Clean up to ensure reproducible workspace ----------------------------------
rm(list = ls(all.names = TRUE))
```
```{r setup, include = FALSE}
# Packages -------------------------------------------------------------------
library(hyperSpec)
library(latticeExtra)
library(akima)
library(ggplotify) # To convert plots to necessary class for alignment
library(ggpubr) # To align plots
# Functions ------------------------------------------------------------------
source("vignette-functions.R", encoding = "UTF-8")
# Settings -------------------------------------------------------------------
source("vignette-default-settings.R", encoding = "UTF-8")
# Temporaty options ----------------------------------------------------------
# Change the value of this option in "vignette-default-settings.R"
show_reviewers_notes = getOption("show_reviewers_notes", TRUE)
```
```{r bib, echo=FALSE, paged.print=FALSE}
dir.create("resources", showWarnings = FALSE)
knitr::write_bib(
c(
"hyperSpec"
),
file = "resources/chondro-pkg.bib",
prefix = "R_"
)
```
<!-- ======================================================================= -->
```{block, type="note-t", echo=show_reviewers_notes}
**V. Gegzna's notes** `chondro-1`
1. `FIXME:`{.r} After the translation is completed, the contents in the box below must be fixed.
```
<!-- ======================================================================= -->
```{block, type="note", echo=show_reviewers_notes}
`# FIXME. Fix the contents of this box:`{.r}
`# FIXME. This box should be removed`{.r}
**Reproducing this vignette.**
The data set and source file of this vignette are available at package
**hyperSpec**'s github page at <https://github.com/cbeleites/hyperSpec/blob/master/Vignettes/chondro/chondro.Rnw> and
<https://github.com/cbeleites/hyperSpec/tree/master/Vignettes/fileio/txt.Renishaw/chondro.txt>
(ca. 31 MB).
<!-- FIXME: -->
The .Rnw looks for `rawdata/chondro.txt`, so please save the data file in subdirectory `rawdata` of your working directory.
In order to reproduce the examples by typing in the commands, have a look at the definitions of color palettes used in this document are defined in `vignettes.defs`.
Also, the package **hyperSpec** needs to be loaded first via `library(hyperSpec)`{.r}.
```
# Introduction
This vignette describes the `chondro`{.r} data set.
It shows a complete data analysis work flow on a Raman map demonstrating frequently needed preprocessing methods:
- baseline correction,
- normalization,
- smoothing / interpolating spectra,
- preprocessing the spatial grid,
as well as other basic work techniques:
- plotting spectra,
- plotting false color maps,
- cutting the spectral range,
- selecting (extracting) or deleting spectra, and
- *aggregating* spectra (e.g., calculating cluster mean spectra).
The chemometric methods used are:
- Principal Component Analysis (PCA) and
- Hierarchical Cluster Analysis,
showing how to use data analysis procedures provided by R and other
packages.
# The Data Set
Raman spectra of a cartilage section were measured on each point of a grid, resulting in a so-called
*Raman map*.
Figure \@ref(fig:vis-all) shows a microscope picture of the measured area and its
surroundings.
```{r vis-all, echo=FALSE, fig.cap=CAPTION, out.width="400"}
CAPTION = "
Microphotograph of the cartilage section.
The frame indicates the measurement area of $35×25 \\mu m.$
"
knitr::include_graphics("chondro--080606d-flip-ausw.jpg")
```
The measurement parameters were:
- **Excitation wavelength:** 633 nm
- **Exposure time:** 10 s per spectrum
- **Objective:** 100×, NA 0.85
- **Measurement grid:** 35 × 25 $\mu m$, 1 $\mu m$ step size
- **Spectrometer:** Renishaw InVia
# Data Import
Renishaw provides a converter to export their proprietary data in a so-called long format ASCII file.
Raman maps are exported having four columns, *y*, *x*, *raman shift*, and *intensity*.
Package **hySpc.read.txt** comes with a function to import such files, `hySpc.read.txt::read_txt_Renishaw()`{.r}.
The function assumes a map as default, but can also handle single spectra (`data = "spc"`{.r}), time series (`data = "ts"`{.r}), and depth profiles (`data = "depth"`{.r}).
In addition, large files may be processed in chunks.
In order to speed up the reading `read_txt_Renishaw()`{.r} does not allow missing values, but it does work with `NA`{.r}.
```{r chondro-import, cache=TRUE}
# Download file from:
raw_data_url <-
'https://media.githubusercontent.com/media/cbeleites/hyperSpec/master/Vignettes/fileio/txt.Renishaw/chondro.gz'
dir.create('rawdata', showWarnings = FALSE)
download.file(raw_data_url, 'rawdata/chondro.gz')
# Read the raw data and make a hyperSpec object
chondro <- hySpc.read.txt::read_txt_Renishaw("rawdata/chondro.gz", data = "xyspc")
chondro
```
To get an overview of the spectra (figure \@ref(fig:rawspc)):
```{r include=FALSE}
CAPTION <- "The raw spectra: median, 16^th^ and 84^th^, and 5^th^ and 95^th^ percentile
spectra."
```
```{r rawspc, fig.cap=CAPTION}
plot(chondro, "spcprctl5")
```
A mean intensity map (figure \@ref(fig:rawmap)) is produced by:
```{r include=FALSE}
CAPTION <- "The sum intensity of the raw spectra."
```
```{r}
# Set a color palette
seq_palette <- colorRampPalette(c("white", "dark green"), space = "Lab")
```
```{r rawmap, fig.cap=CAPTION}
plotmap(chondro, func.args = list(na.rm = TRUE), col.regions = seq_palette(20))
```
Function `plotmap()`{.r} squeezes all spectral intensities into a summary characteristic for the whole spectrum.
This function defaults to the `mean()`{.r}.
Further arguments that should be handed to this function can be given in list `func.args`{.r}.
As the raw data contains `NA`{.r}s due to deleting cosmic ray spikes, this argument is needed here.
# Preprocessing
As usual in Raman spectroscopy of biological tissues, the spectra need some preprocessing.
## Aligning the Spatial Grid
The spectra were acquired on a regular square grid of measurement positions over the sample.
The `hyperSpec`{.r} object, however, does not store the data on this grid but rather stores the position where the spectra were taken.
This allows more handling of irregular point patterns, and of sparse measurements where not a
complete (square) is retained but only few positions on this grid.
Occasionally, numeric or rounding errors will lead to seemingly irregular spacing of the data
points.
This is most obvious in false-colour maps of the sample (fig. \@ref(fig:rawmap), as `plotmap`{.r} by default uses `panel.levelplot.raster`{.r}, which assumes a regular grid is underlying the data.
The symptoms are warnings "`'x' values are not equispaced; output may be wrong`" and white stripes in the false colour map (fig. \@ref(fig:irregular-a)), which occur even thought the points are almost at their correct place (fig. \@ref(fig:irregular-b)).
```{r}
## disturb a few points
chondro$x[500] <- chondro$x[500] + rnorm(1, sd = 0.01)
chondro$y[660] <- chondro$y[660] + rnorm(1, sd = 0.01)
```
<!-- irregular-grid ------------------------------------------------------ -->
```{r include=FALSE}
CAPTION = "
Rounding erros in the point coordinates of lateral measurement grid:
**(A)** off-grid points cause stripes.
"
```
```{r irregular-a, fig.cap=CAPTION}
plotmap(chondro, col.regions = seq_palette(20))
```
```{r irregular-a-sub, include=FALSE}
# For subplot A
irregular_grid_a <- as.ggplot(plotmap(chondro, col.regions = seq_palette(20)))
```
<!-- --------------------------------------------------------------------- -->
<!-- irregular-pts ------------------------------------------------------- -->
```{r include=FALSE}
CAPTION = "
Rounding erros in the point coordinates of lateral measurement grid:
**(B)** slightly wrong coordinages.
"
```
```{r irregular-b, fig.cap=CAPTION}
library("latticeExtra")
plotmap(chondro,
col.regions = seq_palette(20), panel = panel.levelplot.points,
col = NA, pch = 22, cex = 1.9
)
```
```{r irregular-b-sub, include=FALSE}
# For subplot B
irregular_grid_b <- as.ggplot(
plotmap(chondro, col.regions = seq_palette(20), panel = panel.levelplot.points,
col = NA, pch = 22, cex = 1.9)
)
```
<!-- --------------------------------------------------------------------- -->
Such slight rounding errors can be corrected by `raster_fit()`{.r} or `raster_make()`{.r}.
Function `raster_fit()`{.r} needs the step size of the raster and a starting coordinate, whereas `raster_make()`{.r} tries to guess these parameters for `raster_fit()`{.r}.
As long as just a few points are affected by rounding errors, `raster_make()`{.r} works fine:
```{r}
chondro$x <- raster_fit(chondro$x)$x
chondro$y <- raster_fit(chondro$y)$x
```
the result is shown in figure \@ref(fig:irregular-c).
<!-- irregular-corr ------------------------------------------------------ -->
```{r include=FALSE}
CAPTION = "
Rounding erros in the point coordinates of lateral measurement grid:
**(C)** corrected grid.
"
```
```{r irregular-c, fig.cap=CAPTION}
plotmap(chondro, col.regions = seq_palette(20))
```
```{r irregular-c-sub, include=FALSE}
# For subplot C
irregular_grid_c <- as.ggplot(plotmap(chondro, col.regions = seq_palette(20)))
```
<!-- --------------------------------------------------------------------- -->
<!-- The subplots -------------------------------------------------------- -->
```{r irregular, fig.cap=CAPTION, fig.height=3, fig.width=12, echo=FALSE, out.width="98%"}
CAPTION = "
Rounding erros in the point coordinates of lateral measurement grid.
**(A)** Off-grid points cause stripes.
**(B)** Slightly wrong coordinages.
**(C)** Corrected grid. "
# Arrange the subplots
ggpubr::ggarrange(
irregular_grid_a, irregular_grid_b, irregular_grid_c, ncol = 3,
labels = LETTERS[1:3]
)
```
<!-- --------------------------------------------------------------------- -->
<!-- ===================================================================== -->
```{block2, type="note-t", echo=show_reviewers_notes}
**V. Gegzna's notes** `chondro-2`
1. Which is better: separate plots \@ref(fig:irregular-a), \@ref(fig:irregular-b), \@ref(fig:irregular-c) or single plot with subplots \@ref(fig:irregular)?
2. The narration and plots do not seem to indicate the same ideas.
E.g., \@ref(fig:irregular-b) looks totally OK, and \@ref(fig:irregular-c) have while pixels.
In the pdf vignettes these two plots look identically.
Has something changed in R or **hyperSpec**?
```
<!-- ===================================================================== -->
## Spectral Smoothing
As the overview shows that the spectra contain `NA`{.r}s (from cosmic spike removal that was done previously), the first step is to remove these.
Also, the wavelength axis of the raw spectra is not evenly spaced (the data points are between `r signif(min(diff(wl(chondro))), 2)` and `r signif(max(diff(wl(chondro))), 2)` $cm^{-1}$ apart from each other).
Furthermore, it would be good to trade some spectral resolution for higher signal to noise ratio.
All three of these issues are tackled by interpolating and smoothing of the wavelength axis by `spc_loess()`{.r}.
The resolution is to be reduced to 8 $cm^{-1}$, or 4 $cm^{-1}$ data point spacing.
```{r interp}
chondro <- spc_loess(chondro, seq(602, 1800, 4))
chondro
```
The spectra are now the same as in the data set `chondro`{.r}.
However, the data set also contains the clustering results (see at the very end of this document).
They are stored for saving as the distributed demo data:
```{r save_chondro}
spectra_to_save <- chondro
```
## Baseline Correction
The next step is a linear baseline correction. `spc_fit_poly_below()`{.r} tries to automatically find appropriate support points for polynomial baselines.
The default is a linear baseline, which is appropriate in our case:
```{r bl}
baselines <- spc_fit_poly_below(chondro)
chondro <- chondro - baselines
```
## Normalization
As the spectra are quite similar, area normalization should work well:
```{r include=FALSE}
# The preprocessed spectra.
CAPTION <- "The spectra after smoothing, baseline correction, and normalization. "
```
```{r bl-norm, fig.cap=CAPTION}
chondro <- chondro / rowMeans(chondro)
plot(chondro, "spcprctl5")
```
Note that normalization effectively cancels the information of one variate (wavelength), it introduces a collinearity.
If needed, this collinearity can be broken by removing one of the variates involved in the normalization.
Note that the `chondro`{.r} object shipped with package **hyperSpec** set has multiple collinearities as only the first 10 principal components are shipped (see below).
For the results of these preprocessing steps, see figure \@ref(fig:bl-norm).
## Subtracting the Overall Composition
The spectra are very homogeneous, but I'm interested in the differences between the different regions of the sample.
Subtracting the minimum spectrum cancels out the matrix composition that is common to all spectra.
But the minimum spectrum also picks up a lot of noise.
So instead, the 5^th^ percentile spectrum is subtracted:
```{r include=FALSE}
# The preprocessed spectra.
CAPTION <- "The spectra after subtracting the 5^th^ percentile spectrum. "
```
```{r bl-perc, fig.cap=CAPTION}
chondro <- chondro - quantile(chondro, 0.05)
plot(chondro, "spcprctl5")
```
The resulting data set is shown in figure \@ref(fig:bl-perc).
Some interesting differences start to show up: there are distinct lipid bands in some but not all of the spectra.
## Outlier Removal by Principal Component Analysis (PCA)
PCA is a technique that decomposes the data into scores and loadings (virtual spectra).
It is known to be quite sensitive to outliers.
Thus, I use it for outlier detection. The resulting scores and loadings are put again into `hyperSpec`{.r} objects by `decomposition()`{.r}:
```{r pca}
pca <- prcomp(chondro, center = TRUE)
scores <- decomposition(chondro, pca$x,
label.wavelength = "PC",
label.spc = "score / a.u."
)
loadings <- decomposition(chondro, t(pca$rotation),
scores = FALSE,
label.spc = "loading I / a.u."
)
```
Plotting the scores of each PC against all other gives a good idea where to look for outliers.
```{r include=FALSE}
CAPTION <- "The `pairs()`{.r} plot of the first 7 scores. "
```
```{r pca-pairs, fig.cap=CAPTION, fig.height=5, fig.width=5}
pairs(scores[[, , 1:7]], pch = 19, cex = 0.5)
```
Now the spectra can be found either by plotting two scores against each other (by `plot()`{.r}) and identifying with `identify()`{.r}, or they can be identified in the score map by `map.identify()`{.r}.
There is also a function to identify spectra in a spectra plot, `identify_spc()`{.r}, which could be used to identify principal components that are heavily influenced, e.g., by cosmic ray spikes.
```{r pca-identify, eval=FALSE}
## omit the first 4 PCs
out <- map.identify(scores [, , 5])
out <- c(out, map.identify(scores [, , 6]))
out <- c(out, map.identify(scores [, , 7]))
```
```{r pca-out, echo=FALSE, results='hide'}
out <- c(105, 140, 216, 289, 75, 69)
```
```{r pca-cols}
out
outcols <- c("red", "blue", "#800080", "orange", "magenta", "brown")
cols <- rep("black", nrow(chondro))
cols[out] <- outcols
```
We can check our findings by comparing the spectra to the bulk of spectra (figure \@ref(fig:pca-outspc)):
```{r include=FALSE}
CAPTION <- "The suspected outlier spectra. "
```
```{r pca-outspc, fig.cap=CAPTION, fig.width=5, fig.height=4.5, small.mar=TRUE}
plot(chondro[1],
plot.args = list(ylim = c(1, length(out) + .7)),
lines.args = list(type = "n")
)
for (i in seq(along = out)) {
plot(chondro, "spcprctl5", yoffset = i, add = TRUE, col = "gray")
plot(chondro[out[i]],
yoffset = i, col = outcols[i], add = TRUE,
lines.args = list(lwd = 2)
)
text(600, i + .33, out[i])
}
```
and also by looking where these spectra appear in the scores `pairs()`{.r} plot (figure \@ref(fig:pca-pairs-2)):
```{r include=FALSE}
CAPTION <- "The `pairs()`{.r} plot of the first 7 scores with suspected outliers. "
```
```{r pca-pairs-2, fig.cap=CAPTION, fig.height=5, fig.width=5}
pch <- rep(46L, nrow(chondro))
pch[out] <- 19L
pairs(scores[[, , 1:7]], pch = pch, cex = 1, col = cols)
```
<!-- ```{r pca-pairs2} -->
<!-- png("chondro-fig--pca-pairs2.png", width = 500, height = 500) -->
<!-- pch <- rep(46L, nrow(chondro)) -->
<!-- pch[out] <- 19L -->
<!-- pairs(scores[[, , 1:7]], pch = pch, cex = 1, col = cols) -->
<!-- dev.off() -->
<!-- ``` -->
Finally, the outliers are removed:
```{r outdel}
chondro <- chondro[-out]
```
# Hierarchical Cluster Analysis (HCA)
HCA merges objects according to their (dis)similarity into clusters. The result is
a dendrogram, a graph stating at which level two objects are similar
and thus grouped together.
The first step in HCA is the choice of the distance.
The R function `dist()`{.r} offers a variety of distance measures to be computed.
The so-called **Pearson** distance $D^2_{Pearson}=\frac{1-\mathrm{COR}\left(X\right)}{2}$ is popular in data analysis of vibrational spectra and is provided by package **hyperSpec**'s `pearson.dist()`{.r} function.
Also for computing the dendrogram, a number of choices are available.
Here we choose **Ward**'s method, and, as it uses **Euclid**ean distance for calculating the dendrogram, **Euclid**ean distance also for the distance matrix:
```{r hca}
dist <- dist(chondro)
dendrogram <- hclust(dist, method = "ward.D2")
```
```{r include=FALSE}
CAPTION <- "Dendogram. "
```
```{r denddummy, fig.cap=CAPTION}
plot(dendrogram, labels = FALSE)
```
In order to get clusters, the dendrogram is cut at a level specified either by height or by the number of clusters.
```{r dendcut}
chondro$clusters <- as.factor(cutree(dendrogram, k = 3))
cols <- c("dark blue", "orange", "#C02020")
```
The result for $k$ = 3 clusters is plotted as a map (figure \@ref(fig:clustmap)).
If the color-coded variate (left hand side of the formula) is a factor, the legend bar does not show intermediate colors, and `hyperSpec`{.r}'s `levelplot()`{.r} method uses the levels of the factor for the legend.
Thus meaningful names are assigned
```{r clustname}
levels(chondro$clusters) <- c("matrix", "lacuna", "cell")
```
and the cluster membership map is plotted:
```{r include=FALSE}
CAPTION <- "Hierarchical cluster analysis: the cluster map for $k=3$ clusters. "
```
```{r clustmap, fig.cap=CAPTION}
print(plotmap(chondro, clusters ~ x * y, col.regions = cols))
```
The cluster membership can also be marked in the dendrogram:
```{r include=FALSE}
CAPTION <- "Hierarchical cluster analysis: the dendrogram."
```
```{r dend, fig.cap=CAPTION}
par(xpd = TRUE) # allow plotting the markers into the margin
plot(dendrogram, labels = FALSE, hang = -1)
mark_groups_in_dendrogram(dendrogram, chondro$clusters, col = cols)
```
Figure \@ref(fig:dend) shows the dendrogram and \@ref(fig:clustmap) the resulting cluster map.
The three clusters correspond to the cartilage matrix, the lacuna and the cells.
The left cell is destroyed and its contents are leaking into the matrix, while the right cells looks intact.
We can plot the cluster mean spectra $\pm$ 1 standard deviation using `aggregate()`{.r} (see figure \@ref(fig:clustmeans)):
```{r include=FALSE}
CAPTION <- "The cluster mean $\\pm$ 1 standard deviation spectra.
The blue cluster shows distinct lipid bands, the yellow cluster collagen, and the red cluster proteins and nucleic acids. "
```
```{r clustmeans, fig.cap=CAPTION, fig.width=7}
cluster.means <- aggregate(chondro, chondro$clusters, mean_pm_sd)
op <- par(las = 1, mar = c(4, 6, 1, 1), mgp = c(4, 1, 0))
plot(cluster.means, stacked = ".aggregate", fill = ".aggregate", col = cols)
par(op)
```
# Plotting a False-Colour Map of Certain Spectral Regions
Package `hyperSpec`{.r} comes with a sophisticated interface for specifying spectral ranges.
Expressing things like 1000$cm^{-1}$ $\pm$ 1 data points is easily possible.
Thus, we can have a fast look at the nucleic acid distribution, using the DNA bands at 728, 782, 1098, 1240, 1482, and 1577 $cm^{-1}:$
```{r include=FALSE}
CAPTION <- "False color map of DNA. "
```
```{r DNA, fig.cap=CAPTION}
DNAcols <- colorRampPalette(c("white", "gold", "dark green"), space = "Lab")(20)
plotmap(chondro[, , c(728, 782, 1098, 1240, 1482, 1577)],
col.regions = DNAcols
)
```
The result is shown in figure \@ref(fig:DNA).
While the nucleus of the right cell shows up nicely, only low concentration remainders are detected of the left cell.
# Smoothed False-Colour Maps and Interpolation
As we plotted only a few selected wavelenths, figure \@ref(fig:DNA) is quite noisy.
Smoothing interpolation could help.
In R, such a smoother is mostly seen as a model, whose predictions are then displayed as smooth map (fig. \@ref(fig:DNAsm)).
This smoothing model can be calculated on the fly, e.g., by using the `panel.2dsmoother()`{.r} wrapper provided by package **latticeExtra**:
```{r include=FALSE}
CAPTION <- "False colour map of the DNA band intensities: smoothed DNA abundance. "
```
```{r DNAsm, fig.cap=CAPTION}
plotmap(chondro[, , c(728, 782, 1098, 1240, 1482, 1577)],
col.regions = DNAcols,
panel = panel.2dsmoother, args = list(span = 0.05)
)
```
For interpolation (i.e., no smoothing at the available data points), a Voronoi plot (a.k.a. Delaunay triangulation) is basically a 2d constant interpolation: each point in space has the same z/color value as its closest available point.
For a full rectangular grid, this corresponds to the usual square/rectangular pixel plot.
With missing points, differences become clear (fig. \@ref(fig:DNAmissing)):
```{r}
tmp <- sample(chondro[, , c(728, 782, 1098, 1240, 1482, 1577)], 500)
```
```{r include=FALSE}
CAPTION <- 'Two-dimesional (2D) "constant" interpolation with missing values: omitting missing data points. '
```
```{r DNAmissing, fig.cap=CAPTION}
plotmap(tmp, col.regions = DNAcols)
```
```{r include=FALSE}
CAPTION <- 'Two-dimesional (2D) "constant" interpolation with missing values: Delaunay triangulation / Voronoi plot. '
```
```{r DNAvoronoi, fig.cap=CAPTION}
plotmap(tmp,
col.regions = DNAcols,
panel = panel.voronoi, pch = 19, col = "black", cex = 0.1
)
```
The 2D linear interpolation can be done, e.g., by the functions provided by package package **akima**.
However, they do not follow the model-predict paradigm, but instead do directly return an object suitable for base plotting with `image()`{.r} (fig. \@ref(fig:DNAinterp)):
```{r include=FALSE}
CAPTION <- "Two-dimensional (2D) interpolation. "
```
```{r DNAinterp, fig.cap=CAPTION}
if (require("akima")) {
tmp <- rowMeans(chondro[[, , c(728, 782, 1098, 1240, 1482, 1577)]])
chondro.bilinear <- interp(chondro$x, chondro$y, as.numeric(tmp), nx = 100, ny = 100)
image(chondro.bilinear,
xlab = labels(chondro, "x"), ylab = labels(chondro, "y"),
col = DNAcols
)
} else {
plot(NULL, xlim = c(0, 1), ylim = c(0, 1))
text(0.5, 0.5, 'Package "akima" is required to create this plot')
}
```
# Saving the data set {#sec:saving-data-set}
Finally, the example data set is put together and saved.
In order to keep the package size small, only a PCA-compressed version with 10 PCs is shipped as example data set of the package.
```{r save-chondro, eval=FALSE}
spectra_to_save$clusters <- factor(NA, levels = levels(chondro$clusters))
spectra_to_save$clusters[-out] <- chondro$clusters
pca <- prcomp(spectra_to_save)
.chondro_scores <- pca$x[, seq_len(10)]
.chondro_loadings <- pca$rot[, seq_len(10)]
.chondro_center <- pca$center
.chondro_wl <- wl(chondro)
.chondro_labels <- lapply(labels(chondro), as.expression)
.chondro_extra <- spectra_to_save$..
chondro <- new("hyperSpec",
spc = tcrossprod(.chondro_scores, .chondro_loadings) +
rep(.chondro_center, each = nrow(.chondro_scores)),
wavelength = .chondro_wl,
data = .chondro_extra,
labels = .chondro_labels
)
```
This is the file distributed with package **hyperSpec** as example data set.
# Session Info {-}
<details><summary>Session info</summary>
```{r session-info-chondro, paged.print=FALSE}
sessioninfo::session_info("hyperSpec")
```
</details>
# References {-}