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
|
var margin = {top: 20, right: 70, bottom: 60, left: 60}
, width = 960 - margin.left - margin.right
, height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([d3.min(loadings, function(d) { return d[0]; }) + 0.1*d3.min(loadings, function(d) { return d[0]; }), d3.max(loadings, function(d) { return d[0]; })])
.range([ 0, width ]);
var y = d3.scale.linear()
.domain([d3.min(loadings, function(d) { return d[1]; }) + 0.1*d3.min(loadings, function(d) { return d[1]; }), d3.max(loadings, function(d) { return d[1]; })])
.range([ height, 0 ]);
var chart = d3.select('#loadings_plot')
.append('svg:svg')
.attr('width', width + margin.right + margin.left)
.attr('height', height + margin.top + margin.bottom)
.attr('class', 'chart')
var main = chart.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
.attr('width', width)
.attr('height', height)
.attr('class', 'main')
// draw the x axis
var xAxis = d3.svg.axis()
.scale(x)
.orient('bottom');
main.append('g')
.attr('transform', 'translate(0,' + height + ')')
.attr('class', 'x axis')
.call(xAxis);
chart.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", 550)
.attr("y", 480)
.style("font-size", 14)
.style("fill", "blue")
.text("Factor (1)");
chart.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("x", -200)
.attr("y", 5)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.style("font-size", 14)
.style("fill", "blue")
.text("Factor (2)");
// draw the y axis
var yAxis = d3.svg.axis()
.scale(y)
.orient('left');
main.append('g')
.attr('transform', 'translate(0,0)')
.attr('class', 'y axis')
.call(yAxis);
chart.select('.x.axis')
.selectAll("text")
.style("font-size","14px");
chart.select('.y.axis')
.selectAll("text")
.style("font-size","14px");
var g = main.append("svg:g");
g.selectAll("scatter-dots")
.data(loadings)
.enter().append("svg:circle")
.attr("cx", function (d,i) { return x(d[0]); } )
.attr("cy", function (d) { return y(d[1]); } )
.attr("r", 4)
.style("fill", "blue");
traits_and_loadings = []
for (i = 0; i < js_data.traits.length; i++) {
this_trait_loadings = []
this_trait_loadings[0] = js_data.traits[i]
this_trait_loadings[1] = [loadings[i][0], loadings[i][1]]
traits_and_loadings[i] = this_trait_loadings
}
g.selectAll("scatter-dots")
.data(traits_and_loadings)
.enter().append("text")
.attr("x", function(d, i) { return x(d[1][0]); })
.attr("y", function(d) { return y(d[1][1]); })
.text(function(d) { return d[0]; })
.style("font-size", 12)
.style("fill", "blue");
g.selectAll("scatter-lines")
.data(loadings)
.enter().append("svg:line")
.attr("x1", x(0))
.attr("y1", y(0))
.attr("x2", function (d,i) {return x(d[0]); } )
.attr("y2", function (d) { return y(d[1]); } )
.attr("stroke-width", 1)
.attr("stroke", "red");
|