Open In App

Plot Z-Score in R

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Z-scores are utilized to quantify the relationship of a value to the mean of a data set. Plots of z-scores within R visualize how values are removed from the mean. This article is a guide for calculating and plotting z-scores in R.
A z-score is the number of standard deviations a value is away from the mean. The z-score formula is:

Z = \frac{(X - \mu)}{\sigma}

Where:

  • X = individual value
  • μ = mean of the dataset
  • σ = standard deviation

A z-score of 1 indicates that the value is one standard deviation above the mean, and -1 indicates that it is one standard deviation below the mean.

Method 1: Naive Approach

This method consists of calculating the z-score by hand and plotting it.

Steps:

  • Make a vector of data values.
  • Compute the mean and standard deviation.
  • Compute the z-score for each point.

Example 1: 

R
a <- c(9, 10, 12, 14, 5, 8, 9)

mean(a)

# find standard deviation
sd(a)

# calculate z
a.z <- (a - mean(a)) / sd(a)

plot(a.z, type="o", col="green")

 Output:

Example 2:

R
a <- c(7, 9, 2, 4, 25, 18, 19)

mean(a)

# find standard deviation
sd(a)

# calculate z-score
a.z <- (a - mean(a)) / sd(a)

plot(a.z, type="o", col="green")

 Output:

Method 2: Using qnorm()

If provided with a p-value, the Z-Score can be determined using R's qnorm() function. The p-value is the probability that a value lies below some value.

Syntax: 

qnorm(p-value)

Approach:

  • Call qnorm() function with required p-value
  • Plot z-score with the value so obtained

Example :

R
set <- qnorm(0.75)

plot(set, type="o", col="green")

Output:

graph4

Similar Reads