blue-sky blue-sky - 4 months ago 33
R Question

Explanation for 'jitter' function in r

According to the documentation, the explanation for the

function is "Add a small amount of noise to a numeric vector."

What does this mean?

Is a random number associated with each number in the vector and added to it?


Jittering indeed means just adding random noise to a vector of numeric values, by default this is done in jitter-function by drawing samples from the uniform distribution. The range of values in the jittering is chosen according to the data, if amount-parameter is not provided.

I think term 'jittering' covers other distributions than uniform, and it is typically used to better visualize overlapping values, such as integer covariates. This helps grasp where the density of observations is high. It is good practice to mention in the figure legend if some of the values have been jittered, even if it is obvious. Here is an example visualization with the jitter-function as well as a normal distribution jittering where I arbitrarily threw in value sd=0.1:

n <- 500
dat <- data.frame(integer = rep(1:3, each=n), continuous = c(rnorm(n, mean=1), rnorm(n, mean=2), rnorm(n, mean=3))^2)

plot(dat, main="No jitter for x-axis", xlab="Integer", ylab="Continuous")
plot(jitter(dat[,1]), dat[,2], main="Jittered x-axis (uniform distr.)", xlab="Integer", ylab="Continuous")
plot(dat[,1]+rnorm(3*n, sd=0.1), dat[,2], main="Jittered x-axis (normal distr.)", xlab="Integer", ylab="Continuous")

enter image description here