sjp.frq {sjPlot}

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


(back to table of content)

Data initialization

Please refer to this document.

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

Customizing plot appearance

Please refer to this document

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).


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.

        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)
        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)
        type = "dot",      # dot plot
        coord.flip = TRUE, # flip coordinates
        show.n = FALSE,    # no counts, only percentages
        geom.size = 2.5)   # adjust geom size

Adding confidence intervals

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

        sort.frq = "desc", # sort frequencies = TRUE,    # show confidence intervals
        type = "dot" ,     # dot plot
        coord.flip = TRUE, # flip coordinates
        show.n = FALSE,    # no counts, only percentages
        geom.size = 2.5)   # adjust geom size

Plotting histograms

Histrograms can be plotted using the type argument.

Frequency plot with count data

If you have variables with count or numerical variables with many categories, you can still plot it as normal frequency plot. However, you have to take into account that the bars and axis labels may be very narrow, thus adjusting the figure width is recommended:

sjp.frq(efc$e17age, show.values = FALSE)

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 argument to specify at which amount of unique values a variable is automatically grouped.

sjp.frq(efc$e17age, = 15)

Note that = 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 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.

        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.

        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":

        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:

        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.

        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.

        type = "hist", 
        geom.size = 1, 
        geom.colors = "grey30", 
        normal.curve = TRUE, 
        normal.curve.color = "white")


As comparison, the original qplot-histogram is shown in the following figure:


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.

        type = "box", 
        geom.size = .3, = 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.

        type = "violin", 
        geom.colors = "grey70", = .2)