/
widget_scatterplots.Rmd
100 lines (77 loc) · 2.54 KB
/
widget_scatterplots.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
---
title: "3D scatterplots for PLNPCA"
author: "State of the R"
date: "24-28/08/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
screenshot.force = FALSE,
echo = TRUE,
rows.print = 5,
fig.width = 10,
fig.height = 10,
message = FALSE,
warning = FALSE)
```
## Preliminaries
We will illustrate some widgets for 3D scatterplots to enhance output of PCA in **PLNmodels**. The packages required for the analysis are **PLNmodels** plus some others for data manipulation and representation:
```{r dependencies}
library(PLNmodels)
library(tidyverse)
library(ggplot2)
```
Here are a collection of packages implementing various JS widgets:
```{r widgets}
library(DT)
library(scatterD3)
library(plotly)
library(edgebundleR)
library(threejs)
library(networkD3)
```
The _oaks amplicon data set_ at play gives the abundance of 114 taxa (66 bacterial OTU, 48 fungal OTUs) in 116 samples. For each sample, 11 additional covariates are known
```{r loading}
data(oaks)
datatable(oaks$Abundance[, 1:5] )
```
## PLNmodels Analyses
### PCA analysis
We only fit two PCA models, which we know are in the right range for ICL
```{r principal component analysis, results='hide'}
my_PCAs <- PLNPCA(Abundance ~ 1 + offset(log(Offset)), data = oaks, ranks = 27:28)
```
## Outputs and Vizualisation
### Principal Components Map
We explore various scatterplot solutions to represent the individual factor map of the PLN PCA.
```{r data for scatterplot}
coord <- my_PCAs$getBestModel()$scores[, 1:3] %>%
as.data.frame() %>%
setNames(c("PC1", "PC2", "PC3")) %>%
add_column(tree = oaks$tree, names = rownames(oaks$Abundance))
```
#### ScatterD3 (Another fancy scatterplot)
```{r scatterD3}
scatterD3(data = coord, x = PC1, y = PC2, lab = names,
col_var = tree, symbol_var = tree,
xlab = "PC1", ylab = "PC2", col_lab = "tree",
symbol_lab = "tree", lasso = TRUE)
```
#### Native plotly (3D scatterplot)
```{r plotly}
fig <- plot_ly(
coord, x = ~PC1, y = ~PC2, z = ~PC3, color = ~tree, size = .35,
text = ~paste('status:', tree), type = "scatter3d") %>%
layout(title = "Individual Factor Map of the Oaks powdery Mildew data set",
scene = list(xaxis = list(title = 'PC1'),
yaxis = list(title = 'PC2'),
zaxis = list(title = 'PC3'))
)
fig
```
#### threejs (3D scatterplot)
```{r threejs}
group <- rainbow(3)[as.numeric(oaks$tree)]
coord %>% select(1:3) %>% as.matrix() %>%
scatterplot3js(col = group, size = 0.25, pch = ".", grid = FALSE, bg = "black")
```