Shapiro-Wilk Normality Test in R (Example)


In this tutorial, I’ll explain how to perform a Shapiro-Wilk normality test in the R programming language.

The table of content looks as follows:

Let’s do this.


Construction of Example Data

As a first step, we need to create some data that we can use in the examples below:

set.seed(946322)                       # Set random seed
x1 <- rnorm(100)                       # Create normally distributed vector
x2 <- runif(100)                       # Create uniformly distributed vector

We can plot the exemplifying data to get a first impression of the distributions of our data by using the plot and the density functions:

plot(density(x1), ylim = c(0, 1.1), col = 2) # Draw data to density plot
lines(density(x2), col = 3)
legend("topleft", c("x1", "x2"), col = 2:3, lty = 1)


r graph figure 1 shapiro wilk normality test


As revealed in Figure 1, we created a graphic containing multiple density pots with the previous R programming syntax.

The variable x1 looks normally distributed (we’ll check if this is true later). The variable x2, however, is clearly not normally distributed.


Example: Perform Shapiro-Wilk Normality Test Using shapiro.test() Function in R

The R programming syntax below illustrates how to use the shapiro.test function to conduct a Shapiro-Wilk normality test in R.

For this, we simply have to insert the name of our vector (or data frame column) into the shapiro.test function.

Let’s check our vector x1 first:

shapiro.test(x1)                       # Apply shapiro.test function
#          Shapiro-Wilk normality test
# data:  x1
# W = 0.98862, p-value = 0.5548

Have a look at the previous RStudio console output of the shapiro.test function: As you can see, the p-value is larger than 0.05 meaning that our input data x1 is normally distributed.

Let’s do the same for our second variable x2:

shapiro.test(x2)                       # Apply shapiro.test function
#      Shapiro-Wilk normality test
# data:  x2
# W = 0.93307, p-value = 7.464e-05

This time the Shapiro-Wilk normality test is clearly significant, i.e. the vector x2 is not normally distributed.


Video, Further Resources & Summary

Do you need more info on the content of this article? Then you might watch the following video of my YouTube channel. I’m explaining the R syntax of this tutorial in the video:


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


In addition, you could have a look at the related articles which I have published on this homepage. I have released numerous posts about distributions in R already.


Summary: In this tutorial you learned how to conduct a Shapiro-Wilk normality test in the R programming language. If you have any additional questions, please let me know in the comments.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

6 Comments. Leave new

  • Fabio Venancio
    October 2, 2021 12:58 pm

    Hi. Is it possible to make the y-axis scaling custom, without following a common sequence? example: 0.0, 0.1, 0.3, 0.5 and 1.0

  • Iñaki Peeters
    March 28, 2022 10:09 am

    Hi, I was wondering if it’s possible to adjust the significance level of the Shapiro-Wilk test?
    By default in RStudio, this is set to 95%, but I would like to test for significance levels of 90% and 99% as well. Is there any way to do this?

    • Hey Iñaki,

      As far as I know, you can simply interpret the p-value output of the shapiro.wilk function in terms of significance level.

      Or am I misinterpreting your question?


  • Hi, I am trying to run this type of test but I am not fully understanding the data section.

    Where did the “set.seed” figure come from or is it completely random?
    Also the “rnorm and “runif” functions, is there a specific reason 100 is the figure selected?

    Thank you.

    • Hey Dion,

      The set.seed function is used to create a reproducible example. Have a look here for more details.

      The 100 within rnorm and runif specifies that we want to draw 100 values. You may replace this by another number.



Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.