Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jitter reproducibility in geom_jitter() #920

Merged
merged 7 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Support seed in jitterdodge.
  • Loading branch information
RYangazov committed Nov 7, 2023
commit 6c1b039570259b2aed7b8ac635d6185b9ed02108
2 changes: 1 addition & 1 deletion future_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
### Changed

### Fixed
- Jitter reproducibility in geom_jitter [[#911](https://github.com/JetBrains/lets-plot/issues/911)].
- Jitter reproducibility in geom_jitter, position_jitter, position_jitterdodge [[#911](https://github.com/JetBrains/lets-plot/issues/911)].
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ import org.jetbrains.letsPlot.core.plot.base.DataPointAesthetics
import org.jetbrains.letsPlot.core.plot.base.GeomContext
import org.jetbrains.letsPlot.core.plot.base.PositionAdjustment

class JitterDodgePos(aesthetics: Aesthetics, groupCount: Int, width: Double?, jitterWidth: Double?, jitterHeight: Double?) :
class JitterDodgePos(
aesthetics: Aesthetics,
groupCount: Int,
width: Double?,
jitterWidth: Double?,
jitterHeight: Double?,
jitterSeed: Long? = null
) :
PositionAdjustment {
private val myJitterPosHelper: PositionAdjustment
private val myDodgePosHelper: PositionAdjustment

init {
myJitterPosHelper = JitterPos(jitterWidth, jitterHeight)
myJitterPosHelper = JitterPos(jitterWidth, jitterHeight, jitterSeed)
myDodgePosHelper = DodgePos(aesthetics, groupCount, width)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ internal class JitterPos(width: Double?, height: Double?, seed: Long? = null) :
}

companion object {

val DEF_JITTER_WIDTH = 0.4
val DEF_JITTER_HEIGHT = 0.4
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ object PositionAdjustments {
groupCount: Int,
width: Double?,
jitterWidth: Double?,
jitterHeight: Double?
jitterHeight: Double?,
jitterSeed: Long?
): PositionAdjustment {
return JitterDodgePos(aesthetics, groupCount, width, jitterWidth, jitterHeight)
return JitterDodgePos(aesthetics, groupCount, width, jitterWidth, jitterHeight, jitterSeed)
}

enum class Meta(private val handlesGroups: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,19 @@ abstract class PosProvider {
}
*/

fun jitterDodge(width: Double?, jitterWidth: Double?, jitterHeight: Double?): PosProvider {
fun jitterDodge(width: Double?, jitterWidth: Double?, jitterHeight: Double?, jitterSeed: Long?): PosProvider {
return object : PosProvider() {
override fun createPos(ctx: PosProviderContext): PositionAdjustment {
val aesthetics = ctx.aesthetics
val groupCount = ctx.groupCount
return PositionAdjustments.jitterDodge(aesthetics, groupCount, width, jitterWidth, jitterHeight)
return PositionAdjustments.jitterDodge(
aesthetics,
groupCount,
width,
jitterWidth,
jitterHeight,
jitterSeed
)
}

override fun handlesGroups(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ object Option {
const val DODGE_WIDTH = "dodge_width"
const val JITTER_WIDTH = "jitter_width"
const val JITTER_HEIGHT = "jitter_height"
const val JITTER_SEED = "jitter_seed"
}

object Stack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ internal object PosProto {
JITTER_DODGE -> PosProvider.jitterDodge(
opts.getDouble(Pos.JitterDodge.DODGE_WIDTH),
opts.getDouble(Pos.JitterDodge.JITTER_WIDTH),
opts.getDouble(Pos.JitterDodge.JITTER_HEIGHT)
opts.getDouble(Pos.JitterDodge.JITTER_HEIGHT),
opts.getLong(Pos.JitterDodge.JITTER_SEED)
)

else -> throw IllegalArgumentException("Unknown position adjustments name: '$posName'")
Expand Down
10 changes: 7 additions & 3 deletions python-package/lets_plot/plot/pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def position_nudge(x=None, y=None):
return _pos('nudge', x=x, y=y)


def position_jitterdodge(dodge_width=None, jitter_width=None, jitter_height=None):
def position_jitterdodge(dodge_width=None, jitter_width=None, jitter_height=None, jitter_seed=None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In R:

position_jitterdodge(
  jitter.width = NULL,
  jitter.height = 0,
  dodge.width = 0.75,
  seed = NA
)

"""
This is primarily used for aligning points generated through `geom_point()`
with dodged boxplots (e.g., a `geom_boxplot()` with a fill aesthetic supplied).
Expand All @@ -207,6 +207,9 @@ def position_jitterdodge(dodge_width=None, jitter_width=None, jitter_height=None
Jittering height.
The value of `jitter_height` is relative and typically ranges between 0 and 0.5.
Values that are greater than 0.5 lead to overlapping of the points.
jitter_seed : int
A random seed to make the jitter reproducible.
If None (the default value), the seed is initialised with a random value.

Returns
-------
Expand Down Expand Up @@ -235,10 +238,11 @@ def position_jitterdodge(dodge_width=None, jitter_width=None, jitter_height=None
stat='boxplot') + \\
geom_point(aes(x='c', y='x', color='c'), \\
size=4, shape=21, fill='white',
position=position_jitterdodge())
position=position_jitterdodge(jitter_seed=42))

"""
return _pos('jitterdodge', dodge_width=dodge_width, jitter_width=jitter_width, jitter_height=jitter_height)
return _pos('jitterdodge', dodge_width=dodge_width, jitter_width=jitter_width, jitter_height=jitter_height,
jitter_seed=jitter_seed)


def position_stack(vjust=None, mode=None):
Expand Down