-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParallel_Plot.R
255 lines (196 loc) · 5.73 KB
/
Parallel_Plot.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
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
# Parallel Plot
# makes parallel coordinate plots using csv output muscle data
# to create plot, call the function make_plot
# returns an extended dataframe from original data
# make 3 sections: 1= data, 2= normalized costs, 3= raw costs
make_dataframe = function(data) {
m = dim(data)[2]
data[(m+1):(m+12)] = 0
colnames(data) = 1:(m+12)
return(data)
}
# fills raw cost columns (14-19 for finger)
# section 3 of dataframe
fill_costs = function(df,fmax) {
m = dim(df)[2] - 12
# L1 (col 14)
df[,(m+7)] = rowSums(df[,1:m])
# L2 (col 15)
df[,(m+8)] = (rowSums(df[,1:m]^2))^(1/2)
# L3 (col 16)
df[,(m+9)] = (rowSums(df[,1:m]^3))^(1/3)
F0 = matrix(fmax,dim(df)[1],m,byrow=TRUE)
# Lw1 (col 17)
df[,(m+10)] = rowSums(df[,1:m]*F0)
# Lw2 (col 18)
df[,(m+11)] = (rowSums((df[,1:m]*F0)^2))^(1/2)
# Lw3 (col 19)
df[,(m+12)] = (rowSums((df[,1:m]*F0)^3 ))^(1/3)
return(df)
}
# fills adjusted axes columns (8-13 for finger)
# normalizes cost columns
# section 2 of dataframe
fill_axes = function(df) {
m = dim(df)[2] - 12
for (i in (m+1):(m+6)) {
# subtract by lowest
low = min(df[,(i+6)])
df[,i] = df[,(i+6)] - low
# divided by new highest
high = max(df[,i])
if (high > 0) {
df[,i] = df[,i] / high
}
}
return(df)
}
# plot point lines
points = function(df,alpha) {
m = dim(df)[2] - 12
# define transparency and margins
color_transparent <- adjustcolor(col='blue', alpha.f = alpha)
#plot points
plot(colnames(df)[1:(m+6)], df[1,1:(m+6)], type='l', col=color_transparent, ylim=c(0.0,1.1),
lwd=0.2, axes=FALSE,ann=FALSE)
N = dim(df)[1]
for (i in 2:N) {
lines(colnames(df)[1:(m+6)], df[i,1:(m+6)], type='l', lwd=0.2, col=color_transparent, ylim=c(0.0,1.1))
}
}
# calculate the axes bounds and labels
get_axis_bounds = function(df,axnum) {
# calculate labels
lower = min(df[,(axnum+6)])
upper = max(df[,(axnum+6)])
# find appropriate well-spaced labels
scale = if (upper < 10) 10 else 1
n=5
llabel = trunc(lower*scale)/scale + (1/scale)
inc = trunc((upper-llabel)*scale/n)/scale
ulabel = llabel + (n*inc)
if (upper-ulabel > inc) {
n = 4
inc = trunc((upper-llabel)*scale/n)/scale
ulabel = llabel + (n*inc)
}
dig = if (upper < 10) 1 else 0
labels = formatC(seq(llabel,ulabel,inc), format="f", digits=dig, width=5)
# calculate normalized coordinates
lcoord = (llabel-lower) / (upper-lower)
ucoord = (ulabel-lower) / (upper-lower)
inccoord = (ucoord-lcoord) / n
return(list("low"=lcoord, "high"=ucoord, "inc"=inccoord, "labels"=labels))
}
# plot axes
axes = function(df){
m = dim(df)[2] - 12
# create custom axis for each muscle
for (axnum in 1:m) {
axis(2, at=seq(0,1,0.2), pos=axnum, las=2, lwd=0.2, tck=-0.005, cex.axis=0.2, hadj=1.5)
}
# custom axes for costs
for (axnum in (m+1):(m+6)) {
A = get_axis_bounds(df, axnum)
if (A$high != Inf) {
# axes for custom normalized label locations
axis(2, at=seq(A$low,A$high,A$inc), pos=axnum, labels=A$labels,
las=2, lwd=0.2, tck=-0.005, cex.axis=0.2, hadj=1.5)
# empty axis to make sure it extends all the way from end to end
axis(2, at=c(0,1), pos=axnum, labels=c('',''), las=2, lwd=0.2, tck=-0.005, cex.axis=0.2)
}
else {
axis(2, at=seq(0,1,0.2), pos=axnum, las=2, lwd=0.2, tck=-0.005, cex.axis=0.2, hadj=1.5)
}
}
}
# label the axes
label_axes = function(df,finger=FALSE) {
m = dim(df)[2] - 12
if (finger==TRUE & m==7) {
axis(3, at=1:13, lwd=0, cex.axis=0.3,
lab=c('FP','FS','DI','PI','EI','LUM','EC','L1','L2','L3','L1W','L2W','L3W'),
pos=1, padj=0)
}
else {
mlabels = 1:(m+6)
for (i in 1:m){
mlabels[i] = paste(c("M",i),collapse="")
}
mlabels[(m+1):(m+6)] = c('L1','L2','L3','L1W','L2W','L3W')
axis(3, at=1:(m+6), lwd=0, cex.axis=0.3,
lab=mlabels,
pos=1,padj=0)
}
}
# plot and save to pdf
pdf_plot = function(df,alpha,finger=FALSE) {
# start pdf
pdf(file="Downloads/figure.pdf", height=2.3, width=4,compress=FALSE)
# plot lines
par(mar=c(0,0,0,0), mgp=c(0,0,0))
points(df,alpha)
# plot axes
axes(df)
# label axes
label_axes(df,finger)
# flush out pdf
dev.off()
}
# pdf_mplot
pdf_mplot = function(df,alpha,finger=FALSE) {
# start pdf
pdf(file="Downloads/mult_figure.pdf", height=3, width=4,compress=FALSE)
# make multiple subplots
par(mar=c(0,0.5,0,0.5), mgp=c(0,0,0),mfcol = c(3,2))
N = dim(df)[1]
# 1 PI < 60%
temp = df[df$`4` < 0.6,]
points(temp,alpha)
axes(df)
label_axes(df,finger)
# 2 DI < 60%
temp = df[df$`3` < 0.6,]
points(temp,alpha)
axes(df)
# 3 PI < 60% and DI < 60%
temp = df[(df$`3` < 0.6) & (df$`4` < 0.6),]
points(temp,alpha)
axes(df)
# 4 lower 50% of L1
temp = df[df$`8` %in% sort(df$`8`)[1:floor(N/2)],]
points(temp,alpha)
axes(df)
label_axes(df,finger)
# 5 lower 50% of L2w
temp = df[df$`12` %in% sort(df$`12`)[1:floor(N/2)],]
points(temp,alpha)
axes(df)
# 6 lower 50% of all costs
temp = df
temp[,20] = rowSums(df[,14:19])
temp = temp[temp[,20] %in% sort(temp[,20])[1:floor(N/2)],]
temp = temp[,1:19]
points(temp,alpha)
axes(df)
# flush out pdf
dev.off()
}
# make_plot
# in: csv file (file), (fmax), number of points (N), transparency (t)
# out: pdf of plot in Downloads folder
make_plot = function(file, fmax, N, t, finger=FALSE) {
data = read.csv(file)
df = make_dataframe(data[1:N,])
df = fill_costs(df,fmax)
df = fill_axes(df)
pdf_plot(df,t,finger)
}
# make_mplot
make_mplot = function(file, fmax, N, t, finger=FALSE) {
data = read.csv(file)
df = make_dataframe(data[1:N,])
df = fill_costs(df,fmax)
df = fill_axes(df)
pdf_mplot(df,t,finger)
}