A function to request a frequency plot on a call to proc_freq. The function allows you to specify the type of frequency plot to produce, and various layout options. It supports bar charts and dot plots for one and two-way analysis. It also supports vertical or horizontal orientation, by variables, and various scale options.

freqplot(
  type = "barchart",
  orient = "vertical",
  scale = "freq",
  twoway = "groupvertical",
  groupby = "column",
  npanelpos = 4
)

Arguments

type

A string indicating the type of plot to create. Valid values are "barchart" or "dotplot". Default is "barchart".

orient

The orientation of the plot. Valid values are "vertical" or "horizontal". Default is "vertical".

scale

The scale to use for the plot. Valid values are "freq", "log", "percent", or "sqrt". Default is "freq". For two-way tables, the value "grouppercent" is also valid.

twoway

Options for two-way layouts. Valid values are "cluster", "grouphorizontal", "groupvertical", or "stacked". Default is "groupvertical".

groupby

The variable configuration for two-way charts. Valid values are "column" or "row". Default is "column", which means the first variable in the interaction will be used for the "column" variable, and the second variable for the "rows" on the panel. The "row" option effectively reverses the variable configuration.

npanelpos

The number of charts per panel. Default is 4. This parameter applies to two-way tables only.

Value

The frequency plot object. This object is then passed to proc_freq for evaluation and rendering. Data for the frequency plot comes directly from the proc_freq reporting data frames.

Details

The freqplot function can be passed to the plots parameter on proc_freq to give you some control over what kind of frequency plot is produced. Generally, one call to freqplot produces one frequency plot. The exception is on two-way interactions when there are more interactions that can fit on a single panel. In this case, the function will produce multiple panels so that frequencies for all interactions are displayed. The number of plots on a panel can be controlled using the npanelpos parameter.

The default plot is a simple bar chart showing frequency counts for each category. The X axis will show the categories, and the Y axis will show the frequency counts. You may change the orientation of the chart using the orient parameter. You may also change the Y scale using the scale parameter. Options include a percentage, logarithmic, or square root scale.

There are more options for two-way interactions. For two-way interactions, you may choose to display the data as stacked bar charts or clustered bar charts. To create stacked or clustered bar charts, set the type parameter to "barchart", and then specify the desired layout with the twoway parameter.

The "grouphorizontal" and "groupvertical" options on the twoway parameter apply to both bar charts and dot plots. These keywords control whether you want the frequency groups displayed horizontally across the panel, or vertically from top to bottom. The default is to display groups vertically.

The layout for two-way charts can be further manipulated with the groupby parameter. Normally for two-way charts the first variable is the X axis "columns" and the second variable is the grouping variable "rows". Passing a value of "rows" to the groupby parameter essentially flips this orientation, so that the first variable becomes the rows and the second variable becomes the columns. Note that these types of charts can be manipulated further with the orient parameter.

By combining the above options, you can produce many styles of frequency plots.

Any requested plots will be displayed on the interactive report only. Plots are created as jpeg files, and stored in a temp directory. Those temporary files are then referenced by the interactive report to display the graphic.

If desired, you may output the report objects and pass to proc_print. To do this, set output = report on the call to proc_freq, and pass the resulting list to proc_print.

See also

Examples

library(procs)

# Turn off printing for CRAN checks
# Set to TRUE to run in local environment
options("procs.print" = FALSE)

# Prepare sample data
dt <- as.data.frame(HairEyeColor, stringsAsFactors = FALSE)

# Example 1: Single plot request for all tables
res <- proc_freq(dt, tables = v(Hair, Eye, Hair * Eye),
                 weight = Freq,
                 plots = freqplot(type = "dotplot"),
                 titles = "Hair and Eye Frequency Statistics")

# View results
res

# Example 2: Separate plots request for each table
res <- proc_freq(dt, tables = v(Hair, Eye, Hair * Eye),
                 weight = Freq,
                 plots = list(freqplot(type = "barchart",
                                       orient = "horizontal"),
                              freqplot(type = "dotplot",
                                       scale = "percent"),
                              freqplot(type = "barchart",
                                       twoway = "cluster")),
                 titles = "Hair and Eye Frequency Statistics")

# View results
res

# Example 3: Display options for group orientation
res <- proc_freq(dt, tables = v(Hair * Eye, Eye * Hair),
                 weight = Freq,
                 plots = list(freqplot(type = "dotplot",
                                       twoway = "grouphorizontal"),
                              freqplot(type = "dotplot",
                                       twoway = "groupvertical")),
                 titles = "Hair and Eye Frequency Statistics")

# View results
res