forked from KatiePelletier/WingShapeBSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectionFigs.R
More file actions
190 lines (141 loc) · 7.05 KB
/
projectionFigs.R
File metadata and controls
190 lines (141 loc) · 7.05 KB
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
#I can't make these look the way I want using base so I am going to convert to ggplot.
#plus, because they are all ggobjects, they will go directly into cowplot.
library(ggplot2)
library(cowplot)
library(latex2exp)
# two raw vectors
x <- seq(0, 1.5, 0.1)
y <- seq(0, 1.5, 0.1)
dat1 <- data.frame(x = seq(0, 1.5, 0.1), y = seq(0, 1.5, 0.1))
x_vector <- c(1, 1)
length_x <- sqrt(t(x_vector) %*% x_vector)
x_vector_scaled <- x_vector/length_x
x1dat <- data.frame(x = c(0, 1/length_x), y = c(0, 1/length_x))
y_vector <- c(0.5, 0.25)
length_y <- sqrt(t(y_vector) %*% y_vector)
y_vector_scaled <- y_vector/length_y
dsdat <- data.frame(x = c(0, 0.5/length_y), y = c(0, 0.25/length_y))
#Now I want to make the plot
gmaxDs <- ggplot(x1dat, aes(x, y)) +
geom_line(arrow = arrow(), col = "blue") +
annotate("text", x = x1dat[2,1], y = x1dat[2,2] + 0.03, label = expression(paste(italic(g))[paste(italic(max))]), col = "blue", size = 8) +
geom_line(aes(dsdat$x, dsdat$y), arrow = arrow(), col = "red") +
annotate("text", x = dsdat[2,1], y = dsdat[2,2] + 0.03, label = expression(paste(italic("ds"))), col = "red", size = 8) +
annotate("text", x = 0.24, y = 0.18, label = expression(theta), col = "black", size = 10) +
annotate("text", x = 0.28, y = 0.7, label = TeX(r"($ \textit{r} = \frac{\textbf{ds} \cdot \textbf{g_{max}}}{||\textbf{ds}|| \times ||\textbf{g_{max}}||}$)"), size = 8) +
xlab(expression(x[1])) +
ylab(expression(x[2])) +
theme_classic() +
xlim(0, 1) +
ylim(0, 1) +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
text = element_text(size=20))
gmaxDs
#Now onto the projection/shape score figure
# x <- seq(0, 1.5, 0.1)
# y <- seq(0, 1.5, 0.1)
#
# newds <- data.frame(x = seq(0, 1.5, 0.1), y = seq(0, 1.5, 0.1))
ds_new <- c(1.2, 0.6)
length_ds_new <- sqrt(t(y_vector) %*% y_vector)
ds_new_scaled <- y_vector/length_y
dsnewdat <- data.frame(x = c(0, ds_new[1]), y = c(0, ds_new[2]))
#making the two vecs
y1_vector <- c(0.8, 1)
length_y1 <- sqrt(t(y1_vector) %*% y1_vector)
y1dat <- data.frame(x = c(0, 0.8), y = c(0, 1))
proj_veckp = as.vector( (y1_vector %*% ds_new) / norm(ds_new, type = "2")^2 ) * ds_new
projdf <- data.frame(x = c(y1dat[2,1], proj_veckp[1]), y = c(y1dat[2,2], proj_veckp[2]))
y2_vector <- c(0.22, 0.85)
length_y2 <- sqrt(t(y2_vector) %*% y2_vector)
y2dat <- data.frame(x = c(0, 0.22), y = c(0, 0.85))
proj_vec2 = as.vector( (y2_vector %*% ds_new) / norm(ds_new, type = "2")^2 ) * ds_new
projdf2 <- data.frame(x = c(y2dat[2,1], proj_vec2[1]), y = c(y2dat[2,2], proj_vec2[2]))
shapescore <- ggplot(dsnewdat, aes(x, y)) +
geom_line(arrow = arrow(), col = "red", size = 1.5) +
annotate("text", x = dsnewdat[2,1], y = dsnewdat[2,2] + 0.05, label = expression(paste(italic("ds"))), col = "red", size = 8) +
geom_line(data = y1dat, aes(x, y), arrow = arrow(), col = "black", size = 1.5) +
annotate("text", x = y1dat[2,1], y = y1dat[2,2] + 0.05, label = expression(y[1]), col = "black", size = 10) +
geom_line(data = projdf, aes(x, y), arrow = arrow(), col = "black", linetype = "dashed") +
geom_line(data = y2dat, aes(x, y), arrow = arrow(), col = "darkgray", size = 1.5) +
annotate("text", x = y2dat[2,1], y = y2dat[2,2] + 0.05, label = expression(y[2]), col = "darkgrey", size = 10) +
geom_line(data = projdf2, aes(x, y), arrow = arrow(), col = "darkgray", linetype = "dashed") +
annotate("text", x = 0.7, y = 0.1, label = TeX(r"($ || proj_{\textbf{ds}} \textbf{y} || = \frac{\textbf{y} \cdot \textbf{ds}}{ ||\textbf{ds}||}$)"), size = 8) +
xlab(expression(x[1])) +
ylab(expression(x[2])) +
theme_classic() +
xlim(0, 1.25) +
ylim(0, 1.25) +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
text = element_text(size=20))
png("../Figures/shapeScorefig.png")
shapescore
dev.off()
#####
#Now for the same figure with gmax
# y1_vector <- c(0.8, 1)
# length_y1 <- sqrt(t(y1_vector) %*% y1_vector)
#
# y2_vector <- c(0.22, 0.85)
# length_y2 <- sqrt(t(y2_vector) %*% y2_vector)
gmaxvec <- c(1.1, 0.9)
length_gmaxvec <- sqrt(t(gmaxvec) %*% gmaxvec)
gmaxvec_scaled <- gmaxvec /length_gmaxvec
gmaxdat <- data.frame(x = c(0,gmaxvec[1]), y = c(0, gmaxvec[2]))
proj_vec3 = as.vector( (y1_vector %*% gmaxvec) / norm(gmaxvec, type = "2")^2 ) * gmaxvec
projdf3 <- data.frame(x = c(y1dat[2,1], proj_vec3[1]), y = c(y1dat[2,2], proj_vec3[2]))
proj_vec4 = as.vector( (y2_vector %*% gmaxvec) / norm(gmaxvec, type = "2")^2 ) * gmaxvec
projdf4 <- data.frame(x = c(y2dat[2,1], proj_vec4[1]), y = c(y2dat[2,2], proj_vec4[2]))
gmaxproj <- ggplot(gmaxdat, aes(x, y)) +
geom_line(arrow = arrow(), col = "blue", size = 1.5) +
annotate("text", x = gmaxdat[2,1], y = gmaxdat[2,2] + 0.05, label = expression(paste(italic(g))[paste(italic(max))]) , col = "blue", size = 8) +
geom_line(data = y1dat, aes(x, y), arrow = arrow(), col = "black", size = 1.5) +
annotate("text", x = y1dat[2,1], y = y1dat[2,2] + 0.05, label = expression(y[1]), col = "black", size = 10) +
geom_line(data = projdf3, aes(x, y), arrow = arrow(), col = "black", linetype = "dashed") +
geom_line(data = y2dat, aes(x, y), arrow = arrow(), col = "darkgray", size = 1.5) +
annotate("text", x = y2dat[2,1], y = y2dat[2,2] + 0.05, label = expression(y[2]), col = "darkgrey", size = 10) +
geom_line(data = projdf4, aes(x, y), arrow = arrow(), col = "darkgray", linetype = "dashed") +
xlab(expression(x[1])) +
ylab(expression(x[2])) +
theme_classic() +
xlim(0, 1.25) +
ylim(0, 1.25) +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
text = element_text(size=20))
gmaxproj
###############
#and the final pannel with the points to show what that corralation looks like
#first I need a bunch of points, each with a PC1 score and ds shape score.
#I should look at the actual mean and sd of the data and fix this
#Pulled these from the wild wing estimates
sig <- matrix(c(0.1, 0.1,0.001,0.2),2,2)
points <- data.frame(mvrnorm(n =100, c(2, 5), sig))
# data.frame(gmax = rnorm(100, 4.7007e-20, 0.009032069), ds = rnorm(100, -5.16757e-19, 0.008005567))
scatterplot <- ggplot(points, aes(x = points[,1], y = points[,2])) +
geom_smooth(method = "lm", se = FALSE, colour = "grey") +
geom_point() +
theme_classic() +
ylab(expression(paste(italic("ds"), " shape score"))) +
xlab(expression(paste(italic(g))[paste(italic(max))])) +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
text = element_text(size=20))
scatterplot
#Making the full figure
library(cowplot)
projectionplot <- plot_grid(shapescore, gmaxproj, gmaxDs, scatterplot,
labels = c("A", "B", "C", "D"))
png("../Figures/projectionFigures.png", width =2000, height = 2000, units = "px",res = 200)
projectionplot
dev.off()