jitter Function In R: The Usage for Scatterplots

We will go through when and how to utilize the jitter function for Scatterplots in the R programming language. So, follow this article to understand this function.

What is the jitter Function in R?

A scatterplot is a visualization plot that places two variables on the x- and y-axes to represent values for a data set using cartesian coordinates. Finding trends in data and understanding the relationships between variables are made much easier by doing this. 

However, because the points are clustered and indistinguishable on the scatter plot, it isn’t easy to visualize the link between a continuous variable and an almost continuous variable.

Now, you can see the syntax below.

jitter Function in R: The Syntax

jitter(data , amount )

Parameters

  • data: decides which input vector will receive the noise addition.
  • amount: decides how much noise should be added to the input vector.

How to use the jitter function in R?

First, we will create data as follows to use the jitter function in R.

# Create a vector
set.seed(100)
w <- sample(1:15, 300, TRUE)
h <- 3 * w + rnorm(300)

# Create a data frame
data <- data.frame(weight = w, height = h)

# View head a data frame
head(data)

Output

  weight   height
1     10 30.73549
2      7 21.02687
3      6 20.44709
4      3 11.65302
5      9 27.03989
6     10 31.35091

Then, we use the plot() function to plot this data

# Create a vector
set.seed(100)
w <- sample(1:15, 300, TRUE)
h <- 3 * w + rnorm(300)

# Create a data frame
data <- data.frame(weight = w, height = h)

# Plot
plot(data$weight, data$height,
    pch = 16, col = "steelblue"
)

Output

The quantity of noise added to the data frame also significantly impacts how the data is shown. The integrity of the dataset is impacted if we add a significant amount of noise. 

The jitter() function’s noise addition is only helpful for visualizing data. If noise is added, the statistical calculation will be impacted, and the dataset will become untrustworthy.

jitter function in R: The Basic Application

So, in this case, we have added a ton of noise, which has rendered the plot illogical and random. Check out the code example.

# Create a vector
set.seed(100)
w <- sample(1:15, 300, TRUE)
h <- 3 * w + rnorm(300)

# Create a data frame
data <- data.frame(weight = w, height = h)

# View head a data frame
head(data)

# Using the jitter function
# add noise to the dataframe.
data$weight <- jitter(data$weight, 20)

# Plot
plot(data$weight, data$height,
    pch = 16, col = "steelblue"
)

Output

Summary

In conclusion, we will learn when and how to use the jitter function in R. So, we hope you will know how to use it after reading this article. And if you have any questions, don’t hesitate to comment below.

Have a great day!

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *