# sjp.frq {sjPlot}

This document shows examples for using the sjp.frq function of the sjPlot package.

Ressources:

• Developer snapshot at GitHub
• Submission of bug reports and issues at GitHub

(back to table of content)

## Data initialization

# load package
library(sjPlot)
library(sjmisc)
data(efc)
# set theme
sjp.setTheme(theme = "539",
geom.label.size = 3.5,
axis.textsize = .8,
axis.title.size = .9)

## Plotting simple frequencies

First, the basic plotting type is described. You can change the plot type with the type argument.

### Basic frequency plot

The simplest function call is just to use a vector / variable as argument. The intention is to plot distributions of categorial variables (factors). By default, count and percentage values are automatically plotted. Note that value and variable labels have already been attached (see paragraph Data initialization).

sjp.frq(efc$e42dep) ### Frequency plot with automatic title The plot title can also be automatically be extracted if variable labels are attached to the variable (see paragraph Data initialization, function set_label from the sjmisc-package). If the title argument is set to NULL, variable labels will be used as plot title. sjp.frq(efc$e42dep,
geom.colors = "darkslategray4",
title = NULL)

### Frequency plot with flipped coordinates

The plot’s axes can be flipped using coord.flip = TRUE. If needed, labels can be placed inside the bars with vjust and hjust arguments. In such cases, you might need to adjust the label colors with geom.label.color = "white".

sjp.setTheme(theme = "scatter",
geom.label.size = 3.5,
geom.label.color = "white",
axis.textsize = .8,
axis.title.size = .9)
sjp.frq(efc$e42dep, coord.flip = TRUE, hjust = "top") ### Dot plots Use the type argument for dot plots. You may have to adjust the geom.size, so dots are a bit bigger and better to see. sjp.setTheme(theme = "forestgrey", geom.label.size = 3, geom.label.color = "grey30", axis.textsize = .8, axis.title.size = .9) sjp.frq(efc$e15relat,
type = "dot",      # dot plot
coord.flip = TRUE, # flip coordinates
show.n = FALSE,    # no counts, only percentages
geom.size = 2.5)   # adjust geom size

Bar charts and dot plots may also have confidence intervals. Use the showCI argument to add confidence intervals to dot plots or bar charts.

### Frequency plot with grouped count data, automatic grouping

The sjp.frq function offers to automatically group variables with many categories in order to have clear plots. Use the auto.group argument to specify at which amount of unique values a variable is automatically grouped.

sjp.frq(efc$e17age, auto.group = 15) Note that auto.group = 15 does not produce exactly 15 groups, but not more than 15 groups. The amount of groups is calculated by dividing the range of the variable by auto.group value and round up to the next integer. In the above case: 38/15 = 2.53, which means a group size of 3. See group_var from the sjmisc-package for more details. ### Basic histogram Using the default plot type (as shown in the examples above), variables are visually treated as categorial. If you want to have a plot in histogram style, use type = "hist". The following example plots a histogram with an overlay of the mean value and standard deviation of the plotted variable. Note that the geom.size argument needs to be changed. sjp.frq(efc$e17age,
type = "hist",
geom.size = 1,
show.mean = TRUE)

Value labels can be suppressed with show.values = FALSE to avoid overlapping labels, or resized using the geom.label.size argument from the sjp.setTheme function.

sjp.frq(efc$e17age, type = "hist", show.mean = TRUE, show.values = FALSE, geom.colors = "#336699") ### Line-styled histogram Instead of plotting bars, you can also plot a line curve with type = "lines": sjp.frq(efc$e17age,
show.values = FALSE,
type = "line")

### Density curves

To plot a density curve, use type = "dens". To describe the skewness of a variable, you can plot an overlay with a normal curve and compare it with the density curve:

sjp.frq(efc$e17age, type = "dens", normal.curve = TRUE, normal.curve.alpha = .3) ### Basic histogram in qplot-style If you prefer the histogram style of the quick plot function of ggplot (qplot), adjust the geom.size argument. sjp.frq(efc$e17age,
type = "hist",
geom.size = 3)

As you can see, the function suggests adjusting the bar width, which is by default 0.6. The ggplot-default value for histograms is 1/30 of the value range. This is shown in the next example.

### Basic histogram in qplot-style with adjusted bar width

Here we have a histogram in qplot-style with adjusted bar width and a normal curve as overlay.

## Box and violin plots

Count variables may also be plotted as box or violin plots. Use argument type = "box" to plot a box plot. Beside the median, the mean value of a variable is plotted as small circle inside the box plot.

sjp.frq(efc$barthtot, type = "box", geom.size = .3, inner.box.width = 5) With type = "violin" you can plot a violin plot. This plot shows a (mirrored) vertical density curve of the variable with a box plot inside of the violin plot. sjp.frq(efc$c160age,
type = "violin",
geom.colors = "grey70",
inner.box.width = .2)