hist_var_plot: Create histograms for several Likert-type variables

View source: R/hist_var_plot.r

hist_var_plotR Documentation

Create histograms for several Likert-type variables

Description

This functions computes a facetted plot of variable distributions using the function ggplot. The first layer is a histogram and the second a normal distributed density curve based on the mean and standard deviation of the respective variable. Both a data frame containing several variables or a single vector can be passed to the function.

Usage

hist_var_plot(data, bins = 5, fill = "#5e999b", color = "white",
  density = TRUE, time = NULL, key = NULL)

Arguments

data

An object of one of the following classes: "numeric", "double", "integer", "data.frame", "tbl", or "tbl_df".

bins

Number of values that should be plotted on the x-axis (should equal the number of answer options or a reasonable scale).

fill

Color of the bins (defaults to green).

color

Color of the bin borders (defaults to white).

density

A logical value indicating whether a normally distributed density curve should be plotted on top of the histogram. The density curve takes the mean and sd of the respective variables as arguments. In this case, the histograms are computed using aes(y = stat(density).

Value

A gg object that can be further customized using standard ggplot2 elements (e.g., + theme_minimal(), + ylimit(0,10), ...).

Examples

d <- data.frame(x = rnorm(200, 3, 1),
                y = rnorm(200, 0, 1))

# Several variables
hist_var_plot(d, density = F, bins = 10) 
hist_var_plot(d, bins = 10)

# One variable
hist_var_plot(d$x, fill = "lightblue", bins = 10) +
   theme_minimal()

masurp/pmmisc documentation built on May 3, 2024, 7:13 p.m.