Using position_jitter creates random jitter to prevent overplotting of data points.
In the below I have used the example of baseball statistics to illustrate my prob
I ended up generating a uniform distribution to solve this problem.
I had to address the same underlying problem today. I create one plot, jittering the points, and then I create a second plot that essentially zooms in on a subsection of the first. It's dissonant and distracting if the points move around.
Following is a demo of the problem and my solution. I don't use ggplot for this plot, but the same concept applies. I make a uniform distribution, one value for each value I need to jitter. I add it to the source dataframe so that each time I take a subset, the jitter value corresponds to the same original data value.
data(airquality)
someDataset= airquality
someDataset$color="black"
someDataset$color[someDataset$Month==8 & someDataset$Wind==9.7]="red"
## jitter gives different results each time it's run
for (fZoom in c(TRUE, FALSE)){
if (fZoom) myAirQuality = someDataset[someDataset $Wind >7.5 & someDataset $Wind < 11.5,]
else myAirQuality = someDataset[someDataset $Wind >8.5 & someDataset $Wind < 10.5,]
quartz("Using Jitter")
plot(myAirQuality $Wind ~ jitter(myAirQuality $Month), col= myAirQuality$color)
}
someDataset$MonthJit=runif(nrow(someDataset), min=-0.2, max=0.2)
for (fZoom in c(TRUE, FALSE)){
if (fZoom) myAirQuality = someDataset[someDataset $Wind >7.5 & someDataset $Wind < 11.5,]
else myAirQuality = someDataset[someDataset $Wind >8.5 & someDataset $Wind < 10.5,]
quartz("Using runif")
plot(myAirQuality $Wind ~ c(myAirQuality $Month + myAirQuality $MonthJit), col= myAirQuality$color)
}