View source: R/hist_var_plot.r
hist_var_plot | R Documentation |
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.
hist_var_plot(data, bins = 5, fill = "#5e999b", color = "white",
density = TRUE, time = NULL, key = NULL)
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 |
A gg
object that can be further customized using standard ggplot2 elements (e.g., + theme_minimal(), + ylimit(0,10), ...).
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.