-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ca.R
58 lines (46 loc) · 1.96 KB
/
test_ca.R
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
# See https://www.displayr.com/how-correspondence-analysis-works/ for data examples
library(ggplot2)
library(ggrepel)
source("myCA.R")
# Read original data used to compute CA
mydata <- read.csv(file="data/test_ca.data.csv", check.names=FALSE, head=TRUE, quote = "\"", sep=",", dec=".")
Y = data.matrix(mydata[,c("Big", "Athletic", "Friendly", "Trainable", "Resourceful", "Animal", "Lucky")])
rownames(Y) = mydata[,c("Name")]
# Read supplementary rows
otherdata <- read.csv(file="data/test_ca.suppl.csv", check.names=FALSE, head=TRUE, quote = "\"", sep=",", dec=".")
Y2 = data.matrix(otherdata[,c("Big", "Athletic", "Friendly", "Trainable", "Resourceful", "Animal", "Lucky")])
rownames(Y2) = otherdata[,c("Name")]
# Compute CA
myCA = myCA(Y, lessMemory = FALSE)
# Project supplementary rows
other = myCA_project(myCA, Y2)
# Concatenate data before ploting
Pdf = as.data.frame(myCA$colProj)
rownames(Pdf) = colnames(Y)
Pdf$type = 'characteristic'
Tdf = as.data.frame(myCA$rowProj)
rownames(Tdf) = rownames(Y)
Tdf$type = 'animal'
Hdf = as.data.frame(other$rowProj)
rownames(Hdf) = rownames(Y2)
Hdf$type = 'animal'
plotData = rbind(Pdf, Tdf, Hdf)
# Plot
mytheme <- theme_bw(14) +
theme(
panel.grid = element_blank(),
axis.text = element_text(size = rel(1)),
axis.title = element_text(size = rel(1)),
strip.text = element_text(size = rel(1)),
strip.background = element_rect(fill = "grey90"),
legend.position = "none",
panel.border = element_rect()
)
plotColors = c('red', 'blue')
names(plotColors) = c('animal', 'characteristic')
ggplot(data = plotData, aes(x = V1, y = V2, color = type)) +
geom_point() + coord_fixed() + mytheme +
geom_hline(yintercept = 0, linetype="dashed") + geom_vline(xintercept = 0, linetype="dashed") +
geom_label_repel(data=plotData, aes(fill = type, label=rownames(plotData)), color = 'white') +
scale_color_manual(breaks = names(plotColors), values = plotColors) +
scale_fill_manual(breaks = names(plotColors), values = plotColors)