Learn R Programming

rfriend (version 1.0.0)

f_qqnorm: Normal Q-Q Plot with Confidence Bands

Description

This function creates a normal Q-Q plot for a given numeric vector and adds confidence bands to visualize the variability of the quantiles.

Usage

f_qqnorm(
  x,
  main = NULL,
  ylab = NULL,
  conf_level = 0.95,
  col = NULL,
  pch = NULL,
  cex = NULL,
  save_png = FALSE,
  open_png = TRUE,
  output_file = NULL,
  output_dir = NULL,
  save_in_wdir = FALSE,
  width = 8,
  height = 7,
  units = "in",
  res = 300,
  ...
)

Value

A Q-Q plot is created and the function returns this as a recordedplot.

Arguments

x

A numeric vector of data values.

main

A character string specifying the title of the histogram. Default is "Histogram with Normal Curve".

ylab

A character string specifying the y-axsis label. Default name is "Quantiles of: data_name".

conf_level

Numeric, between 0 and 1. Confidence level for the confidence bands. Default is 0.95 (95% confidence).

col

Numeric, optional parameter for color of point with default 'black'.

pch

Numeric, optional parameter shape of points default pch = 19.

cex

Numeric, optional parameter for graph cex with default cex = 0.6.

save_png

A logical value default FALSE, if TRUE a png file is saved under the name of the data of under the specified file name.

open_png

Logical. If TRUE, opens generated png files.

output_file

Character string specifying the name of the output file (without extension). Default is the name of the vector or dataframe followed by "_histogram.png".

output_dir

Character string specifying the name of the directory of the output file. Default is tempdir(). If the output_file already contains a directory name output_dir can be omitted, if used it overwrites the dir specified in output_file.

save_in_wdir

Logical. If TRUE, saves the file in the working directory Default is FALSE, to avoid unintended changes to the global environment. If the output_dir is specified save_in_wdir is overwritten with output_dir.

width

Numeric, png figure width default 8 inch.

height

Numeric, png figure height default 7 inch.

units

Numeric, png figure units default inch.

res

Numeric, png figure resolution default 300 dpi.

...

Additional graphical parameters to be passed to the qqnorm function.

Author

Sander H. van Delden plantmind@proton.me

Details

The function calculates theoretical quantiles for a normal distribution and compares them with the sample quantiles of the input data.

It also computes confidence intervals for the order statistics using the Blom approximation and displays these intervals as shaded bands on the plot.

The reference line is fitted based on the first and third quartiles of both the sample data and theoretical quantiles.

To increase resolution you can use png(...,res = 600) or the 'RStudio' chunck setting, e.g. dpi = 600.

Examples

Run this code
# Generate random normal data
set.seed(123)
data <- rnorm(100)

# Create a Q-Q plot with confidence bands
f_qqnorm(data)

# Customize the plot with additional graphical parameters
f_qqnorm(data, conf_level = 0.99, pch = 16, col = "blue")

Run the code above in your browser using DataLab