#' Histogram of distribution of digits
#'
#' The 'hist_digits()' function generates a histogram that visualizes the
#' distribution of the digits.
#'
#' @param data A data frame
#' @param variable A numeric variable that includes decimals.
#' @param group ~ A second variable used to group the primary variable such
#' that histograms are generated separately for each group.
#' @param decimal_place The decimal place for which to generate the histogram.
#' The default is set to one but any place may be specified by numeric rank,
#' i.e. "1" for the 1st decimal (tenths), "2" for the 2nd decimal (hundreds), etc.
#'
#' @return A plot
#' @export
#'
#' @examples
#'hist_digits(simulated_normal, obs, ~group)
#'
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
hist_digits <- function(data, variable, group = NULL, decimal_place = 1) {
data %>%
dplyr::mutate(var = trunc({{ variable }} * 10^{{ decimal_place }}),
digits = str_sub(.data$var, start = - {{ decimal_place }})) %>%
ggplot2::ggplot(ggplot2::aes(.data$digits)) +
ggplot2::geom_bar() +
ggplot2::ggtitle("Distribution of Digits") +
ggplot2::xlab(NULL) +
ggplot2::facet_wrap({{ group }}) +
ggplot2::theme_minimal() +
ggplot2::theme(panel.grid.major.x = ggplot2::element_blank())
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.