0

I am trying to build a custom function to add points to a plot window. My problem is that when I use jitter it seems that the spread is different in the right vs left part of the plot.

Why is this ?

set.seed(8)

f1 <- function(x, y, col = 'black', xjit = 5, ...) {
  points(x = jitter(rep(x, length(y)), xjit), y = y, col = col, ...)
}

v <- sample(1:1000, 1000, T) #Some data to plot
ymax <- max(v)

plot.new() #Build a canvas from scratch
plot.window( xlim = c(1, 10), ylim = c(0, ymax) )
ticks <- seq(1, 10, 1)
axis(1, at=ticks, labels=NA)
axis(2)

#Plot the same data to the left (x=1) and right (x=10)
f1(1, v, 'red', pch = 4)
f1(10, v, 'blue', pch = 4)

figure

user3375672
  • 3,728
  • 9
  • 41
  • 70
  • 2
    See `?jitter`. The second argument is `factor` which is proportional to the original x values (either 1 or 10). Try specifying the argument name explicitly: `jitter(..., amount = xjit)`. –  Oct 31 '17 at 11:15
  • A what! I did read the documentation to `jitter` but apparently not careful enough. I love to learn ! – user3375672 Oct 31 '17 at 11:28

1 Answers1

0

Jitter is a function of the input variable so just add the jitter results to the original number after it is calculated using the same first variable in you f1 function.

Scott
  • 1