Skip to content

Commit

Permalink
Fix geom_boxplot: should be possible to create boxplot without specif…
Browse files Browse the repository at this point in the history
…ying x-series [#325]
  • Loading branch information
alshan committed Apr 27, 2021
1 parent e492dac commit 777bb2b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import jetbrains.datalore.base.gcommon.collect.Iterables
import jetbrains.datalore.base.gcommon.collect.Sets
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.base.values.Pair
import jetbrains.datalore.plot.base.*
import jetbrains.datalore.plot.base.Aes
import jetbrains.datalore.plot.base.Aesthetics
import jetbrains.datalore.plot.base.GeomContext
import jetbrains.datalore.plot.base.PositionAdjustment
import jetbrains.datalore.plot.base.aes.AestheticsBuilder
import jetbrains.datalore.plot.base.aes.AestheticsBuilder.Companion.listMapper
import jetbrains.datalore.plot.base.data.DataFrameUtil
Expand Down Expand Up @@ -351,8 +354,12 @@ object PlotUtil {

// expand X-range to ensure that the data is placed some distance away from the axes.
// see: https://ggplot2.tidyverse.org/current/scale_continuous.html - expand
val mulExp = getMultiplicativeExpand(layer, aes)
val addExp = getAdditiveExpand(layer, aes)
// val mulExp = getMultiplicativeExpand(layer, aes)
// val addExp = getAdditiveExpand(layer, aes)

val scale = layer.scaleMap[aes]
val mulExp = scale.multiplicativeExpand
val addExp = scale.additiveExpand
val lowerEndpoint = range.lowerEnd
val upperEndpoint = range.upperEnd

Expand All @@ -376,30 +383,30 @@ object PlotUtil {
return ClosedRange(lowerEndpoint - lowerExpand, upperEndpoint + upperExpand)
}

private fun getMultiplicativeExpand(layer: GeomLayer, aes: Aes<Double>): Double {
val scale = findBoundScale(layer, aes)
return scale?.multiplicativeExpand ?: 0.0
}

private fun getAdditiveExpand(layer: GeomLayer, aes: Aes<Double>): Double {
val scale = findBoundScale(layer, aes)
return scale?.additiveExpand ?: 0.0
}

private fun findBoundScale(layer: GeomLayer, aes: Aes<*>): Scale<*>? {
if (layer.hasBinding(aes)) {
return layer.scaleMap[aes]
}
if (Aes.isPositional(aes)) {
val horizontal = Aes.isPositionalX(aes)
for (rendered in layer.renderedAes()) {
if (layer.hasBinding(rendered)) {
if (horizontal && Aes.isPositionalX(rendered) || !horizontal && Aes.isPositionalY(rendered)) {
return layer.scaleMap[aes]
}
}
}
}
return null
}
// private fun getMultiplicativeExpand(layer: GeomLayer, aes: Aes<Double>): Double {
// val scale = findBoundScale(layer, aes)
// return scale?.multiplicativeExpand ?: 0.0
// }
//
// private fun getAdditiveExpand(layer: GeomLayer, aes: Aes<Double>): Double {
// val scale = findBoundScale(layer, aes)
// return scale?.additiveExpand ?: 0.0
// }
//
// private fun findBoundScale(layer: GeomLayer, aes: Aes<*>): Scale<*>? {
// if (layer.hasBinding(aes)) {
// return layer.scaleMap[aes]
// }
// if (Aes.isPositional(aes)) {
// val horizontal = Aes.isPositionalX(aes)
// for (rendered in layer.renderedAes()) {
// if (layer.hasBinding(rendered)) {
// if (horizontal && Aes.isPositionalX(rendered) || !horizontal && Aes.isPositionalY(rendered)) {
// return layer.scaleMap[aes]
// }
// }
// }
// }
// return null
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,24 @@ internal object PlotAssemblerUtil {
return initialRange
}

var xRangeOverall: ClosedRange<Double>? = null
var yRangeOverall: ClosedRange<Double>? = null

// the "scale map" is shared by all layers.
// // the "scale map" is shared by all layers.
val scaleMap = layersByTile[0][0].scaleMap
if (scaleMap.containsKey(Aes.X)) {
xRangeOverall = initialRange(scaleMap[Aes.X])
}
if (scaleMap.containsKey(Aes.Y)) {
yRangeOverall = initialRange(scaleMap[Aes.Y])
var xRangeOverall: ClosedRange<Double>? = initialRange(scaleMap[Aes.X])
var yRangeOverall: ClosedRange<Double>? = initialRange(scaleMap[Aes.Y])

fun completeOverallRange(
layer: GeomLayer,
aes: Aes<Double>,
initialRange: ClosedRange<Double>?,
aestheticsRange: ClosedRange<Double>?,
): ClosedRange<Double>? {
var range: ClosedRange<Double>? = updateRange(aestheticsRange, initialRange)
range = PlotUtil.rangeWithExpand(layer, aes, range)
// include zero if necessary
if (layer.rangeIncludesZero(aes)) {
range = updateRange(ClosedRange.singleton(0.0), range)
}
return range
}

for (layers in layersByTile) {
Expand All @@ -276,19 +284,8 @@ internal object PlotAssemblerUtil {
// adjust X/Y range with 'pos adjustment' and 'expands'
val xyRanges = computeLayerDryRunXYRanges(layer, aesthetics)

xRangeOverall = updateRange(xyRanges.first, xRangeOverall)
xRangeOverall = PlotUtil.rangeWithExpand(layer, Aes.X, xRangeOverall)
// include zero if necessary
if (layer.rangeIncludesZero(Aes.X)) {
xRangeOverall = updateRange(ClosedRange.singleton(0.0), xRangeOverall)
}

yRangeOverall = updateRange(xyRanges.second, yRangeOverall)
yRangeOverall = PlotUtil.rangeWithExpand(layer, Aes.Y, yRangeOverall)
// include zero if necessary
if (layer.rangeIncludesZero(Aes.Y)) {
yRangeOverall = updateRange(ClosedRange.singleton(0.0), yRangeOverall)
}
xRangeOverall = completeOverallRange(layer, Aes.X, xRangeOverall, xyRanges.first)
yRangeOverall = completeOverallRange(layer, Aes.Y, yRangeOverall, xyRanges.second)
}
}

Expand Down

0 comments on commit 777bb2b

Please sign in to comment.