plot_hist_with_density: Plot histogram and smooth density curve

Description Usage Arguments Value Examples

View source: R/plot_histogram.R

Description

plot_hist_with_density uses dplyr to plot a histogram of a specific numeric column for a given dataset. It also plots a smooth density curve overlayed on the histogram. The function also allows custom parameters to adjust the colours of the plot, bin size, and alpha transparency.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
plot_hist_with_density(
  data,
  column,
  title,
  xlab,
  ylab = "density",
  bins = 20,
  alpha = 0.5,
  hist_colour = "black",
  hist_fill = "white",
  density_fill = "grey",
  narm = TRUE
)

Arguments

data

Tibble. The dataset to use for plotting. It is called data because it is a dataset.

column

Variable. The variable name for the column to plot. It must exist as a column in data. It is called column because it is the column to plot.

title

String. The title of the plot. It is called title because it is the title of the plot.

xlab

String. The x-axis label for the plot. It is called xlab because it is the x-axis label for the plot.

ylab

String. The y-axis label for the plot. It is called ylab because it is the x-axis label for the plot.

bins

Integer. The number of bins to use for the histogram to plot. It is called bins because it represents the number of bins.

alpha

Float. A value in the range [0,1] that represents the alpha transparency of the smooth density curve to plot. It is called alpha because it represents the alpha transparency.

hist_colour

String. A string containing either a hex code or a named colour that indicates the colour to use for the histogram outline. It is called hist_colour because it indicates the colour of the histogram outline.

hist_fill

String. A string containing either a hex code or a named colour that indicates the colour to use for the histogram fill. It is called hist_fill because it indicates the colour of the histogram fill

density_fill

String. A string containing either a hex code or a named colour that indicates the colour to use for the density curve fill. It is called density_fill because it indicates the colour of the density curve fill

narm

Boolean. A Boolean value indicting whether or not NA values should be removed from the column to plot. It is called narm because it indicates whether to remove (rm) NA values.

Value

An image of the plot containing the histogram and density curve

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Using default parameters
(plot_hist_with_density(
  data=iris,
  column=Sepal.Length,
  xlab="Sepal Length",
  title="Histogram and density plot of sepal length"))

# Overwriting default parameters
(plot_hist_with_density(
  data=iris,
  column=Sepal.Length,
  xlab="Sepal Length",
  title="Histogram and density plot of sepal length",
  bins = 40,
  alpha = 0.3,
  hist_colour = "grey",
  hist_fill = "grey",
  density_fill = "blue"))

stat545ubc-2021/maggieliu545ba2 documentation built on Dec. 23, 2021, 5:25 a.m.