in plot-api/src/commonMain/kotlin/org/jetbrains/letsPlot/bistro/corr/CorrPlot.kt [241:361]
fun build(): Plot {
if (!(tiles.added || points.added || labels.added)) {
return letsPlot()
}
OptionsConfigurator.configure(tiles, points, labels, flip)
val originalVariables = data.keys.map { it.toString() }.toList()
// Compute correlations
@Suppress("NAME_SHADOWING")
val data = asPlotData(data)
val correlations = when (isCoefficientsMatrix(data)) {
true -> correlationsFromCoefficients(data)
false -> correlations(data, Method::correlationPearson)
}
// variables in the 'original' order
val varsInMatrix = correlations.keys.map { it.first }.toSet()
val varsInOrder = originalVariables.filter { varsInMatrix.contains(it) }
val keepDiag = getKeepMatrixDiag(tiles, points, labels)
val combinedType = OptionsConfigurator.getCombinedMatrixType(tiles, points, labels)
var plot = letsPlot() + colorScale + fillScale
// Add layers
val tooltips = (layerTooltips()
.format(field = "@${CorrVar.CORR}", format = VALUE_FORMAT)
.line("@${CorrVar.CORR}"))
if (tiles.added) {
val layerData = layerData(
tiles,
correlations,
varsInOrder,
keepDiag = keepDiag || combinedType == "full",
threshold
)
plot += geomTile(
data = layerData,
showLegend = showLegend,
tooltips = tooltips,
sampling = samplingNone,
size = 0.0, width = 1.002, height = 1.002
) {
x = CorrVar.X
y = CorrVar.Y
fill = CorrVar.CORR
}
}
if (points.added) {
val layerData = layerData(
points,
correlations,
varsInOrder,
keepDiag = keepDiag || combinedType == "full",
threshold
)
plot += geomPoint(
data = layerData,
showLegend = showLegend,
sizeUnit = "x",
tooltips = tooltips,
sampling = samplingNone
) {
x = CorrVar.X
y = CorrVar.Y
size = CorrVar.CORR_ABS
color = CorrVar.CORR
}
}
if (labels.added) {
val layerData = layerData(
labels,
correlations,
varsInOrder,
keepDiag = keepDiag || combinedType == "full",
threshold
)
plot += geomText(
data = layerData,
showLegend = showLegend,
naText = "",
labelFormat = VALUE_FORMAT,
sizeUnit = "x",
tooltips = tooltips,
sampling = samplingNone,
size = if (labels.mapSize == true) null else 1.0,
color = labels.color
) {
x = CorrVar.X
y = CorrVar.Y
label = CorrVar.CORR
size = CorrVar.CORR_ABS
color = CorrVar.CORR
}
}
// Actual labels on axis.
val (xs, ys) = matrixXYSeries(
correlations, varsInOrder, combinedType, !keepDiag, threshold,
dropDiagNA = !keepDiag,
dropOtherNA = combinedType == "full"
)
val plotSize = plotSize(xs, ys, title != null, showLegend, adjustSize)
plot += ggsize(plotSize.first, plotSize.second)
title?.run { plot += ggtitle(title) }
// preserve the original order on x/y scales
val xsSet = xs.distinct().toSet()
val ysSet = ys.distinct().toSet()
val plotX = varsInOrder.filter { it in xsSet }
val plotY = varsInOrder.filter { it in ysSet }
val onlyTiles = tiles.added && !(points.added || labels.added)
return addCommonParams(plot, plotX, plotY, onlyTiles, flip)
}