-
Notifications
You must be signed in to change notification settings - Fork 0
/
pca_plots.qmd
186 lines (146 loc) · 3.94 KB
/
pca_plots.qmd
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
# PCA Plots
```{r}
#| label: setup
#| echo: false
#| message: false
source("_common.R")
library(knitr)
```
## Introduction
This tutorial is based on a nice blog post by Claus Wilke: <https://clauswilke.com/blog/2020/09/07/pca-tidyverse-style/>
Functions from the Tidyverse packages (`dplyr` , `tidyr`, `ggplot2`) and `broom` will be used in this tutorial. As example data a lipidomics dataset is imported that contains the lipid concentrations as columns and with one additional column indicating the experimental group
```{r}
#| label: load-lib
#| echo: true
#| message: false
library(here)
library(tidyverse)
library(broom)
library(cowplot)
```
```{r}
#| label: read-data
#| echo: true
#| message: false
d_long <- read_csv(here("data/Metabolites-1614644-supplementary.csv"))
```
```{r}
#| label: tbl-data
#| echo: false
#| tbl-cap: "Test dataset in tidy format"
kable(head(d_long, n = 5),digits = 2)
```
## Transform and Scale the Data
```{r}
#| label: prcomp
#| echo: true
#| message: false
pca_res <- d_long %>%
select(where(is.numeric)) %>% # retain only numeric columns
log2() |>
prcomp(scale = TRUE)
```
Compare this to plotting non-normalized concentrations
```{r}
#| label: pcaplot-1
#| echo: true
#| message: false
# Use broom to make the data ggplot ready and include annotations
pca_annot <- pca_res |> broom::augment(d_long)
pca_contrib <- pca_res |> tidy(matrix = "eigenvalues")
ggplot(data = pca_annot, aes(.fittedPC1, .fittedPC2, color = Group)) +
geom_point(size = 1.5) +
stat_ellipse(level = 0.95) +
theme_light() +
theme(aspect.ratio=1)
```
```{r}
#| label: pcaplot-2
#| echo: true
#| message: false
#| fig-width: 6
#| fig-height: 3
# Use broom to make the data ggplot ready and include annotations
pca_annot <- pca_res |> broom::augment(d_long)
pca_12 <- ggplot(data = pca_annot, aes(.fittedPC1, .fittedPC2, color = Group)) +
geom_point(size = 1.5) +
stat_ellipse(level = 0.95)+
theme_light() +
theme(legend.position = "none")
pca_34 <- ggplot(data = pca_annot, aes(.fittedPC3, .fittedPC4, color = Group)) +
geom_point(size = 1.5) +
stat_ellipse(level = 0.95) +
theme_light() +
theme(legend.position = "none")
cowplot::plot_grid(pca_12, pca_34, nrow = 1,
rel_widths = c(1,1),
labels = c("A","B"))
```
```{r}
#| label: pcaplot-3
#| echo: true
#| message: false
#| fig-width: 6
#| fig-height: 3
# PCA plot of merged
m_log <- d_long |>
dplyr::select(-Group) |>
dplyr::select(where(~!any(is.na(.)))) |>
column_to_rownames("SubjectID") |>
log2()
d_annot <- d_long |>
select(SubjectID, Group)|>
column_to_rownames("SubjectID")
d_annot$Group <- factor(d_annot$Group)
# get pca result with annotation
pca_res <- PCAtools::pca(mat = m_log, center = TRUE,
metadata = d_annot ,
scale = TRUE,
transposed = TRUE)
```
```{r}
#| label: pcaplot-4
#| echo: true
#| message: false
#| fig-width: 6
#| fig-height: 6
#|
p_PCA <- PCAtools::biplot(
pca_res,
x = 1,
y = 2 ,
colby = "Group",
#colkey = colby_color,
legendPosition = 'right',
pointSize = 3,
lengthLoadingsArrowsFactor = 1.5,
lab = NULL,
sizeLoadingsNames = 2.5,
alphaLoadingsArrow = 0.1,
colLoadingsNames = "grey35",
showLoadings = TRUE,
ntopLoadings = 20,
drawConnectorsLoadings = T,
boxedLoadingsNames = T,
widthLoadingsArrows = 0.5,
widthConnectorsLoadings = 0.001,
max.overlaps = 33,
ellipseType = "t",
# xlim = c(-22, 25),
# ylim = c(-21, 20),
ellipse = TRUE,
ellipseLevel = 0.95,
ellipseLineSize = 1,
ellipseFill = TRUE,
ellipseAlpha = .05,
gridlines.minor = FALSE,
)
p_PCA
# theme(
# aspect.ratio = 1,
# panel.grid.major = element_line(size = .5),
# legend.position = c(.91, .93),
# legend.title = element_blank(),
# legend.background = element_rect(fill = alpha("white", 0.0), inherit.blank = TRUE)
# )
```