create_density_plot: Function creates a density plot.

View source: R/create_density_plot.R

create_density_plotR Documentation

Function creates a density plot.

Description

Function creates a ggplot2 based density plot with options for scaling, shading probability areas, and plotting observation locations. The function's density arguments mirror most of the arguments available from density for the Kernel Density Estimation (KDE). See the density help page for more information.

Usage

create_density_plot(
  df,
  aes_x = NULL,
  aes_color = NULL,
  aes_fill = NULL,
  bw = "nrd0",
  adjust = 1,
  kernel = "gaussian",
  n = 512,
  position = "identity",
  title = NULL,
  subtitle = NULL,
  caption = NULL,
  center_titles = FALSE,
  x_title = NULL,
  y_title = NULL,
  hide_x_tics = FALSE,
  hide_y_tics = FALSE,
  rot_x_tic_angle = 0,
  rot_y_tic_label = FALSE,
  density_size = 1,
  density_color = "black",
  density_fill = "gray",
  density_alpha = 0.4,
  x_limits = NULL,
  x_major_breaks = waiver(),
  x_minor_breaks = waiver(),
  x_labels = waiver(),
  y_limits = NULL,
  y_major_breaks = waiver(),
  y_minor_breaks = waiver(),
  y_labels = waiver(),
  axis_text_size = 11,
  show_major_grids = TRUE,
  show_minor_grids = TRUE,
  plot_obs = FALSE,
  plot_obs_len = 0.02,
  plot_obs_color = "black",
  plot_obs_jitter = FALSE,
  panel_color = "white",
  panel_border_color = "black",
  show_legend = TRUE,
  legend_pos = "right",
  legend_key_width = 0.7,
  legend_key_height = 0.7,
  legend_key_backgrd = "white",
  silent_NA_warning = FALSE,
  cum_prob = NULL,
  area_colors = c("gray", "green", "gray"),
  area_quantile_line_color = "red",
  png_file_path = NULL,
  png_width_height = c(480, 480)
)

Arguments

df

A data frame that contains a numeric vector from which to estimate the KDE along with possible variables for setting aes_color and aes_fill (see below).

aes_x

The variable from df from which to estimate the KDE. This argument is required.

aes_color

Sets the variable name from df for the aesthetic mapping for color.

aes_fill

Sets the variable name from df for the aesthetic mapping for fill.

bw

A string or numeric that sets the smoothing bandwidth to be used with the KDE function.

adjust

A numeric that adjusts bw since the actual bandwidth is computed as adjust*bw.

kernel

A string that set the type of Kernel Density Estimation (KDE). Acceptable values are "gaussian", "rectangular", "triangular", "epanechnikov", "biweight", "cosine" or "optcosine".

n

The number of equally spaced points at which the density is to be estimated. This should be a power of two.

position

A string that sets the position. Acceptable values are identity which overlays or stack which stacks.

title

A string that sets the overall title.

subtitle

A string that sets the overall subtitle.

caption

A string that sets the plot caption

center_titles

A logical which if TRUE centers both the title and subtitle.

x_title

A string that sets the x axis title. If NULL (the default) then the x axis title does not appear.

y_title

A string that sets the y axis title. If NULL (the default) then the y axis title does not appear.

hide_x_tics

A logical that controls the appearance of the x axis tics.

hide_y_tics

A logical that controls the appearance of the y axis tics.

rot_x_tic_angle

A numeric that sets the angle of rotation for the x tic labels. When x tic labels are long, a value of 40 for this argument usually works well.

rot_y_tic_label

A logical which if TRUE rotates the y tic labels 90 degrees for enhanced readability.

density_size

A numeric that sets the width attribute for the density line .

density_color

A string that sets the color attribute for the density line .

density_fill

A string that sets the fill color attribute for the area under the density line.

density_alpha

A numeric that sets the alpha attribute value for density_fill.

x_limits

A numeric 2 element vector or function that sets the minimum and maximum for the x axis. Use NA to refer to the existing minimum and maximum.

x_major_breaks

A numeric vector or function that sets the major tic locations along the x axis.

x_minor_breaks

A numeric vector or function that sets the minor tic locations along the x axis.

x_labels

A character vector or function giving x axis tic labels. Must be the same length as x_breaks.

y_limits

A numeric 2 element vector or function that sets the minimum and maximum for the y axis. Use NA to refer to the existing minimum and maximum.

y_major_breaks

A numeric vector or function that sets the major tic locations along the y axis.

y_minor_breaks

A numeric vector or function that sets the minor tic locations along the y axis.

y_labels

A character vector or function giving y axis tic labels. Must be the same length as y_breaks.

axis_text_size

A numeric that sets the font size along the axis'. Default is 11.

show_major_grids

A logical that controls the appearance of major grids.

show_minor_grids

A logical that controls the appearance of minor grids.

plot_obs

A logical which if TRUE plots a line for each observation along the axis margin.

plot_obs_len

A numeric that sets the length of the plot_obs lines.

plot_obs_color

A string that sets the color of the plot_obs lines.

plot_obs_jitter

A logical which if TRUE will add a slight horizontal adjustment to overlapping observations.

panel_color

A string in hexidecimal or color name that sets the plot panel's color. The default is "white".

panel_border_color

A string in hexidecimal or color name that sets the plot panel's border color. The default is "black".

show_legend

A logical that controls the appearance of the legend.

legend_pos

A string that sets the legend position. Acceptable values are "top", "bottom", "left", "right".

legend_key_width

A numeric that sets the legend width in cm.

legend_key_height

A numeric that sets the legend height in cm.

legend_key_backgrd

A string that sets the legend's background color.

silent_NA_warning

A logical that controls the appearance of a console warning when Na's are removed.

cum_prob

A one or two element numeric vector for defining cumulative probabilities which divide the density plot area. The values are probabilities with values within 0.0 to 1.0. If for example with a cumulative probability cum_prob of .95, then the density is divided into two probability areas of 95% and 5% with the dividing quantile value labeled. Similarly if two cumulative probabilities are submitted, then three areas are defined.

area_colors

A string vector that sets the color of each area defined by cum_prob.

area_quantile_line_color

A string that sets the vertical line color at quantile locations that divide the areas defined by cum_prob.

png_file_path

A character string with the directory and file name to produce a png image of the plot.

png_width_height

A numeric vector that sets the width and height of the png image in pixels. The default is c(480,480). There are 37.8 pixels in a centimeter.

Value

A ggplot2 plot object.

Author(s)

Rick Dean


deandevl/RplotterPkg documentation built on Feb. 1, 2024, 8:02 p.m.