This function creates a histogram of the provided data and overlays it with a normal distribution curve.
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,
...
)A histogram plot is created and the function returns this as a recordedplot.
A numeric vector of data values to be plotted.
A character string specifying the title of the histogram. Default is "Histogram with Normal Curve".
A character string specifying the label for the x-axis. Default is the name of the data variable.
A logical value indicating whether to plot a probability or frequency histogram. Default is TRUE.
A character string specifying the fill color of the histogram bars. Default is "white".
A character string specifying the color of the histogram bar borders. Default is "black".
A character string specifying the color of the normal curve line. Default is "red".
A logical value default FALSE, if TRUE a png file is saved under the name of the data of under the specified file name.
Logical. If TRUE, opens generated png files.
Character string specifying the name of the output file (without extension). Default is the name of the vector or dataframe followed by "_histogram.png".
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.
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.
Numeric, png figure width default 8 inch.
Numeric, png figure height default 7 inch.
Character string, png figure units default "in" = inch, other options are: "px" = Pixels, "cm" centimeters, "mm" millimeters.
Numeric, png figure resolution default 300 dpi.
Additional arguments to be passed to the hist function.
Sander H. van Delden plantmind@proton.me
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.
# Example usage:
set.seed(123)
sample_data <- rnorm(100)
f_hist(sample_data)
Run the code above in your browser using DataLab