This is a wrapper to the typical ggplot based density plot, i.e., using geom_density. A continuous variable, x, is required as an input. Optionally, a by categorical variable can be provided.

densityplot(
  x,
  data,
  by = NULL,
  position = "stack",
  facet = NULL,
  facet_type = "wrap",
  alpha = 1,
  xlab = NULL,
  ylab = NULL
)

Arguments

x

The numeric variable that is to be density plotted.

data

A data frame with at least one numeric variable (the x variable).

by

A categorical variable by which to group the x values. If provided there will be one density plot for each set of x values grouped by the values of the by variable.

position

If the by variable is provided, there are three ways these multiple density plots can be positioned: stacked (position = 'stack'), superimposed (position = identity').

facet

A character string or character vector. If provided, we facet_wrap (by default) the histogram by the variables. This is equivalent to the facet_wrap(variables) in ggplot2.

facet_type

By default, this takes the value of wrap, and facet leads to a facet wrap. If facet_type is grid, then facet gives us a facet_grid.

alpha

The transparency to for the filled histogram bars. This is probably only required when using position = 'identity'.

xlab

The label of the x-axis (defaults to the x variable name).

ylab

The label of the y-axis (defaults to the y variable name).

Value

A ggplot2::ggplot object, which may be modified with further ggplot2 commands.

Examples

densityplot(x = age, data = schizophrenia, by = gender)