f_hist: Plot a Histogram with an Overlaid Normal Curve

View source: R/fhist.R

f_histR Documentation

Plot a Histogram with an Overlaid Normal Curve

Description

This function creates a histogram of the provided data and overlays it with a normal distribution curve.

Usage

f_hist(
  data,
  main = NULL,
  xlab = NULL,
  probability = TRUE,
  col = "white",
  border = "black",
  line_col = "red",
  save_png = FALSE,
  open_png = TRUE,
  output_file = NULL,
  output_dir = NULL,
  save_in_wdir = FALSE,
  width = 8,
  height = 7,
  units = "in",
  res = 300,
  ...
)

Arguments

data

A numeric vector of data values to be plotted.

main

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

xlab

A character string specifying the label for the x-axis. Default is the name of the data variable.

probability

A logical value indicating whether to plot a probability or frequency histogram. Default is TRUE.

col

A character string specifying the fill color of the histogram bars. Default is "white".

border

A character string specifying the color of the histogram bar borders. Default is "black".

line_col

A character string specifying the color of the normal curve line. Default is "red".

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

Character string, png figure units default "in" = inch, other options are: "px" = Pixels, "cm" centimeters, "mm" millimeters.

res

Numeric, png figure resolution default 300 dpi.

...

Additional arguments to be passed to the hist function.

Details

The function first captures the name of the input variable for labeling purposes. It then calculates a sequence of x-values and corresponding y-values for a normal distribution based on the mean and standard deviation of the data. The histogram is plotted with specified aesthetics, and a normal curve is overlaid. To increase resolution you can use png(...,res = 600) or the 'RStudio' chunk setting, e.g. dpi=600.

Value

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

Author(s)

Sander H. van Delden plantmind@proton.me

See Also

hist, dnorm, lines

Examples

# Example usage:
set.seed(123)
sample_data <- rnorm(100)
f_hist(sample_data)



rfriend documentation built on Aug. 8, 2025, 7:33 p.m.

Related to f_hist in rfriend...