| 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 |
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 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 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 |
|
| 116 |
|
| 117 |
protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 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 |
|
| 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 |
double correlation = DiscreteStatistics.covariance(xDataArray, yDataArray); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
Color fillColor = colors[(int) (5 + 5 * correlation)]; |
| 188 |
g2.setColor(fillColor); |
| 189 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 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 |
|
| 215 |
|
| 216 |
double ellipseWidth = Math.abs(transformX(x2) - transformX(x1)); |
| 217 |
|
| 218 |
double ellipseHeight = selectedHeight * Math.abs(transformY(y2) - transformY(y1)); |
| 219 |
|
| 220 |
AffineTransform oldTransform = g2.getTransform(); |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
Shape ellipse = new Ellipse2D.Double(transformX(x1), transformY(y2), ellipseWidth, ellipseHeight); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
Shape rotatedEllipse = AffineTransform.getRotateInstance(rotationDegree, transformX((x1 + x2) / 2.0), transformY(y2) + ellipseHeight / 2.0).createTransformedShape(ellipse); |
| 234 |
|
| 235 |
|
| 236 |
Shape translatedEllipse = AffineTransform.getTranslateInstance(0.0, transformY((y1 + y2) / 2.0) - (transformY(y2) + ellipseHeight / 2.0)).createTransformedShape(rotatedEllipse); |
| 237 |
|
| 238 |
|
| 239 |
g2.fill(translatedEllipse); |
| 240 |
|
| 241 |
g2.setColor(Color.BLACK); |
| 242 |
|
| 243 |
|
| 244 |
g2.draw(translatedEllipse); |
| 245 |
|
| 246 |
if (PRINT_VISUAL_AIDES) { |
| 247 |
|
| 248 |
g2.fill(new Ellipse2D.Double(transformX((x1 + x2) / 2.0), transformY(y2) + ellipseHeight / 2.0, 5, 5)); |
| 249 |
|
| 250 |
|
| 251 |
|
| 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 |
|
| 261 |
double newX = x1 + ((xDataArray[i] - minX) * ELLIPSE_WIDTH / (maxX - minX)); |
| 262 |
double newY = y1 + ((yDataArray[i] - minY) * ELLIPSE_WIDTH / (maxY - minY)); |
| 263 |
|
| 264 |
|
| 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 |
} |