red <- apply(red, 2, jitter)
The above is my current code. After applying it to my data frame, I noticed the strength of it to be quite small.
If I try the following, it doesn't work:
red <- apply(red, 2, jitter(red, factor = 5))
I don't think you need apply()
for this at all, at least, not if I understand your intention. Here's an example using the amount
parameter and some iris
data.
From the docs, amount
is described as:
numeric; if positive, used as amount (see below), otherwise, if = 0 the default is factor * z/50.
Default (NULL): factor * d/5 where d is about the smallest difference between x values.
First I use apply()
as your example, then I increase the jitter without it.
data(iris) # data (for example)
red <- iris # keeping your data.frame name
red <- red[,!colnames(red)=="Species"]
red <- apply(red, 2, jitter)
var(red)
Sepal.Length Sepal.Width Petal.Length Petal.Width Sepal.Length 11.528025 0.1123260 2.2771584 1.085271 Sepal.Width 0.112326 9.2040367 0.8921243 0.296827 Petal.Length 2.277158 0.8921243 9.9957082 1.680199 Petal.Width 1.085271 0.2968270 1.6801995 9.194739
red <- jitter(red, amount = 5)
var(red)
Sepal.Length Sepal.Width Petal.Length Petal.Width Sepal.Length 19.5928296 1.2155940 2.0093691 0.9365071 Sepal.Width 1.2155940 16.8435802 -0.6173289 0.4445300 Petal.Length 2.0093691 -0.6173289 16.6392921 1.7726687 Petal.Width 0.9365071 0.4445300 1.7726687 18.0234312
red <- jitter(red, amount = 500)
var(red)
Sepal.Length Sepal.Width Petal.Length Petal.Width Sepal.Length 79193.216 2570.665 6473.9229 -2702.2620 Sepal.Width 2570.665 74802.846 -5463.7883 2568.4585 Petal.Length 6473.923 -5463.788 87327.8161 865.1276 Petal.Width -2702.262 2568.458 865.1276 87766.5185
Increasing factor
also does work, but it's just potentially less apparent dependent on the distribution of the data.
Yet another approach is to take the difference of your data's values with and without jitter to get the components that's pure jitter and multiple that by any number you like and add it back to the original data.
head(red)
Sepal.Length Sepal.Width Petal.Length Petal.Width 1 5.1 3.5 1.4 0.2 2 4.9 3.0 1.4 0.2 3 4.7 3.2 1.3 0.2 4 4.6 3.1 1.5 0.2 5 5.0 3.6 1.4 0.2 6 5.4 3.9 1.7 0.4
head((red + apply(red,2,jitter))*10)
Sepal.Length Sepal.Width Petal.Length Petal.Width 1 101.91095 70.00209 28.03810 3.831979 2 97.93295 60.18538 27.88743 3.948451 3 93.88483 64.09243 26.13381 3.886840 4 92.19319 61.80549 29.87822 3.818302 5 100.00335 71.95687 28.09964 3.991321 6 108.03858 78.09225 34.06902 8.037211
You could also just apply jitter to the plots you make of the data, instead of the data itself, as below:
mtcars %>%
as_tibble() %>%
select(cyl, vs) %>%
ggplot(aes(x = vs, y = cyl, fill = cyl)) +
geom_point() +
geom_jitter()
If you want the scatters to be reproducible, include set.seed(123)
in your code.