Debian Codegraph @

src/dr/app/gui/chart/CorrelationPlot.java

raw symbols SCIP
1 /*
2 * CorrelationPlot.java
3 *
4 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui.chart;
27
28 import dr.stats.DiscreteStatistics;
29 import dr.stats.Variate;
30 import dr.util.Author;
31 import dr.util.Citable;
32 import dr.util.Citation;
33
34 import java.awt.*;
35 import java.awt.geom.AffineTransform;
36 import java.awt.geom.Ellipse2D;
37 import java.util.*;
38
39 /**
40 * Description: A correlation ellipse plot.
41 *
42 * @author Guy Baele
43 */
44 public class CorrelationPlot extends Plot.AbstractPlot implements Citable {
45
46 private static final boolean PRINT_VISUAL_AIDES = false;
47
48 private final double ELLIPSE_WIDTH = 0.65;
49 private final double ELLIPSE_HALF_WIDTH = ELLIPSE_WIDTH / 2;
50
51 private final double NEGATIVE_CORRELATION_DEGREE = 0.785398163;
52 private final double POSITIVE_CORRELATION_DEGREE = 2.35619449;
53
54 //colors from plotcorr R package
55 private final Color[] colorbrewer_RdBu = {new Color(165,15,21),
56 new Color(222, 45, 38),
57 new Color(251, 106, 74),
58 new Color(252, 174, 145),
59 new Color(254, 229, 217),
60 new Color( 255, 255, 255),
61 new Color(239, 243, 255),
62 new Color(189, 215, 231),
63 new Color(107, 174, 214),
64 new Color(49, 130, 189),
65 new Color(8, 81, 156)};
66
67 private final Color[] colorbrewer_RdYlBu = {
68 new Color(165,0,38),
69 new Color(215,48,39),
70 new Color(244,109,67),
71 new Color(253,174,97),
72 new Color(254,224,144),
73 new Color(255,255,191),
74 new Color(224,243,248),
75 new Color(171,217,233),
76 new Color(116,173,209),
77 new Color(69,117,180),
78 new Color(49,54,149)};
79
80 private final Color[] colorbrewer_Spectral = {
81 new Color(158,1,66),
82 new Color(213,62,79),
83 new Color(244,109,67),
84 new Color(253,174,97),
85 new Color(254,224,139),
86 new Color(255,255,191),
87 new Color(230,245,152),
88 new Color(171,221,164),
89 new Color(102,194,165),
90 new Color(50,136,189),
91 new Color(94,79,162)};
92
93 private final Color[] translucentColors = {new Color(165,15,21, 32),
94 new Color(222, 45, 38, 32),
95 new Color(251, 106, 74, 32),
96 new Color(252, 174, 145, 32),
97 new Color(254, 229, 217, 32),
98 new Color(255, 255, 255, 32),
99 new Color(239, 243, 255, 32),
100 new Color(189, 215, 231, 32),
101 new Color(107, 174, 214, 32),
102 new Color(49, 130, 189, 32),
103 new Color(8, 81, 156, 32)};
104
105 private final Color[] colors = colorbrewer_RdYlBu;
106 private final boolean showPoints;
107
108 public CorrelationPlot(String name, java.util.List<Double> xData, java.util.List<Double> yData, boolean showPoints) {
109 super(xData, yData);
110 setName(name);
111 this.showPoints = showPoints;
112 }
113
114 /**
115 * Paint data series
116 */
117 protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) {
118
119 //System.out.println("CorrelationPlot: paintData");
120 //System.out.println("PlotNumber = " + plotNumber);
121 //System.out.println("TotalPlotCount = " + this.plotCount);
122
123 int xCount = xData.getCount();
124 int yCount = yData.getCount();
125
126 double minX = (Double)xData.getMin();
127 double maxX = (Double)xData.getMax();
128 double minY = (Double)yData.getMin();
129 double maxY = (Double)yData.getMax();
130
131 double xMean = (Double)xData.getMean();
132 double yMean = (Double)yData.getMean();
133
134 double[] xDataArray = new double[xCount];
135 double[] yDataArray = new double[yCount];
136 for (int i = 0; i < xCount; i++) {
137 xDataArray[i] = (Double) xData.get(i);
138 yDataArray[i] = (Double) yData.get(i);
139 }
140
141 // if (this.samples) {
142 //
143 // //Only using a subset of available samples
144 // int minCount = Math.min(xCount, yCount);
145 // int sampleSize = minCount;
146 // /*if (sampleSize < 20) {
147 // sampleSize = 20;
148 // }*/
149 // if (sampleSize > 200) {
150 // sampleSize = 200;
151 // }
152 //
153 // xDataArray = new double[sampleSize];
154 // yDataArray = new double[sampleSize];
155 //
156 // int k = 0;
157 // for (int i = 0; i < sampleSize; i++) {
158 //
159 // xDataArray[i] = (Double) xData.get(k);
160 // yDataArray[i] = (Double) yData.get(k);
161 // k += minCount / sampleSize;
162 //
163 // if (xDataArray[i] < minX) {
164 // minX = xDataArray[i];
165 // }
166 // if (xDataArray[i] > maxX) {
167 // maxX = xDataArray[i];
168 // }
169 // if (yDataArray[i] < minY) {
170 // minY = yDataArray[i];
171 // }
172 // if (yDataArray[i] > maxY) {
173 // maxY = yDataArray[i];
174 // }
175 // }
176 //
177 // xCount = sampleSize;
178 // yCount = sampleSize;
179 //
180 //
181 // }
182
183 double correlation = DiscreteStatistics.covariance(xDataArray, yDataArray);
184
185 //System.out.println("plotNumber: " + plotNumber + " ; correlation = " + correlation);
186
187 Color fillColor = colors[(int) (5 + 5 * correlation)];
188 g2.setColor(fillColor);
189
190 //g2.setPaint(linePaint);
191 //g2.setStroke(lineStroke);
192
193 double x1 = (1.0 - ELLIPSE_HALF_WIDTH);
194 double y1 = (1.0 - ELLIPSE_HALF_WIDTH);
195 double x2 = (1.0 + ELLIPSE_HALF_WIDTH);
196 double y2 = (1.0 + ELLIPSE_HALF_WIDTH);
197
198 //System.out.println("(" + x1 + "," + y1 + ") (" + x2 + "," + y2 + ")");
199
200 if (PRINT_VISUAL_AIDES) {
201 drawRect(g2, x1, y1, x2, y2);
202 g2.drawString("plot" + getPlotNumber(), (float) transformX(x1), (float) transformY(y1));
203 }
204
205 double rotationDegree = NEGATIVE_CORRELATION_DEGREE;
206 if (correlation > 0) {
207 rotationDegree = POSITIVE_CORRELATION_DEGREE;
208 }
209
210 double selectedHeight;
211 double absCorrelation = Math.abs(correlation);
212 selectedHeight = 1.0 - absCorrelation;
213
214 //System.out.println("selectedHeight = " + selectedHeight);
215
216 double ellipseWidth = Math.abs(transformX(x2) - transformX(x1));
217 //double ellipseHeight = 0.5 * Math.abs(transformY(y2) - transformY(y1));
218 double ellipseHeight = selectedHeight * Math.abs(transformY(y2) - transformY(y1));
219
220 AffineTransform oldTransform = g2.getTransform();
221
222 //double drawOffset = Math.abs(transformY(y2) - transformY(y1)) - ellipseHeight;
223 //System.out.println("ellipse draw offset = " + drawOffset);
224
225 Shape ellipse = new Ellipse2D.Double(transformX(x1), transformY(y2), ellipseWidth, ellipseHeight);
226
227 //this transformation rotates around the upper right rectangle corner
228 //Shape rotatedEllipse = AffineTransform.getRotateInstance(rotationDegree, transformX(x1), transformY(y2)).createTransformedShape(ellipse);
229
230 //rotate around rectangle center
231 //Shape rotatedEllipse = AffineTransform.getRotateInstance(rotationDegree, transformX((x1+x2)/2.0), transformY((y1+y2)/2.0)).createTransformedShape(ellipse);
232 //rotate around ellipse center
233 Shape rotatedEllipse = AffineTransform.getRotateInstance(rotationDegree, transformX((x1 + x2) / 2.0), transformY(y2) + ellipseHeight / 2.0).createTransformedShape(ellipse);
234
235 //center rotated ellipse within rectangle
236 Shape translatedEllipse = AffineTransform.getTranslateInstance(0.0, transformY((y1 + y2) / 2.0) - (transformY(y2) + ellipseHeight / 2.0)).createTransformedShape(rotatedEllipse);
237
238 //g2.fill(rotatedEllipse);
239 g2.fill(translatedEllipse);
240
241 g2.setColor(Color.BLACK);
242 //g2.draw(rotatedEllipse);
243
244 g2.draw(translatedEllipse);
245
246 if (PRINT_VISUAL_AIDES) {
247 //draw ellipse center in black
248 g2.fill(new Ellipse2D.Double(transformX((x1 + x2) / 2.0), transformY(y2) + ellipseHeight / 2.0, 5, 5));
249
250 //draw rectangle center in black
251 ///g2.fill(new Ellipse2D.Double(transformX((x1+x2)/2.0), transformY((y1+y2)/2.0), 5, 5));
252 g2.draw(new Ellipse2D.Double(transformX((x1 + x2) / 2.0), transformY((y1 + y2) / 2.0), 10, 10));
253 }
254
255 if (showPoints) {
256
257 g2.setColor(new Color(0, 0, 0, 32));
258
259 for (int i = 0; i < xDataArray.length; i++) {
260 //1 unit wide, so divide by maximum data value on both axes
261 double newX = x1 + ((xDataArray[i] - minX) * ELLIPSE_WIDTH / (maxX - minX));
262 double newY = y1 + ((yDataArray[i] - minY) * ELLIPSE_WIDTH / (maxY - minY));
263
264 //System.out.println("(" + xDataArray[i] + "," + yDataArray[i] + ") >>> (" + newX + "," + newY + ")");
265 g2.fill(new Ellipse2D.Double(transformX(newX), transformY(newY), 2, 2));
266 }
267
268 }
269
270 g2.setTransform(oldTransform);
271
272 }
273
274
275 @Override
276 public Citation.Category getCategory() {
277 return Citation.Category.MISC;
278 }
279
280 @Override
281 public String getDescription() {
282 return "method for displaying correlation matrices";
283 }
284
285 @Override
286 public java.util.List<Citation> getCitations() {
287 return Collections.singletonList(CITATION);
288 }
289
290 public static Citation CITATION = new Citation(
291 new Author[]{
292 new Author("DJ", "Murdoch"),
293 new Author("ED", "Chow")
294 },
295 "A graphical display of large correlation matrices",
296 1986,
297 "Am. Stat.",
298 50,
299 178, 180,
300 Citation.Status.PUBLISHED
301 );
302
303 }