# sjp.xtab {sjPlot}

This document shows examples for using the sjp.xtab 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

library(sjPlot)
library(sjmisc)
data(efc)
sjp.setTheme("539",
axis.title.size = .85,
axis.textsize = .85,
legend.size = .8,
geom.label.size = 3,
geom.label.color = "black")

## Plotting proportional (contingency) tables

First, the basic plotting type is described. You can change the plot type with the type argument. Either bar or line charts can be plotted. With the show.summary argument you can add an annotation with contigency table statistics (N, Chi-square, df, Phi or Cramer’s V and p-value).

## Basic contigency table

The simplest function call is just to use two vectors / variables as argument, just like for a simple cross tabulation. By default, count and percentage values are automatically plotted above the bars.

The value labels of the count variable are plotted on the x-axis, the value labels of the grouping variable are used as legend labels. If column percenatges are plotted (default), a “total” bar is added to each category if you plot . You can suppress this bar using show.total = FALSE.

sjp.xtab(efc$e42dep, efc$e16sex)

By default, “column” percentages of each category are plotted, i.e. all bars belonging to the grouping variable (red bars in the above example) sum up to 100 percent. You can also plot row or cell percentages, using the margin argument:

sjp.xtab(efc$e42dep, efc$e16sex, margin = "row")

sjp.xtab(efc$e42dep, efc$e16sex, margin = "cell")

## Comparison to sjp.grpfrq function

As you may have noticed, the sjp.xtab function is similar to the sjp.grpfrq function. The main difference between those two functions is the y scale. While sjp.grpfrq adjusts the y scale to the count values, sjp.xtab has a percentage formatted scale. See comparison of following two figures:

sjp.xtab(efc$e42dep, efc$e16sex, margin = "row")

sjp.grpfrq(efc$e42dep, efc$e16sex)

## Using stacked bars

With the bar.pos argument, bars can be stacked.

sjp.xtab(efc$e42dep, efc$e16sex, bar.pos = "stack")

sjp.xtab(efc$e42dep, efc$e16sex,
margin = "row",
bar.pos = "stack")

sjp.xtab(efc$e42dep, efc$e16sex,
margin = "cell",
bar.pos = "stack")

## Contigency table with automatic title

The plot title can also be automatically be set if the variable has label attributes. If the title argument is set to NULL, variable labels will be used as plot title. The title then combines both the variable labels of the count and grouping variable. Use title="" to remove title (default behaviour).

sjp.xtab(efc$e42dep, efc$e16sex, title = NULL)

## Vertical value labels

With sjp.setTheme you can change the angle of value labels. Use the hjust and vjust arguments to properly position the value labels related to the bar geoms. Furthermore, using y.offset allows you to put a small margin between bars and labels.

# example with vertical labels
data(efc)
sjp.setTheme(geom.label.angle = 90,
geom.label.size = 2.5,
axis.textsize = .8)
sjp.xtab(efc$e42dep, efc$e16sex,
vjust = "center",
hjust = "bottom",
y.offset = .005)

## Line plot

And finally an example for a line plot.

sjp.xtab(efc$e42dep, efc$c172code,
show.values = FALSE,
type = "line")