GG_denhist: Histogram with an empirical density curve

View source: R/plotting.R

GG_denhistR Documentation

Histogram with an empirical density curve

Description

Plots a histogram with an empirical density curve and a vertical line at desired central tendency measure.

Usage

GG_denhist(
  data,
  var = NULL,
  group = NULL,
  vline = mean,
  histogram_pars = NULL,
  clean_name = T,
  no_y_axis_values = T,
  alpha = 0.2,
  auto_fraction_bounary = T,
  ...
)

Arguments

data

(data.frame or something coercible into) A data frame or a vector.

var

(chr sclr) The name of the variable to use. Not needed if data is a vector. Not needed if data has 1 column.

group

(chr sclr) The name of the grouping variable to use.

vline

(chr sclr) Whether and how to plot vertical line(s) at some point(s. Set to NULL for none. Default is mean. Can also be a custom function. Beware, it should ignore values.

histogram_pars

(list) Histogram pars

clean_name

(lgl) Wheter to call str_clean on the x axis label.

no_y_axis_values

(lgl) Hide numbers on the Y axis?

alpha

Alpha for density fit.

...

Any other params we can try to match, mainly for backwards compatibility with geom_histogram pars

Details

Automatically rescales the histogram and density fit so the heights match. Applies theme_bw.

Value

A ggplot2 object.

Examples

GG_denhist(iris, "Sepal.Length") #plot overall distribution
GG_denhist(iris, "Sepal.Length", vline = median) #use another central tendency
GG_denhist(iris, "Sepal.Length", group = "Species") #plot by group
#also accepts vectors
iris$Sepal.Length %>% GG_denhist
#also accepts 1-column data.frames, but throws a warning
GG_denhist(iris[1])
#warns you if some groups have no data
data.frame(x = c(1, 2, NA), y = c(1, 2, 3)) %>% GG_denhist("x", "y")
#warns you if grouping variable has missing data
data.frame(x = c(1, 2, 3), y = c(1, 2, NA)) %>% GG_denhist("x", "y")
#autodetect fractional data
runif(1000) %>% GG_denhist()

Deleetdk/kirkegaard documentation built on May 8, 2024, 12:27 a.m.