in plot-stem/src/commonMain/kotlin/org/jetbrains/letsPlot/core/spec/back/transform/bistro/corr/CorrPlotOptionsBuilder.kt [213:363]
fun build(): PlotOptions {
if (!(tiles.added || points.added || labels.added)) {
return PlotOptions()
}
OptionsConfigurator.configure(tiles, points, labels, flip)
val data = standardiseData(data)
val correlations = when (coefficients) {
true -> correlationsFromCoefficients(data)
false -> computeCorrelations(data, ::correlationPearson)
}
// variables in the 'original' order
val varsInMatrix = correlations.keys.map { it.first }.toSet()
val varsInOrder = data.keys.filter(varsInMatrix::contains)
val keepDiag = getKeepMatrixDiag(tiles, points, labels)
val combinedType = OptionsConfigurator.getCombinedMatrixType(tiles, points, labels)
val layers = mutableListOf<LayerOptions>()
// Add layers
if (tiles.added) {
layers.add(
newCorrPlotLayerOptions {
geom = GeomKind.TILE
this.data = layerData(
tiles,
correlations,
varsInOrder,
keepDiag = keepDiag || combinedType == FULL,
threshold
)
mapping = Mapping(
Aes.X to CorrVar.X,
Aes.Y to CorrVar.Y,
Aes.FILL to CorrVar.CORR,
)
size = 0.0
width = 1.002
height = 1.002
}
)
}
if (points.added) {
layers.add(
newCorrPlotLayerOptions {
geom = GeomKind.POINT
prop[PointLayer.SIZE_UNIT] = Aes.X
this.data = layerData(
points,
correlations,
varsInOrder,
keepDiag = keepDiag || combinedType == FULL,
threshold
)
mapping = Mapping(
Aes.X to CorrVar.X,
Aes.Y to CorrVar.Y,
Aes.SIZE to CorrVar.CORR_ABS,
Aes.COLOR to CorrVar.CORR,
)
}
)
}
if (labels.added) {
layers.add(
newCorrPlotLayerOptions {
geom = GeomKind.TEXT
prop[TextLayer.NA_TEXT] = ""
prop[TextLayer.LABEL_FORMAT] = VALUE_FORMAT
prop[TextLayer.SIZE_UNIT] = Aes.X
this.data = layerData(
labels,
correlations,
varsInOrder,
keepDiag = keepDiag || combinedType == FULL,
threshold
)
mapping = Mapping(
Aes.X to CorrVar.X,
Aes.Y to CorrVar.Y,
Aes.LABEL to CorrVar.CORR,
Aes.SIZE to CorrVar.CORR_ABS,
Aes.COLOR to CorrVar.CORR
)
size = if (labels.mapSize == true) null else 1.0
color = labels.color
}
)
}
// Actual labels on axis.
val (xs, ys) = matrixXYSeries(
correlations, varsInOrder, combinedType, !keepDiag, threshold,
dropDiagNA = !keepDiag,
dropOtherNA = combinedType == FULL
)
// 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 }
return plot {
size = plotSize(xs, ys, title != null, showLegend, adjustSize)
title = title
layerOptions = layers
themeOptions = theme {
axisTitle = Element.BLANK
axisLine = Element.BLANK
panelGrid = Element.BLANK
axisTicksX = Element.line()
axisTicksY = Element.line()
}
scaleOptions = listOf(
scale {
aes = Aes.SIZE
mapperKind = MapperKind.IDENTITY
naValue = 0
guide = GuideOptions.none()
},
scale {
aes = Aes.X
isDiscrete = true
breaks = plotX
limits = plotX
expand = EXPAND
},
scale {
aes = Aes.Y
isDiscrete = true
breaks = plotY
limits = if (flip) plotY.asReversed() else plotY
expand = EXPAND
},
colorScaleOptions,
fillScaleOptions
)
coord = coord {
name = CoordOptions.CoordName.FIXED
xLim = Pair(-0.6, plotX.size - 1 + 0.6)
yLim = Pair(-0.6, plotY.size - 1 + 0.6)
}
}
}