Movatterモバイル変換


[0]ホーム

URL:


Type:Package
Encoding:UTF-8
Title:Data Visualization for Statistics in Social Science
Version:2.9.0
Maintainer:Daniel Lüdecke <d.luedecke@uke.de>
Description:Collection of plotting and table output functions for data visualization. Results of various statistical analyses (that are commonly used in social sciences) can be visualized using this package, including simple and cross tabulated frequencies, histograms, box plots, (generalized) linear models, mixed effects models, principal component analysis and correlation matrices, cluster analyses, scatter plots, stacked scales, effects plots of regression models (including interaction terms) and much more. This package supports labelled data.
License:GPL-3
Depends:R (≥ 4.1)
Imports:graphics, grDevices, stats, utils, bayestestR (≥ 0.16.1),datawizard (≥ 1.1.0), dplyr, ggeffects, ggplot2 (≥ 3.2.0),knitr, insight (≥ 1.3.1), parameters (≥ 0.27.0), performance(≥ 0.15.0), purrr, rlang, scales, sjlabelled (≥ 1.1.2),sjmisc (≥ 2.8.2), sjstats (≥ 0.17.8), tidyr (≥ 1.0.0)
Suggests:brms, car, clubSandwich, cluster, cowplot, effects, haven,GPArotation, ggrepel, glmmTMB, gridExtra, ggridges, httr, lme4,MASS, nFactors, pscl, psych, rmarkdown, rstanarm, sandwich,splines, survey, TMB, testthat
URL:https://strengejacke.github.io/sjPlot/
BugReports:https://github.com/strengejacke/sjPlot/issues
RoxygenNote:7.3.2
VignetteBuilder:knitr
NeedsCompilation:no
Packaged:2025-07-10 18:10:34 UTC; DL
Author:Daniel LüdeckeORCID iD [aut, cre], Alexander BartelORCID iD [ctb], Carsten Schwemmer [ctb], Chuck PowellORCID iD [ctb], Amir Djalovski [ctb], Johannes TitzORCID iD [ctb]
Repository:CRAN
Date/Publication:2025-07-10 19:00:05 UTC

Data Visualization for Statistics in Social Science

Description

Collection of plotting and table output functions for data visualization. Results of various statistical analyses (that are commonly used in social sciences) can be visualized using this package, including simple and cross tabulated frequencies, histograms, box plots, (generalized) linear models, mixed effects models, PCA and correlation matrices, cluster analyses, scatter plots, Likert scales, effects plots of interaction terms in regression models, constructing index or score variables and much more.

The package supports labelled data, i.e. value and variable labels from labelled data (like vectors or data frames) are automatically used to label the output. Own labels can be specified as well.

What does this package do?

In short, the functions in this package mostly do two things:

  1. compute basic or advanced statistical analyses

  2. either plot the results as ggplot-figure or print them as html-table

How does this package help me?

One of the more challenging tasks when working with R is to get nicely formatted output of statistical analyses, either in graphical or table format. The sjPlot-package takes over these tasks and makes it easy to create beautiful figures or tables.

There are many examples for each function in the related help files and a comprehensive online documentation athttps://strengejacke.github.io/sjPlot/.

A note on the package functions

The main functions follow specific naming conventions, hence starting with a specific prefix, which indicates what kind of task these functions perform.

Author(s)

Daniel Lüdecked.luedecke@uke.de


Plot chi-squared distributions

Description

This function plots a simple chi-squared distribution or a chi-squared distributionwith shaded areas that indicate at which chi-squared value a significant p-levelis reached.

Usage

dist_chisq(  chi2 = NULL,  deg.f = NULL,  p = NULL,  xmax = NULL,  geom.colors = NULL,  geom.alpha = 0.7)

Arguments

chi2

Numeric, optional. If specified, a chi-squared distribution withdeg.f degreesof freedom is plotted and a shaded area atchi2 value position is plotted thatindicates whether or not the specified value is significant or not.If bothchi2 andp are not specified, a distribution without shadedarea is plotted.

deg.f

Numeric. The degrees of freedom for the chi-squared distribution. Needs tobe specified.

p

Numeric, optional. If specified, a chi-squared distribution withdeg.f degreesof freedom is plotted and a shaded area at the position where the specified p-levelstarts is plotted. If bothchi2 andp are not specified, a distributionwithout shaded area is plotted.

xmax

Numeric, optional. Specifies the maximum x-axis-value. If not specified, the x-axisranges to a value where a p-level of 0.00001 is reached.

geom.colors

user defined color for geoms. See 'Details' inplot_grpfrq.

geom.alpha

Specifies the alpha-level of the shaded area. Default is 0.7, range between 0 to 1.

Examples

# a simple chi-squared distribution# for 6 degrees of freedomdist_chisq(deg.f = 6)# a chi-squared distribution for 6 degrees of freedom,# and a shaded area starting at chi-squared value of ten.# With a df of 6, a chi-squared value of 12.59 would be "significant",# thus the shaded area from 10 to 12.58 is filled as "non-significant",# while the area starting from chi-squared value 12.59 is filled as# "significant"dist_chisq(chi2 = 10, deg.f = 6)# a chi-squared distribution for 6 degrees of freedom,# and a shaded area starting at that chi-squared value, which has# a p-level of about 0.125 (which equals a chi-squared value of about 10).# With a df of 6, a chi-squared value of 12.59 would be "significant",# thus the shaded area from 10 to 12.58 (p-level 0.125 to p-level 0.05)# is filled as "non-significant", while the area starting from chi-squared# value 12.59 (p-level < 0.05) is filled as "significant".dist_chisq(p = 0.125, deg.f = 6)

Plot F distributions

Description

This function plots a simple F distribution or an F distributionwith shaded areas that indicate at which F value a significant p-levelis reached.

Usage

dist_f(  f = NULL,  deg.f1 = NULL,  deg.f2 = NULL,  p = NULL,  xmax = NULL,  geom.colors = NULL,  geom.alpha = 0.7)

Arguments

f

Numeric, optional. If specified, an F distribution withdeg.f1 anddeg.f2 degreesof freedom is plotted and a shaded area atf value position is plotted thatindicates whether or not the specified value is significant or not.If bothf andp are not specified, a distribution without shadedarea is plotted.

deg.f1

Numeric. The first degrees of freedom for the F distribution. Needs tobe specified.

deg.f2

Numeric. The second degrees of freedom for the F distribution. Needs tobe specified.

p

Numeric, optional. If specified, a F distribution withdeg.f1 anddeg.f2 degreesof freedom is plotted and a shaded area at the position where the specified p-levelstarts is plotted. If bothf andp are not specified, a distributionwithout shaded area is plotted.

xmax

Numeric, optional. Specifies the maximum x-axis-value. If not specified, the x-axisranges to a value where a p-level of 0.00001 is reached.

geom.colors

user defined color for geoms. See 'Details' inplot_grpfrq.

geom.alpha

Specifies the alpha-level of the shaded area. Default is 0.7, range between 0 to 1.

Examples

# a simple F distribution for 6 and 45 degrees of freedomdist_f(deg.f1 = 6, deg.f2 = 45)# F distribution for 6 and 45 degrees of freedom,# and a shaded area starting at F value of two.# F-values equal or greater than 2.31 are "significant"dist_f(f = 2, deg.f1 = 6, deg.f2 = 45)# F distribution for 6 and 45 degrees of freedom,# and a shaded area starting at a p-level of 0.2# (F-Value about 1.5).dist_f(p = 0.2, deg.f1 = 6, deg.f2 = 45)

Plot normal distributions

Description

This function plots a simple normal distribution or a normal distributionwith shaded areas that indicate at which value a significant p-levelis reached.

Usage

dist_norm(  norm = NULL,  mean = 0,  sd = 1,  p = NULL,  xmax = NULL,  geom.colors = NULL,  geom.alpha = 0.7)

Arguments

norm

Numeric, optional. If specified, a normal distribution withmean andsdis plotted and a shaded area atnorm value position is plotted thatindicates whether or not the specified value is significant or not.If bothnorm andp are not specified, a distribution without shadedarea is plotted.

mean

Numeric. Mean value for normal distribution. By default 0.

sd

Numeric. Standard deviation for normal distribution. By default 1.

p

Numeric, optional. If specified, a normal distribution withmean andsdis plotted and a shaded area at the position where the specified p-levelstarts is plotted. If bothnorm andp are not specified, a distributionwithout shaded area is plotted.

xmax

Numeric, optional. Specifies the maximum x-axis-value. If not specified, the x-axisranges to a value where a p-level of 0.00001 is reached.

geom.colors

user defined color for geoms. See 'Details' inplot_grpfrq.

geom.alpha

Specifies the alpha-level of the shaded area. Default is 0.7, range between 0 to 1.

Examples

# a simple normal distributiondist_norm()# a simple normal distribution with different mean and sd.# note that curve looks similar to above plot, but axis range# has changed.dist_norm(mean = 2, sd = 4)# a simple normal distributiondist_norm(norm = 1)# a simple normal distributiondist_norm(p = 0.2)

Plot t-distributions

Description

This function plots a simple t-distribution or a t-distributionwith shaded areas that indicate at which t-value a significant p-levelis reached.

Usage

dist_t(  t = NULL,  deg.f = NULL,  p = NULL,  xmax = NULL,  geom.colors = NULL,  geom.alpha = 0.7)

Arguments

t

Numeric, optional. If specified, a t-distribution withdeg.f degreesof freedom is plotted and a shaded area att value position is plotted thatindicates whether or not the specified value is significant or not.If botht andp are not specified, a distribution without shadedarea is plotted.

deg.f

Numeric. The degrees of freedom for the t-distribution. Needs tobe specified.

p

Numeric, optional. If specified, a t-distribution withdeg.f degreesof freedom is plotted and a shaded area at the position where the specified p-levelstarts is plotted. If botht andp are not specified, a distributionwithout shaded area is plotted.

xmax

Numeric, optional. Specifies the maximum x-axis-value. If not specified, the x-axisranges to a value where a p-level of 0.00001 is reached.

geom.colors

user defined color for geoms. See 'Details' inplot_grpfrq.

geom.alpha

Specifies the alpha-level of the shaded area. Default is 0.7, range between 0 to 1.

Examples

# a simple t-distribution# for 6 degrees of freedomdist_t(deg.f = 6)# a t-distribution for 6 degrees of freedom,# and a shaded area starting at t-value of one.# With a df of 6, a t-value of 1.94 would be "significant".dist_t(t = 1, deg.f = 6)# a t-distribution for 6 degrees of freedom,# and a shaded area starting at p-level of 0.4# (t-value of about 0.26).dist_t(p = 0.4, deg.f = 6)

Sample dataset from the EUROFAMCARE project

Description

A SPSS sample data set, imported with theread_spss function.


Plot frequencies of variables

Description

Plot frequencies of a variable as bar graph, histogram, box plot etc.

Usage

plot_frq(  data,  ...,  title = "",  weight.by = NULL,  title.wtd.suffix = NULL,  sort.frq = c("none", "asc", "desc"),  type = c("bar", "dot", "histogram", "line", "density", "boxplot", "violin"),  geom.size = NULL,  geom.colors = "#336699",  errorbar.color = "darkred",  axis.title = NULL,  axis.labels = NULL,  xlim = NULL,  ylim = NULL,  wrap.title = 50,  wrap.labels = 20,  grid.breaks = NULL,  expand.grid = FALSE,  show.values = TRUE,  show.n = TRUE,  show.prc = TRUE,  show.axis.values = TRUE,  show.ci = FALSE,  show.na = FALSE,  show.mean = FALSE,  show.mean.val = TRUE,  show.sd = TRUE,  drop.empty = TRUE,  mean.line.type = 2,  mean.line.size = 0.5,  inner.box.width = 0.15,  inner.box.dotsize = 3,  normal.curve = FALSE,  normal.curve.color = "red",  normal.curve.size = 0.8,  normal.curve.alpha = 0.4,  auto.group = NULL,  coord.flip = FALSE,  vjust = "bottom",  hjust = "center",  y.offset = NULL)

Arguments

data

A data frame, or a grouped data frame.

...

Optional, unquoted names of variables that should be selected forfurther processing. Required, ifdata is a data frame (and novector) and only selected variables fromdata should be processed.You may also use functions like: or tidyselect'sselect_helpers.

title

Character vector, used as plot title. By default,response_labels is called to retrieve the label ofthe dependent variable, which will be used as title. Usetitle = ""to remove title.

weight.by

Vector of weights that will be applied to weight all cases.Must be a vector of same length as the input vector. Default isNULL, so no weights are used.

title.wtd.suffix

Suffix (as string) for the title, ifweight.by is specified,e.g.title.wtd.suffix=" (weighted)". Default isNULL, sotitle will not have a suffix when cases are weighted.

sort.frq

Determines whether categories should be sortedaccording to their frequencies or not. Default is"none", socategories are not sorted by frequency. Use"asc" or"desc" for sorting categories ascending or descending order.

type

Specifies the plot type. May be abbreviated.

"bar"

for simple bars (default)

"dot"

for a dot plot

"histogram"

for a histogram (does not apply to grouped frequencies)

"line"

for a line-styled histogram with filled area

"density"

for a density plot (does not apply to grouped frequencies)

"boxplot"

for box plot

"violin"

for violin plots

geom.size

size resp. width of the geoms (bar width, line thickness or point size,depending on plot type and function). Note that bar and bin widths mostlyneed smaller values than dot sizes.

geom.colors

User defined color for geoms, e.g.geom.colors = "#0080ff".

errorbar.color

Color of confidence interval bars (error bars).Only applies totype = "bar". In case of dot plots, error barswill have same colors as dots (seegeom.colors).

axis.title

Character vector of length one or two (depending onthe plot function and type), used as title(s) for the x and y axis.If not specified, a default labelling is chosen.Note: Some plot types do not support this argument. In suchcases, use the return value and add axis titles manually withlabs, e.g.:$plot.list[[1]] + ggplot2::labs(x = ...)

axis.labels

character vector with labels used as axis labels. Optionalargument, since in most cases, axis labels are set automatically.

xlim

Numeric vector of length two, defining lower and upper axis limitsof the x scale. By default, this argument is set toNULL, i.e. thex-axis fits to the required range of the data.

ylim

numeric vector of length two, defining lower and upper axis limitsof the y scale. By default, this argument is set toNULL, i.e. they-axis fits to the required range of the data.

wrap.title

Numeric, determines how many chars of the plot title aredisplayed in one line and when a line break is inserted.

wrap.labels

numeric, determines how many chars of the value, variable or axislabels are displayed in one line and when a line break is inserted.

grid.breaks

numeric; sets the distance between breaks for the axis,i.e. at everygrid.breaks'th position a major grid is being printed.

expand.grid

logical, ifTRUE, the plot grid is expanded, i.e. there is a small margin betweenaxes and plotting region. Default isFALSE.

show.values

Logical, whether values should be plotted or not.

show.n

logical, ifTRUE, adds total number of cases for eachgroup or category to the labels.

show.prc

logical, ifTRUE (default), percentage values are plotted to each barIfFALSE, percentage values are removed.

show.axis.values

logical, whether category, count or percentage values for the axisshould be printed or not.

show.ci

Logical, ifTRUE), adds notches to the box plot, which areused to compare groups; if the notches of two boxes do not overlap,medians are considered to be significantly different.

show.na

logical, ifTRUE,NA's (missing values)are added to the output.

show.mean

Logical, ifTRUE, a vertical line in histogramsis drawn to indicate the mean value of the variables. Onlyapplies to histogram-charts.

show.mean.val

Logical, ifTRUE (default), the mean valueis printed to the vertical line that indicates the variable'smean. Only applies to histogram-charts.

show.sd

Logical, ifTRUE, the standard deviationis annotated as shaded rectangle around the mean interceptline. Only applies to histogram-charts.

drop.empty

Logical, ifTRUE and the variable's values are labeled, values / factorlevels with no occurrence in the data are omitted from the output. IfFALSE,labeled values that have no observations are still printed in the table (with frequency0).

mean.line.type

Numeric value, indicating the linetype of the meanintercept line. Only applies to histogram-charts andwhenshow.mean = TRUE.

mean.line.size

Numeric, size of the mean intercept line. Onlyapplies to histogram-charts and whenshow.mean = TRUE.

inner.box.width

width of the inner box plot that is plotted inside of violin plots. Only appliesiftype = "violin". Default value is 0.15

inner.box.dotsize

size of mean dot insie a violin or box plot. Applies onlywhentype = "violin" or"boxplot".

normal.curve

Logical, ifTRUE, a normal curve, which is adjusted to the data,is plotted over the histogram or density plot. Default isFALSE. Only applies when histograms or density plots are plotted (seetype).

normal.curve.color

Color of the normal curve line. Onlyapplies ifnormal.curve = TRUE.

normal.curve.size

Numeric, size of the normal curve line. Onlyapplies ifnormal.curve = TRUE.

normal.curve.alpha

Transparancy level (alpha value) of the normal curve. Onlyapplies ifnormal.curve = TRUE.

auto.group

numeric value, indicating the minimum amount of unique valuesin the count variable, at which automatic grouping into smaller unitsis done (seegroup_var). Default value forauto.group isNULL, i.e. auto-grouping is off.Seegroup_var for examples on grouping.

coord.flip

logical, ifTRUE, the x and y axis are swapped.

vjust

character vector, indicating the vertical position of valuelabels. Allowed are same values as forvjust aesthetics fromggplot2: "left", "center", "right", "bottom", "middle", "top" andnew options like "inward" and "outward", which align text towards andaway from the center of the plot respectively.

hjust

character vector, indicating the horizontal position of valuelabels. Allowed are same values as forvjust aesthetics fromggplot2: "left", "center", "right", "bottom", "middle", "top" andnew options like "inward" and "outward", which align text towards andaway from the center of the plot respectively.

y.offset

numeric, offset for text labels when their alignment is adjustedto the top/bottom of the geom (seehjust andvjust).

Value

A ggplot-object.

Note

This function only works with variables with integer values (or numericfactor levels), i.e. scales / centered variableswith fractional part may result in unexpected behaviour.

Examples

library(sjlabelled)data(efc)data(iris)# simple plots, two different notationsplot_frq(iris, Species)plot_frq(efc$tot_sc_e)# boxplotplot_frq(efc$e17age, type = "box")if (require("dplyr")) {  # histogram, pipe-workflow  efc |>    dplyr::select(e17age, c160age) |>    plot_frq(type = "hist", show.mean = TRUE)  # bar plot(s)  plot_frq(efc, e42dep, c172code)}if (require("dplyr") && require("gridExtra")) {  # grouped data frame, all panels in one plot  efc |>    group_by(e42dep) |>    plot_frq(c161sex) |>    plot_grid()}library(sjmisc)# grouped variableageGrp <- group_var(efc$e17age)ageGrpLab <- group_labels(efc$e17age)plot_frq(ageGrp, title = get_label(efc$e17age), axis.labels = ageGrpLab)# plotting confidence intervals. expand grid and v/hjust for text labelsplot_frq(  efc$e15relat, type = "dot", show.ci = TRUE, sort.frq = "desc",  coord.flip = TRUE, expand.grid = TRUE, vjust = "bottom", hjust = "left")# histogram with overlayed normal curveplot_frq(efc$c160age, type = "h", show.mean = TRUE, show.mean.val = TRUE,        normal.curve = TRUE, show.sd = TRUE, normal.curve.color = "blue",        normal.curve.size = 3, ylim = c(0,50))

Plot grouped proportional tables

Description

Plot grouped proportional crosstables, where the proportion ofeach level ofx for the highest category inyis plotted, for each subgroup ofgrp.

Usage

plot_gpt(  data,  x,  y,  grp,  colors = "metro",  geom.size = 2.5,  shape.fill.color = "#f0f0f0",  shapes = c(15, 16, 17, 18, 21, 22, 23, 24, 25, 7, 8, 9, 10, 12),  title = NULL,  axis.labels = NULL,  axis.titles = NULL,  legend.title = NULL,  legend.labels = NULL,  wrap.title = 50,  wrap.labels = 15,  wrap.legend.title = 20,  wrap.legend.labels = 20,  axis.lim = NULL,  grid.breaks = NULL,  show.total = TRUE,  annotate.total = TRUE,  show.p = TRUE,  show.n = TRUE)

Arguments

data

A data frame, or a grouped data frame.

x

Categorical variable, where the proportion of each category inx for the highest category ofy will be printedalong the x-axis.

y

Categorical or numeric variable. If not a binary variable,ywill be recoded into a binary variable, dichtomized at the highestcategory and all remaining categories.

grp

Grouping variable, which will define the y-axis

colors

May be a character vector of color values in hex-format, validcolor value names (seedemo("colors")) or a name of a pre-definedcolor palette. Following options are valid for thecolors argument:

  • If not specified, a default color brewer palette will be used, which is suitable for the plot style.

  • If"gs", a greyscale will be used.

  • If"bw", and plot-type is a line-plot, the plot is black/white and uses different line types to distinguish groups (seethis package-vignette).

  • Ifcolors is any valid color brewer palette name, the related palette will be used. UseRColorBrewer::display.brewer.all() to view all available palette names.

  • There are some pre-defined color palettes in this package, seesjPlot-themes for details.

  • Else specify own color values or names as vector (e.g.colors = "#00ff00" orcolors = c("firebrick", "blue")).

geom.size

size resp. width of the geoms (bar width, line thickness or point size,depending on plot type and function). Note that bar and bin widths mostlyneed smaller values than dot sizes.

shape.fill.color

Optional color vector, fill-color for non-filled shapes

shapes

Numeric vector with shape styles, used to map the differentcategories ofx.

title

Character vector, used as plot title. By default,response_labels is called to retrieve the label ofthe dependent variable, which will be used as title. Usetitle = ""to remove title.

axis.labels

character vector with labels used as axis labels. Optionalargument, since in most cases, axis labels are set automatically.

axis.titles

character vector of length one or two, defining the title(s)for the x-axis and y-axis.

legend.title

Character vector, used as legend title for plots thathave a legend.

legend.labels

character vector with labels for the guide/legend.

wrap.title

Numeric, determines how many chars of the plot title aredisplayed in one line and when a line break is inserted.

wrap.labels

numeric, determines how many chars of the value, variable or axislabels are displayed in one line and when a line break is inserted.

wrap.legend.title

numeric, determines how many chars of the legend's titleare displayed in one line and when a line break is inserted.

wrap.legend.labels

numeric, determines how many chars of the legend labels aredisplayed in one line and when a line break is inserted.

axis.lim

Numeric vector of length 2, defining the range of the plot axis.Depending on plot type, may effect either x- or y-axis, or both.For multiple plot outputs (e.g., fromtype = "eff" ortype = "slope" inplot_model),axis.lim mayalso be a list of vectors of length 2, defining axis limits for eachplot (only if non-faceted).

grid.breaks

numeric; sets the distance between breaks for the axis,i.e. at everygrid.breaks'th position a major grid is being printed.

show.total

Logical, ifTRUE, a total summary line for all aggregatedgrp is added.

annotate.total

Logical, ifTRUE andshow.total = TRUE,the total-row in the figure will be highlighted with a slightlyshaded background.

show.p

Logical, adds significance levels to values, or value andvariable labels.

show.n

logical, ifTRUE, adds total number of cases for eachgroup or category to the labels.

Details

The p-values are based onchisq.test ofxandy for eachgrp.

Value

A ggplot-object.

Examples

if (requireNamespace("haven")) {  data(efc)  # the proportion of dependency levels in female  # elderly, for each family carer's relationship  # to elderly  plot_gpt(efc, e42dep, e16sex, e15relat)  # proportion of educational levels in highest  # dependency category of elderly, for different  # care levels  plot_gpt(efc, c172code, e42dep, n4pstu)}

Arrange list of plots as grid

Description

Plot multiple ggplot-objects as a grid-arranged single plot.

Usage

plot_grid(x, margin = c(1, 1, 1, 1), tags = NULL)

Arguments

x

A list of ggplot-objects. See 'Details'.

margin

A numeric vector of length 4, indicating the top, right, bottomand left margin for each plot, in centimetres.

tags

Add tags to your subfigures. Can beTRUE (letter tags)or character vector containing tags labels.

Details

This function takes alist of ggplot-objects as argument.Plotting functions of this package that produce multiple plotobjects (e.g., when there is an argumentfacet.grid) usuallyreturn multiple plots as list (the return value is namedplot.list).To arrange these plots as grid as a single plot, useplot_grid.

Value

An object of classgtable.

Examples

if (require("dplyr") && require("gridExtra")) {  library(ggeffects)  data(efc)  # fit model  fit <- glm(    tot_sc_e ~ c12hour + e17age + e42dep + neg_c_7,    data = efc,    family = poisson  )  # plot marginal effects for each predictor, each as single plot  p1 <- ggpredict(fit, "c12hour") |>    plot(show_y_title = FALSE, show_title = FALSE)  p2 <- ggpredict(fit, "e17age") |>    plot(show_y_title = FALSE, show_title = FALSE)  p3 <- ggpredict(fit, "e42dep") |>    plot(show_y_title = FALSE, show_title = FALSE)  p4 <- ggpredict(fit, "neg_c_7") |>    plot(show_y_title = FALSE, show_title = FALSE)  # plot grid  plot_grid(list(p1, p2, p3, p4))  # plot grid  plot_grid(list(p1, p2, p3, p4), tags = TRUE)}

Plot grouped or stacked frequencies

Description

Plot grouped or stacked frequencies of variables as bar/dot,box or violin plots, or line plot.

Usage

plot_grpfrq(  var.cnt,  var.grp,  type = c("bar", "dot", "line", "boxplot", "violin"),  bar.pos = c("dodge", "stack"),  weight.by = NULL,  intr.var = NULL,  title = "",  title.wtd.suffix = NULL,  legend.title = NULL,  axis.titles = NULL,  axis.labels = NULL,  legend.labels = NULL,  intr.var.labels = NULL,  wrap.title = 50,  wrap.labels = 15,  wrap.legend.title = 20,  wrap.legend.labels = 20,  geom.size = NULL,  geom.spacing = 0.15,  geom.colors = "Paired",  show.values = TRUE,  show.n = TRUE,  show.prc = TRUE,  show.axis.values = TRUE,  show.ci = FALSE,  show.grpcnt = FALSE,  show.legend = TRUE,  show.na = FALSE,  show.summary = FALSE,  drop.empty = TRUE,  auto.group = NULL,  ylim = NULL,  grid.breaks = NULL,  expand.grid = FALSE,  inner.box.width = 0.15,  inner.box.dotsize = 3,  smooth.lines = FALSE,  emph.dots = TRUE,  summary.pos = "r",  facet.grid = FALSE,  coord.flip = FALSE,  y.offset = NULL,  vjust = "bottom",  hjust = "center")

Arguments

var.cnt

Vector of counts, for which frequencies or means will be plotted or printed.

var.grp

Factor with the cross-classifying variable, wherevar.cntis grouped into the categories represented byvar.grp.

type

Specifies the plot type. May be abbreviated.

"bar"

for simple bars (default)

"dot"

for a dot plot

"histogram"

for a histogram (does not apply to grouped frequencies)

"line"

for a line-styled histogram with filled area

"density"

for a density plot (does not apply to grouped frequencies)

"boxplot"

for box plot

"violin"

for violin plots

bar.pos

Indicates whether bars should be positioned side-by-side (default),or stacked (bar.pos = "stack"). May be abbreviated.

weight.by

Vector of weights that will be applied to weight all cases.Must be a vector of same length as the input vector. Default isNULL, so no weights are used.

intr.var

An interaction variable which can be used for box plots. Divides each category indicatedbyvar.grp into the factors ofintr.var, so that each category ofvar.grpis subgrouped intointr.var's categories. Only applies whentype = "boxplot" ortype = "violin".

title

character vector, used as plot title. Depending on plot type and function,will be set automatically. Iftitle = "", no title is printed.For effect-plots, may also be a character vector of length > 1,to define titles for each sub-plot or facet.

title.wtd.suffix

Suffix (as string) for the title, ifweight.by is specified,e.g.title.wtd.suffix=" (weighted)". Default isNULL, sotitle will not have a suffix when cases are weighted.

legend.title

character vector, used as title for the plot legend.

axis.titles

character vector of length one or two, defining the title(s)for the x-axis and y-axis.

axis.labels

character vector with labels used as axis labels. Optionalargument, since in most cases, axis labels are set automatically.

legend.labels

character vector with labels for the guide/legend.

intr.var.labels

a character vector with labels for the x-axis breakswhen having interaction variables included.These labels replace theaxis.labels. Only applies, when using box or violin plots(i.e.type = "boxplot" or"violin") andintr.var is notNULL.

wrap.title

numeric, determines how many chars of the plot title are displayed inone line and when a line break is inserted.

wrap.labels

numeric, determines how many chars of the value, variable or axislabels are displayed in one line and when a line break is inserted.

wrap.legend.title

numeric, determines how many chars of the legend's titleare displayed in one line and when a line break is inserted.

wrap.legend.labels

numeric, determines how many chars of the legend labels aredisplayed in one line and when a line break is inserted.

geom.size

size resp. width of the geoms (bar width, line thickness or point size,depending on plot type and function). Note that bar and bin widths mostlyneed smaller values than dot sizes.

geom.spacing

the spacing between geoms (i.e. bar spacing)

geom.colors

user defined color for geoms. See 'Details' inplot_grpfrq.

show.values

Logical, whether values should be plotted or not.

show.n

logical, ifTRUE, adds total number of cases for eachgroup or category to the labels.

show.prc

logical, ifTRUE (default), percentage values are plotted to each barIfFALSE, percentage values are removed.

show.axis.values

logical, whether category, count or percentage values for the axisshould be printed or not.

show.ci

Logical, ifTRUE), adds notches to the box plot, which areused to compare groups; if the notches of two boxes do not overlap,medians are considered to be significantly different.

show.grpcnt

logical, ifTRUE, the count within each group is addedto the category labels (e.g."Cat 1 (n=87)"). Default value isFALSE.

show.legend

logical, ifTRUE, and depending on plot type andfunction, a legend is added to the plot.

show.na

logical, ifTRUE,NA's (missing values)are added to the output.

show.summary

logical, ifTRUE (default), a summary with chi-squaredstatistics (seechisq.test), Cramer's V or Phi-value etc.is shown. If a cell contains expected values lower than five (or lower than 10if df is 1), the Fisher's exact test (seefisher.test) iscomputed instead of chi-squared test. If the table's matrix is largerthan 2x2, Fisher's exact test with Monte Carlo simulation is computed.

drop.empty

Logical, ifTRUE and the variable's values are labeled, values / factorlevels with no occurrence in the data are omitted from the output. IfFALSE,labeled values that have no observations are still printed in the table (with frequency0).

auto.group

numeric value, indicating the minimum amount of unique valuesin the count variable, at which automatic grouping into smaller unitsis done (seegroup_var). Default value forauto.group isNULL, i.e. auto-grouping is off.Seegroup_var for examples on grouping.

ylim

numeric vector of length two, defining lower and upper axis limitsof the y scale. By default, this argument is set toNULL, i.e. they-axis fits to the required range of the data.

grid.breaks

numeric; sets the distance between breaks for the axis,i.e. at everygrid.breaks'th position a major grid is being printed.

expand.grid

logical, ifTRUE, the plot grid is expanded, i.e. there is a small margin betweenaxes and plotting region. Default isFALSE.

inner.box.width

width of the inner box plot that is plotted inside of violin plots. Only appliesiftype = "violin". Default value is 0.15

inner.box.dotsize

size of mean dot insie a violin or box plot. Applies onlywhentype = "violin" or"boxplot".

smooth.lines

prints a smooth line curve. Only applies, when argumenttype = "line".

emph.dots

logical, ifTRUE, the groups of dots in a dot-plot are highlightedwith a shaded rectangle.

summary.pos

position of the model summary which is printed whenshow.summaryisTRUE. Default is"r", i.e. it's printed to the upper right corner.Use"l" for upper left corner.

facet.grid

TRUE to arrange the lay out of of multiple plotsin a grid of an integrated single plot. This argument callsfacet_wrap orfacet_gridto arrange plots. Useplot_grid to plot multiple plot-objectsas an arranged grid withgrid.arrange.

coord.flip

logical, ifTRUE, the x and y axis are swapped.

y.offset

numeric, offset for text labels when their alignment is adjustedto the top/bottom of the geom (seehjust andvjust).

vjust

character vector, indicating the vertical position of valuelabels. Allowed are same values as forvjust aesthetics fromggplot2: "left", "center", "right", "bottom", "middle", "top" andnew options like "inward" and "outward", which align text towards andaway from the center of the plot respectively.

hjust

character vector, indicating the horizontal position of valuelabels. Allowed are same values as forvjust aesthetics fromggplot2: "left", "center", "right", "bottom", "middle", "top" andnew options like "inward" and "outward", which align text towards andaway from the center of the plot respectively.

Details

geom.colors may be a character vector of color valuesin hex-format, valid color value names (seedemo("colors") ora name of acolor brewer palette.Following options are valid for thegeom.colors argument:

Value

A ggplot-object.

Examples

data(efc)plot_grpfrq(efc$e17age, efc$e16sex, show.values = FALSE)# boxplotplot_grpfrq(efc$e17age, efc$e42dep, type = "box")# grouped barsplot_grpfrq(efc$e42dep, efc$e16sex, title = NULL)# box plots with interaction variableplot_grpfrq(efc$e17age, efc$e42dep, intr.var = efc$e16sex, type = "box")# Grouped bar plotplot_grpfrq(efc$neg_c_7, efc$e42dep, show.values = FALSE)# same data as line plotplot_grpfrq(efc$neg_c_7, efc$e42dep, type = "line")# show ony categories where we have data (i.e. drop zero-counts)library(dplyr)efc <- dplyr::filter(efc, e42dep %in% c(3,4))plot_grpfrq(efc$c161sex, efc$e42dep, drop.empty = TRUE)# show all categories, even if not in dataplot_grpfrq(efc$c161sex, efc$e42dep, drop.empty = FALSE)

Plot likert scales as centered stacked bars

Description

Plot likert scales as centered stacked bars.

Usage

plot_likert(  items,  groups = NULL,  groups.titles = "auto",  title = NULL,  legend.title = NULL,  legend.labels = NULL,  axis.titles = NULL,  axis.labels = NULL,  catcount = NULL,  cat.neutral = NULL,  sort.frq = NULL,  weight.by = NULL,  title.wtd.suffix = NULL,  wrap.title = 50,  wrap.labels = 30,  wrap.legend.title = 30,  wrap.legend.labels = 28,  geom.size = 0.6,  geom.colors = "BrBG",  cat.neutral.color = "grey70",  intercept.line.color = "grey50",  reverse.colors = FALSE,  values = "show",  show.n = TRUE,  show.legend = TRUE,  show.prc.sign = FALSE,  grid.range = 1,  grid.breaks = 0.2,  expand.grid = TRUE,  digits = 1,  reverse.scale = FALSE,  coord.flip = TRUE,  sort.groups = TRUE,  legend.pos = "bottom",  rel_heights = 1,  group.legend.options = list(nrow = NULL, byrow = TRUE),  cowplot.options = list(label_x = 0.01, hjust = 0, align = "v"))

Arguments

items

Data frame, or a grouped data frame, with each column representing one item.

groups

(optional) Must be a vector of same length asncol(items),where each item in this vector represents the group numberof the related columns ofitems. See 'Examples'.

groups.titles

(optional, only used if groups are supplied) Titles for each factor group that will be used as table caption for eachcomponent-table. Must be a character vector of same length aslength(unique(groups)).Default is"auto", which means that each table has a standard captionComponent x.UseNULL to use names as supplied togroups and useFALSE to suppress table captions.

title

character vector, used as plot title. Depending on plot type and function,will be set automatically. Iftitle = "", no title is printed.For effect-plots, may also be a character vector of length > 1,to define titles for each sub-plot or facet.

legend.title

character vector, used as title for the plot legend.

legend.labels

character vector with labels for the guide/legend.

axis.titles

character vector of length one or two, defining the title(s)for the x-axis and y-axis.

axis.labels

character vector with labels used as axis labels. Optionalargument, since in most cases, axis labels are set automatically.

catcount

optional, amount of categories ofitems (e.g."strongly disagree","disagree", "agree" and"strongly agree" would becatcount = 4).Note that this argument only applies to "valid" answers, i.e. if youhave an additional neutral category (seecat.neutral) like"don't know",this won't count forcatcount (e.g. "strongly disagree","disagree", "agree", "strongly agree" and neutral category "don't know"would still mean thatcatcount = 4). See 'Note'.

cat.neutral

If there's a neutral category (like "don't know" etc.), specifythe index number (value) for this category. Else, setcat.neutral = NULL (default).The proportions of neutral category answers are plotted as grey bars on the left side ofthe figure.

sort.frq

Indicates whether the items ofitems should be ordered bytotal sum of positive or negative answers.

"pos.asc"

to order ascending by sum of positive answers

"pos.desc"

to order descending by sum of positive answers

"neg.asc"

for sorting ascending negative answers

"neg.desc"

for sorting descending negative answers

NULL

(default) for no sorting

weight.by

Vector of weights that will be applied to weight all cases.Must be a vector of same length as the input vector. Default isNULL, so no weights are used.

title.wtd.suffix

Suffix (as string) for the title, ifweight.by is specified,e.g.title.wtd.suffix=" (weighted)". Default isNULL, sotitle will not have a suffix when cases are weighted.

wrap.title

numeric, determines how many chars of the plot title are displayed inone line and when a line break is inserted.

wrap.labels

numeric, determines how many chars of the value, variable or axislabels are displayed in one line and when a line break is inserted.

wrap.legend.title

numeric, determines how many chars of the legend's titleare displayed in one line and when a line break is inserted.

wrap.legend.labels

numeric, determines how many chars of the legend labels aredisplayed in one line and when a line break is inserted.

geom.size

size resp. width of the geoms (bar width, line thickness or point size,depending on plot type and function). Note that bar and bin widths mostlyneed smaller values than dot sizes.

geom.colors

user defined color for geoms. See 'Details' inplot_grpfrq.

cat.neutral.color

Color of the neutral category, if plotted (seecat.neutral).

intercept.line.color

Color of the vertical intercept line that divides positive and negative values.

reverse.colors

logical, ifTRUE, the color scale fromgeom.colors will be reversed,so positive and negative values switch colors.

values

Determines style and position of percentage value labels on the bars:

"show"

(default) shows percentage value labels in the middle of each category bar

"hide"

hides the value labels, so no percentage values on the bars are printed

"sum.inside"

shows the sums of percentage values for both negative and positive values and prints them inside the end of each bar

"sum.outside"

shows the sums of percentage values for both negative and positive values and prints them outside the end of each bar

show.n

logical, ifTRUE, adds total number of cases for eachgroup or category to the labels.

show.legend

logical, ifTRUE, and depending on plot type andfunction, a legend is added to the plot.

show.prc.sign

logical, ifTRUE, %-signs for value labels are shown.

grid.range

Numeric, limits of the x-axis-range, as proportion of 100.Default is 1, so the x-scale ranges from zero to 100% on both sides from the center.Can alternatively be supplied as a vector of 2 positive numbers (e.g.grid.range = c(1, .8))to set the left and right limit separately. You can use values beyond 1 (100%) in case bar labels are not printed becausethey exceed the axis range. E.g.grid.range = 1.4 will set the axis from -140 to +140%, however, only(valid) axis labels from -100 to +100% are printed. Neutral categories are adjusted to the most left limit.

grid.breaks

numeric; sets the distance between breaks for the axis,i.e. at everygrid.breaks'th position a major grid is being printed.

expand.grid

logical, ifTRUE, the plot grid is expanded, i.e. there is a small margin betweenaxes and plotting region. Default isFALSE.

digits

Numeric, amount of digits after decimal point when roundingestimates or values.

reverse.scale

logical, ifTRUE, the ordering of the categories is reversed, so positive and negative values switch position.

coord.flip

logical, ifTRUE, the x and y axis are swapped.

sort.groups

(optional, only used if groups are supplied) logical, if groups should be sorted according to the values supplied togroups. Defaults toTRUE.

legend.pos

(optional, only used if groups are supplied) Defines the legend position. Possible values arec("bottom", "top", "both", "all", "none").If the is only one group or this option is set to"all" legends will be printed as defined withset_theme.

rel_heights

(optional, only used if groups are supplied) This option can be used to adjust the height of the subplots. The bars in subplots can have different heights due to a differing number of itemsor due to legend placement. This can be adjusted here. Takes a vector of numbers, onefor each plot. Values are evaluated relative to each other.

group.legend.options

(optional, only used if groups are supplied) List of options to be passed toguide_legend.The most notable options arebyrow=T (default), this will order the categories row wise.And withgroup.legend.options = list(nrow = 1) all categories can be forced to be on a single row.

cowplot.options

(optional, only used if groups are supplied) List of label options to be passed toplot_grid.

Value

A ggplot-object.

Note

Note that only even numbers of categories are possible to plot, so the "positive"and "negative" values can be splitted into two halfs. A neutral category (like "don't know")can be used, but must be indicated bycat.neutral.

Thecatcount-argument indicates how many item categories are in theLikert scale. Normally, this argument can be ignored because the amount ofvalid categories is retrieved automatically. However, sometimes (for instance,if a certain category is missing in all items), auto-detection of the amountof categories fails. In such cases, specify the amount of categorieswith thecatcount-argument.

Examples

if (requireNamespace("ggrepel") && requireNamespace("sjmisc")) {library(sjmisc)data(efc)# find all variables from COPE-Index, which all have a "cop" in their# variable name, and then plot that subset as likert-plotmydf <- find_var(efc, pattern = "cop", out = "df")plot_likert(mydf)plot_likert(  mydf,  grid.range = c(1.2, 1.4),  expand.grid = FALSE,  values = "sum.outside",  show.prc.sign = TRUE)# Plot in groupsplot_likert(mydf, c(2,1,1,1,1,2,2,2,1))if (require("parameters") && require("nFactors")) {  groups <- parameters::principal_components(mydf)  plot_likert(mydf, groups = parameters::closest_component(groups))}plot_likert(mydf,            c(rep("B", 4), rep("A", 5)),            sort.groups = FALSE,            grid.range = c(0.9, 1.1),            geom.colors = "RdBu",            rel_heights = c(6, 8),            wrap.labels = 40,            reverse.scale = TRUE)# control legend itemssix_cat_example = data.frame(  matrix(sample(1:6, 600, replace = TRUE),  ncol = 6))## Not run: six_cat_example <-  six_cat_example |>  dplyr::mutate_all(~ordered(.,labels = c("+++","++","+","-","--","---")))# Old defaultplot_likert(  six_cat_example,  groups = c(1, 1, 1, 2, 2, 2),  group.legend.options = list(nrow = 2, byrow = FALSE))# New defaultplot_likert(six_cat_example, groups = c(1, 1, 1, 2, 2, 2))# Single rowplot_likert(  six_cat_example,  groups = c(1, 1, 1, 2, 2, 2),  group.legend.options = list(nrow = 1))## End(Not run)}

Plot regression models

Description

plot_model() creates plots from regression models, eitherestimates (as so-called forest or dot whisker plots) or marginal effects.

Usage

plot_model(  model,  type = c("est", "re", "eff", "emm", "pred", "int", "std", "std2", "slope", "resid",    "diag"),  transform,  terms = NULL,  sort.est = NULL,  rm.terms = NULL,  group.terms = NULL,  order.terms = NULL,  pred.type = c("fe", "re"),  mdrt.values = c("minmax", "meansd", "zeromax", "quart", "all"),  ri.nr = NULL,  title = NULL,  axis.title = NULL,  axis.labels = NULL,  legend.title = NULL,  wrap.title = 50,  wrap.labels = 25,  axis.lim = NULL,  grid.breaks = NULL,  ci.lvl = NULL,  se = NULL,  vcov.fun = NULL,  vcov.args = NULL,  colors = "Set1",  show.intercept = FALSE,  show.values = FALSE,  show.p = TRUE,  show.data = FALSE,  show.legend = TRUE,  show.zeroinf = TRUE,  value.offset = NULL,  value.size,  jitter = NULL,  digits = 2,  dot.size = NULL,  line.size = NULL,  vline.color = NULL,  p.threshold = c(0.05, 0.01, 0.001),  p.val = NULL,  p.adjust = NULL,  grid,  case,  auto.label = TRUE,  prefix.labels = c("none", "varname", "label"),  bpe = "median",  bpe.style = "line",  bpe.color = "white",  ci.style = c("whisker", "bar"),  std.response = TRUE,  ...)get_model_data(  model,  type = c("est", "re", "eff", "pred", "int", "std", "std2", "slope", "resid", "diag"),  transform,  terms = NULL,  sort.est = NULL,  rm.terms = NULL,  group.terms = NULL,  order.terms = NULL,  pred.type = c("fe", "re"),  ri.nr = NULL,  ci.lvl = NULL,  colors = "Set1",  grid,  case = "parsed",  digits = 2,  ...)

Arguments

model

A regression model object. Depending on thetype, manykinds of models are supported, e.g. from packages likestats,lme4,nlme,rstanarm,survey,glmmTMB,MASS,brms etc.

type

Type of plot. There are three groups of plot-types:

Coefficients (related vignette)

type = "est"

Forest-plot of estimates. If the fitted modelonly contains one predictor, slope-line is plotted.

type = "re"

For mixed effects models, plots the randomeffects.

type = "std"

Forest-plot of standardized coefficients.

type = "std2"

Forest-plot of standardized coefficients,however, standardization is done by dividing by two SD (see 'Details').

Marginal Effects (related vignette)

type = "pred"

Predicted values (marginal effects) forspecific model terms. Seeggpredict for details.

type = "eff"

Similar totype = "pred", however,discrete predictors are held constant at their proportions (not referencelevel). Seeggeffect for details.

type = "emm"

Similar totype = "eff", seeggemmeans for details.

type = "int"

Marginal effects of interaction terms inmodel.

Model diagnostics

type = "slope"

Slope of coefficients for each singlepredictor, against the response (linear relationship between each modelterm and response). See 'Details'.

type = "resid"

Slope of coefficients for each singlepredictor, against the residuals (linear relationship between each modelterm and residuals). See 'Details'.

type = "diag"

Check model assumptions. See 'Details'.

Note: For mixed models, the diagnostic plots like linear relationshipor check for Homoscedasticity, donot take the uncertainty ofrandom effects into account, but is only based on the fixed effects partof the model.

transform

A character vector, naming a function that will be appliedon estimates and confidence intervals. By default,transform willautomatically use"exp" as transformation for applicable classes ofmodel (e.g. logistic or poisson regression). Estimates of linearmodels remain untransformed. UseNULL if you want the raw,non-transformed estimates.

terms

Character vector with the names of those terms frommodelthat should be plotted. This argument depends on the plot-type:

Coefficients

Select terms that should be plotted. All otherterm are removed from the output. Note that the term names must matchthe names of the model's coefficients. For factors, this means thatthe variable name is suffixed with the related factor level, and eachcategory counts as one term. E.g.rm.terms = "t_name [2,3]"would remove the terms"t_name2" and"t_name3" (assumingthat the variablet_name is categorical and has at leastthe factor levels2 and3). Another example for theiris-dataset:terms = "Species" would not work, insteadyou would writeterms = "Species [versicolor,virginica]" toremove these two levels, orterms = "Speciesversicolor" if youjust want to remove the levelversicolor from the plot.

Marginal Effects

Hereterms indicates for whichterms marginal effects should be displayed. At least one term isrequired to calculate effects, maximum length is three terms, wherethe second and third term indicate the groups, i.e. predictions offirst term are grouped by the levels of the second (and third) term.terms may also indicate higher order terms (e.g. interactionterms). Indicating levels in square brackets allows for selecting onlyspecific groups. Term name and levels in brackets must be separated bya whitespace character, e.g.terms = c("age", "education [1,3]").It is also possible to specify a range of numeric values for thepredictions with a colon, for instanceterms = c("education [1,3]", "age [30:50]"). Furthermore, it is possible to specify a function name.Values for predictions will then be transformed, e.g.terms = "income [exp]". This is useful when model predictors weretransformed for fitting the model and should be back-transformed to theoriginal scale for predictions. Finally, numeric vectors for which nospecific values are given, a "pretty range" is calculated, to avoidmemory allocation problems for vectors with many unique values. If anumeric vector is specified as second or third term (i.e. if this vectorrepresents a grouping structure), representative values (seevalues_at) are chosen. If all values for anumeric vector should be used to compute predictions, you may usee.g. terms = "age [all]". For more details, seeggpredict.

sort.est

Determines in which way estimates are sorted in the plot:

  • IfNULL (default), no sorting is done and estimates are sorted in the same order as they appear in the model formula.

  • IfTRUE, estimates are sorted in descending order, with highest estimate at the top.

  • Ifsort.est = "sort.all", estimates are re-sorted for each coefficient (only applies iftype = "re" andgrid = FALSE), i.e. the estimates of the random effects for each predictor are sorted and plotted to an own plot.

  • Iftype = "re", specify a predictor's / coefficient's name to sort estimates according to this random effect.

rm.terms

Character vector with names that indicate which terms shouldbe removed from the plot. Counterpart toterms.rm.terms ="t_name" would remove the termt_name. Default isNULL, i.e.all terms are used. For factors, levels that should be removed from the plotneed to be explicitely indicated in square brackets, and match the model'scoefficient names, e.g.rm.terms = "t_name [2,3]" would remove the terms"t_name2" and"t_name3" (assuming that the variablet_namewas categorical and has at least the factor levels2 and3).Another example for theiris dataset would berm.terms = "Species [versicolor,virginica]". Note that therm.terms-argument does not apply toMarginal Effects plots.

group.terms

Numeric vector with group indices, to group coefficients.Each group of coefficients gets its own color (see 'Examples').

order.terms

Numeric vector, indicating in which order the coefficientsshould be plotted. See examples inthis package-vignette.

pred.type

Character, only applies forMarginal Effects plotswith mixed effects models. Indicates whether predicted values should beconditioned on random effects (pred.type = "re") or fixed effectsonly (pred.type = "fe", the default). For details, see documentationof thetype-argument inggpredict.

mdrt.values

Indicates which values of the moderator variable should beused when plotting interaction terms (i.e.type = "int").

"minmax"

(default) minimum and maximum values (lower andupper bounds) of the moderator are used to plot the interaction betweenindependent variable and moderator(s).

"meansd"

uses themean value of the moderator as well as one standard deviation below andabove mean value to plot the effect of the moderator on the independentvariable (following the convention suggested by Cohen and Cohen andpopularized by Aiken and West (1991), i.e. using the mean, the value onestandard deviation above, and the value one standard deviation below themean as values of the moderator, seeGrace-MartinK: 3 Tips to Make Interpreting Moderation Effects Easier).

"zeromax"

is similar to the"minmax" option, however,0 is always used as minimum value for the moderator. This may beuseful for predictors that don't have an empirical zero-value, but absenceof moderation should be simulated by using 0 as minimum.

"quart"

calculates and uses the quartiles (lower, median andupper) of the moderator value.

"all"

uses all values of themoderator variable.

ri.nr

Numeric vector. Iftype = "re" and fitted model has morethan one random intercept,ri.nr indicates which random effects ofwhich random intercept (or: which list elements ofranef) will be plotted. Default isNULL, so allrandom effects will be plotted.

title

Character vector, used as plot title. By default,response_labels is called to retrieve the label ofthe dependent variable, which will be used as title. Usetitle = ""to remove title.

axis.title

Character vector of length one or two (depending on theplot function and type), used as title(s) for the x and y axis. If notspecified, a default labelling is chosen.Note: Some plot typesmay not support this argument sufficiently. In such cases, use the returnedggplot-object and add axis titles manually withlabs. Useaxis.title = "" to remove axistitles.

axis.labels

Character vector with labels for the model terms, used asaxis labels. By default,term_labels iscalled to retrieve the labels of the coefficients, which will be used asaxis labels. Useaxis.labels = "" orauto.label = FALSE touse the variable names as labels instead. Ifaxis.labels is a namedvector, axis labels (by default, the names of the model's coefficients)will be matched with the names ofaxis.label. This ensures thatlabels always match the related axis value, no matter in which wayaxis labels are sorted.

legend.title

Character vector, used as legend title for plots thathave a legend.

wrap.title

Numeric, determines how many chars of the plot title aredisplayed in one line and when a line break is inserted.

wrap.labels

Numeric, determines how many chars of the value, variableor axis labels are displayed in one line and when a line break is inserted.

axis.lim

Numeric vector of length 2, defining the range of the plotaxis. Depending on plot-type, may effect either x- or y-axis. ForMarginal Effects plots,axis.lim may also be a list of twovectors of length 2, defining axis limits for both the x and y axis.

grid.breaks

Numeric value or vector; ifgrid.breaks is asingle value, sets the distance between breaks for the axis at everygrid.breaks'th position, where a major grid line is plotted. Ifgrid.breaks is a vector, values will be used to define theaxis positions of the major grid lines.

ci.lvl

Numeric, the level of the confidence intervals (error bars).Useci.lvl = NA to remove error bars. Forstanreg-models,ci.lvl defines the (outer) probability for thecredible intervalthat is plotted (seeci). Bydefault,stanreg-models are printed with two intervals: the "inner"interval, which defaults to the 50%-CI; and the "outer" interval, whichdefaults to the 89%-CI.ci.lvl affects only the outer interval insuch cases. Seeprob.inner andprob.outer under the...-argument for more details.

se

Logical, ifTRUE, the standard errors arealso printed. If robust standard errors are required, use argumentsvcov.fun andvcov.args (seestandard_error for details).se overridesci.lvl: if notNULL, argumentsci.lvl andtransformwill be ignored. Currently,se only applies toCoefficients plots.

vcov.fun

Variance-covariance matrix used to compute uncertaintyestimates (e.g., for robust standard errors). This argument accepts acovariance matrix, a function which returns a covariance matrix, or astring which identifies the function to be used to compute the covariancematrix. Seemodel_parameters().

vcov.args

List of arguments to be passed to the function identified bythevcov.fun argument. This function is typically supplied by thesandwich orclubSandwich packages. Please refer to theirdocumentation (e.g.,?sandwich::vcovHAC) to see the list ofavailable arguments.

colors

May be a character vector of color values in hex-format, validcolor value names (seedemo("colors")) or a name of a pre-definedcolor palette. Following options are valid for thecolors argument:

  • If not specified, a default color brewer palette will be used, which is suitable for the plot style.

  • If"gs", a greyscale will be used.

  • If"bw", and plot-type is a line-plot, the plot is black/white and uses different line types to distinguish groups (seethis package-vignette).

  • Ifcolors is any valid color brewer palette name, the related palette will be used. UseRColorBrewer::display.brewer.all() to view all available palette names.

  • There are some pre-defined color palettes in this package, seesjPlot-themes for details.

  • Else specify own color values or names as vector (e.g.colors = "#00ff00" orcolors = c("firebrick", "blue")).

show.intercept

Logical, ifTRUE, the intercept of the fittedmodel is also plotted. Default isFALSE. Iftransform ="exp", please note that due to exponential transformation of estimates,the intercept in some cases is non-finite and the plot can not be created.

show.values

Logical, whether values should be plotted or not.

show.p

Logical, adds asterisks that indicate the significance level ofestimates to the value labels.

show.data

Logical, forMarginal Effects plots, also plots theraw data points.

show.legend

ForMarginal Effects plots, shows or hides thelegend.

show.zeroinf

Logical, ifTRUE, shows the zero-inflation part ofhurdle- or zero-inflated models.

value.offset

Numeric, offset for text labels to adjust their positionrelative to the dots or lines.

value.size

Numeric, indicates the size of value labels. Can be usedfor all plot types where the argumentshow.values is applicable,e.g.value.size = 4.

jitter

Numeric, between 0 and 1. Ifshow.data = TRUE, you canadd a small amount of random variation to the location of each data point.jitter then indicates the width, i.e. how much of a bin's widthwill be occupied by the jittered values.

digits

Numeric, amount of digits after decimal point when roundingestimates or values.

dot.size

Numeric, size of the dots that indicate the point estimates.

line.size

Numeric, size of the lines that indicate the error bars.

vline.color

Color of the vertical "zero effect" line. Default color isinherited from the current theme.

p.threshold

Numeric vector of length 3, indicating the treshold forannotating p-values with asterisks. Only applies ifp.style = "asterisk".

p.val

Character specifying method to be used to calculate p-values.Defaults to "profile" for glm/polr models, otherwise "wald".

p.adjust

String value, if notNULL, indicates the methodto adjust p-values. Seep.adjust for details.

grid

Logical, ifTRUE, multiple plots are plotted as gridlayout.

case

Desired target case. Labels will automatically converted into thespecified character case. Seesnakecase::to_any_case() for moredetails on this argument. By default, ifcase is not specified,it will be set to"parsed", unlessprefix.labels is not"none". Ifprefix.labels is either"label" (or"l") or"varname" (or"v") andcase is notspecified, it will be set toNULL - this is a more convenientdefault when prefixing labels.

auto.label

Logical, ifTRUE (the default),anddata is labelled,term_labels is called to retrieve the labelsof the coefficients, which will be used as predictor labels. If data isnot labelled,format_parameters()is used to create pretty labels. Ifauto.label = FALSE,original variable names and value labels (factor levels) are used.

prefix.labels

Indicates whether the value labels of categorical variablesshould be prefixed, e.g. with the variable name or variable label. Seeargumentprefix interm_labels fordetails.

bpe

ForStan-models (fitted with therstanarm- orbrms-package), the Bayesian point estimate is, by default, the medianof the posterior distribution. Usebpe to define other functions tocalculate the Bayesian point estimate.bpe needs to be a characternaming the specific function, which is passed to thefun-argument intypical_value. So,bpe = "mean" wouldcalculate the mean value of the posterior distribution.

bpe.style

ForStan-models (fitted with therstanarm- orbrms-package), the Bayesian point estimate is indicated as a small,vertical line by default. Usebpe.style = "dot" to plot a dotinstead of a line for the point estimate.

bpe.color

Character vector, indicating the color of the Bayesianpoint estimate. Settingbpe.color = NULL will inherit the colorfrom the mapped aesthetic to match it with the geom's color.

ci.style

Character vector, defining whether inner and outer intervalsfor Bayesion models are shown in boxplot-style ("whisker") or inbars with different alpha-levels ("bar").

std.response

Logical, whether the response variable will also bestandardized if standardized coefficients are requested. Setting bothstd.response = TRUE andshow.std = TRUE will behave as ifthe complete data was standardized before fitting the model.

...

Other arguments, passed down to various functions. Here is a listof supported arguments and their description in detail.

prob.inner andprob.outer

ForStan-models(fitted with therstanarm- orbrms-package) and coefficientsplot-types, you can specify numeric values between 0 and 1 forprob.inner andprob.outer, which will then be used as innerand outer probabilities for the uncertainty intervals (HDI). By default,the inner probability is 0.5 and the outer probability is 0.89 (unlessci.lvl is specified - in this case,ci.lvl is used as outerprobability).

size.inner

ForStan-models andCoefficientsplot-types, you can specify the width of the bar for the innerprobabilities. Default is0.1. Settingsize.inner = 0removes the inner probability regions.

width,alpha, andscale

Passeddown toggplot2::geom_errorbar() orgeom_density_ridges(), forforest or diagnostic plots.

width,alpha,dot.alpha,dodge andlog.y

Passeddown toplot.ggeffects forMarginal Effectsplots.

show.loess

Logical, for diagnostic plot-types"slope"and"resid", adds (or hides) a loess-smoothed line to the plot.

Marginal Effects plot-types

When plotting marginal effects,arguments are also passed down toggpredict,ggeffect orplot.ggeffects.

Case conversion of labels

For case conversion of labels (see argumentcase), argumentssep_in andsep_out will be passeddown tosnakecase::to_any_case(). This onlyapplies to automatically retrieved term labels,not ifterm labels are provided by theaxis.labels-argument.

Details

Different Plot Types

type = "std"

Plots standardized estimates. See details below.

type = "std2"

Plots standardized estimates, however,standardization follows Gelman's (2008) suggestion, rescaling theestimates by dividing them by two standard deviations instead of just one.Resulting coefficients are then directly comparable for untransformedbinary predictors.

type = "pred"

Plots estimated marginal means (or marginal effects).Simply wrapsggpredict. See alsothis package-vignette.

type = "eff"

Plots estimated marginal means (or marginal effects).Simply wrapsggeffect. See alsothis package-vignette.

type = "int"

A shortcut for marginal effects plots, whereinteraction terms are automatically detected and used asterms-argument. Furthermore, if the moderator variable (the second- and third - term in an interaction) is continuous,type = "int"automatically chooses useful values based on themdrt.values-argument,which are passed toterms. Then,ggpredictis called.type = "int" plots the interaction term that appearsfirst in the formula along the x-axis, while the second (and possiblythird) variable in an interaction is used as grouping factor(s)(moderating variable). Usetype = "pred" ortype = "eff"and specify a certain order in theterms-argument to indicatewhich variable(s) should be used as moderator. See alsothis package-vignette.

type = "slope" andtype = "resid"

Simple diagnostic-plots,where a linear model for each single predictor is plotted against theresponse variable, or the model's residuals. Additionally, a loess-smoothedline is added to the plot. The main purpose of these plots is to check whetherthe relationship between outcome (or residuals) and a predictor is roughlylinear or not. Since the plots are based on a simple linear regression withonly one model predictor at the moment, the slopes (i.e. coefficients) maydiffer from the coefficients of the complete model.

type = "diag"

ForStan-models, plots the prior versusposterior samples. Forlinear (mixed) models, plots formulticollinearity-check (Variance Inflation Factors), QQ-plots,checks for normal distribution of residuals and homoscedasticity(constant variance of residuals) are shown. Forgeneralizedlinear mixed models, returns the QQ-plot for random effects.

Standardized Estimates

Default standardization is done by completely refitting the model on thestandardized data. Hence, this approach is equal to standardizing thevariables before fitting the model, which is particularly recommended forcomplex models that include interactions or transformations (e.g.,polynomial or spline terms). Whentype = "std2", standardization ofestimates follows Gelman's (2008) suggestion, rescaling the estimates bydividing them by two standard deviations instead of just one. Resultingcoefficients are then directly comparable for untransformed binarypredictors.

Value

Depending on the plot-type,plot_model() returns aggplot-object or a list of such objects.get_model_datareturns the associated data with the plot-object as tidy data frame, or(depending on the plot-type) a list of such data frames.

References

Gelman A (2008) "Scaling regression inputs by dividing by twostandard deviations."Statistics in Medicine 27: 2865-2873.

Aiken and West (1991). Multiple Regression: Testing and Interpreting Interactions.

Examples

# prepare dataif (requireNamespace("haven")) {library(sjmisc)data(efc)efc <- to_factor(efc, c161sex, e42dep, c172code)m <- lm(neg_c_7 ~ pos_v_4 + c12hour + e42dep + c172code, data = efc)# simple forest plotplot_model(m)# grouped coefficientsplot_model(m, group.terms = c(1, 2, 3, 3, 3, 4, 4))# keep only selected terms in the model: pos_v_4, the# levels 3 and 4 of factor e42dep and levels 2 and 3 for c172codeplot_model(m, terms = c("pos_v_4", "e42dep [3,4]", "c172code [2,3]"))}# multiple plots, as returned from "diagnostic"-plot type,# can be arranged with 'plot_grid()'## Not run: p <- plot_model(m, type = "diag")plot_grid(p)## End(Not run)# plot random effectsif (require("lme4") && require("glmmTMB")) {  m <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)  plot_model(m, type = "re")  # plot marginal effects  plot_model(m, type = "pred", terms = "Days")}# plot interactions## Not run: m <- glm(  tot_sc_e ~ c161sex + c172code * neg_c_7,  data = efc,  family = poisson())# type = "int" automatically selects groups for continuous moderator# variables - see argument 'mdrt.values'. The following function call is# identical to:# plot_model(m, type = "pred", terms = c("c172code", "neg_c_7 [7,28]"))plot_model(m, type = "int")# switch moderatorplot_model(m, type = "pred", terms = c("neg_c_7", "c172code"))# same as# ggeffects::ggpredict(m, terms = c("neg_c_7", "c172code"))## End(Not run)# plot Stan-model## Not run: if (require("rstanarm")) {  data(mtcars)  m <- stan_glm(mpg ~ wt + am + cyl + gear, data = mtcars, chains = 1)  plot_model(m, bpe.style = "dot")}## End(Not run)

Forest plot of multiple regression models

Description

Plot and compare regression coefficients with confidenceintervals of multiple regression models in one plot.

Usage

plot_models(  ...,  transform = NULL,  std.est = NULL,  std.response = TRUE,  rm.terms = NULL,  title = NULL,  m.labels = NULL,  legend.title = "Dependent Variables",  legend.pval.title = "p-level",  axis.labels = NULL,  axis.title = NULL,  axis.lim = NULL,  wrap.title = 50,  wrap.labels = 25,  wrap.legend.title = 20,  grid.breaks = NULL,  dot.size = 3,  line.size = NULL,  value.size = NULL,  spacing = 0.4,  colors = "Set1",  show.values = FALSE,  show.legend = TRUE,  show.intercept = FALSE,  show.p = TRUE,  p.shape = FALSE,  p.threshold = c(0.05, 0.01, 0.001),  p.adjust = NULL,  ci.lvl = 0.95,  vcov.fun = NULL,  vcov.args = NULL,  vline.color = NULL,  digits = 2,  grid = FALSE,  auto.label = TRUE,  prefix.labels = c("none", "varname", "label"))

Arguments

...

One or more regression models, including glm's or mixed models.May also be alist with fitted models. See 'Examples'.

transform

A character vector, naming a function that will be appliedon estimates and confidence intervals. By default,transform willautomatically use"exp" as transformation for applicable classes ofmodel (e.g. logistic or poisson regression). Estimates of linearmodels remain untransformed. UseNULL if you want the raw,non-transformed estimates.

std.est

Choose whether standardized coefficients should be usedfor plotting. Default is no standardization (std.est = NULL).May be"std" for standardized beta values or"std2", wherestandardization is done by rescaling estimates by dividing them by two sd.

std.response

Logical, whether the response variable will also bestandardized if standardized coefficients are requested. Setting bothstd.response = TRUE andshow.std = TRUE will behave as ifthe complete data was standardized before fitting the model.

rm.terms

Character vector with names that indicate which terms shouldbe removed from the plot. Counterpart toterms.rm.terms ="t_name" would remove the termt_name. Default isNULL, i.e.all terms are used. For factors, levels that should be removed from the plotneed to be explicitely indicated in square brackets, and match the model'scoefficient names, e.g.rm.terms = "t_name [2,3]" would remove the terms"t_name2" and"t_name3" (assuming that the variablet_namewas categorical and has at least the factor levels2 and3).Another example for theiris dataset would berm.terms = "Species [versicolor,virginica]". Note that therm.terms-argument does not apply toMarginal Effects plots.

title

Character vector, used as plot title. By default,response_labels is called to retrieve the label ofthe dependent variable, which will be used as title. Usetitle = ""to remove title.

m.labels

Character vector, used to indicate the different modelsin the plot's legend. If not specified, the labels of the dependentvariables for each model are used.

legend.title

Character vector, used as legend title for plots thathave a legend.

legend.pval.title

Character vector, used as title of the plot legend thatindicates the p-values. Default is"p-level". Only applies ifp.shape = TRUE.

axis.labels

Character vector with labels for the model terms, used asaxis labels. By default,term_labels iscalled to retrieve the labels of the coefficients, which will be used asaxis labels. Useaxis.labels = "" orauto.label = FALSE touse the variable names as labels instead. Ifaxis.labels is a namedvector, axis labels (by default, the names of the model's coefficients)will be matched with the names ofaxis.label. This ensures thatlabels always match the related axis value, no matter in which wayaxis labels are sorted.

axis.title

Character vector of length one or two (depending on theplot function and type), used as title(s) for the x and y axis. If notspecified, a default labelling is chosen.Note: Some plot typesmay not support this argument sufficiently. In such cases, use the returnedggplot-object and add axis titles manually withlabs. Useaxis.title = "" to remove axistitles.

axis.lim

Numeric vector of length 2, defining the range of the plotaxis. Depending on plot-type, may effect either x- or y-axis. ForMarginal Effects plots,axis.lim may also be a list of twovectors of length 2, defining axis limits for both the x and y axis.

wrap.title

Numeric, determines how many chars of the plot title aredisplayed in one line and when a line break is inserted.

wrap.labels

Numeric, determines how many chars of the value, variableor axis labels are displayed in one line and when a line break is inserted.

wrap.legend.title

numeric, determines how many chars of the legend's titleare displayed in one line and when a line break is inserted.

grid.breaks

Numeric value or vector; ifgrid.breaks is asingle value, sets the distance between breaks for the axis at everygrid.breaks'th position, where a major grid line is plotted. Ifgrid.breaks is a vector, values will be used to define theaxis positions of the major grid lines.

dot.size

Numeric, size of the dots that indicate the point estimates.

line.size

Numeric, size of the lines that indicate the error bars.

value.size

Numeric, indicates the size of value labels. Can be usedfor all plot types where the argumentshow.values is applicable,e.g.value.size = 4.

spacing

Numeric, spacing between the dots and error bars of theplotted fitted models. Default is 0.3.

colors

May be a character vector of color values in hex-format, validcolor value names (seedemo("colors")) or a name of a pre-definedcolor palette. Following options are valid for thecolors argument:

  • If not specified, a default color brewer palette will be used, which is suitable for the plot style.

  • If"gs", a greyscale will be used.

  • If"bw", and plot-type is a line-plot, the plot is black/white and uses different line types to distinguish groups (seethis package-vignette).

  • Ifcolors is any valid color brewer palette name, the related palette will be used. UseRColorBrewer::display.brewer.all() to view all available palette names.

  • There are some pre-defined color palettes in this package, seesjPlot-themes for details.

  • Else specify own color values or names as vector (e.g.colors = "#00ff00" orcolors = c("firebrick", "blue")).

show.values

Logical, whether values should be plotted or not.

show.legend

ForMarginal Effects plots, shows or hides thelegend.

show.intercept

Logical, ifTRUE, the intercept of the fittedmodel is also plotted. Default isFALSE. Iftransform ="exp", please note that due to exponential transformation of estimates,the intercept in some cases is non-finite and the plot can not be created.

show.p

Logical, adds asterisks that indicate the significance level ofestimates to the value labels.

p.shape

Logical, ifTRUE, significant levels are distinguished bydifferent point shapes and a related legend is plotted. DefaultisFALSE.

p.threshold

Numeric vector of length 3, indicating the treshold forannotating p-values with asterisks. Only applies ifp.style = "asterisk".

p.adjust

String value, if notNULL, indicates the methodto adjust p-values. Seep.adjust for details.

ci.lvl

Numeric, the level of the confidence intervals (error bars).Useci.lvl = NA to remove error bars. Forstanreg-models,ci.lvl defines the (outer) probability for thecredible intervalthat is plotted (seeci). Bydefault,stanreg-models are printed with two intervals: the "inner"interval, which defaults to the 50%-CI; and the "outer" interval, whichdefaults to the 89%-CI.ci.lvl affects only the outer interval insuch cases. Seeprob.inner andprob.outer under the...-argument for more details.

vcov.fun

Variance-covariance matrix used to compute uncertaintyestimates (e.g., for robust standard errors). This argument accepts acovariance matrix, a function which returns a covariance matrix, or astring which identifies the function to be used to compute the covariancematrix. Seemodel_parameters().

vcov.args

List of arguments to be passed to the function identified bythevcov.fun argument. This function is typically supplied by thesandwich orclubSandwich packages. Please refer to theirdocumentation (e.g.,?sandwich::vcovHAC) to see the list ofavailable arguments.

vline.color

Color of the vertical "zero effect" line. Default color isinherited from the current theme.

digits

Numeric, amount of digits after decimal point when roundingestimates or values.

grid

Logical, ifTRUE, multiple plots are plotted as gridlayout.

auto.label

Logical, ifTRUE (the default),anddata is labelled,term_labels is called to retrieve the labelsof the coefficients, which will be used as predictor labels. If data isnot labelled,format_parameters()is used to create pretty labels. Ifauto.label = FALSE,original variable names and value labels (factor levels) are used.

prefix.labels

Indicates whether the value labels of categorical variablesshould be prefixed, e.g. with the variable name or variable label. Seeargumentprefix interm_labels fordetails.

Value

A ggplot-object.

Examples

data(efc)# fit three modelsfit1 <- lm(barthtot ~ c160age + c12hour + c161sex + c172code, data = efc)fit2 <- lm(neg_c_7 ~ c160age + c12hour + c161sex + c172code, data = efc)fit3 <- lm(tot_sc_e ~ c160age + c12hour + c161sex + c172code, data = efc)# plot multiple modelsplot_models(fit1, fit2, fit3, grid = TRUE)# plot multiple models with legend labels and# point shapes instead of value labelsplot_models(  fit1, fit2, fit3,  axis.labels = c(    "Carer's Age", "Hours of Care", "Carer's Sex", "Educational Status"  ),  m.labels = c("Barthel Index", "Negative Impact", "Services used"),  show.values = FALSE, show.p = FALSE, p.shape = TRUE)## Not run: # plot multiple models from nested lists argumentall.models <- list()all.models[[1]] <- fit1all.models[[2]] <- fit2all.models[[3]] <- fit3plot_models(all.models)# plot multiple models with different predictors (stepwise inclusion),# standardized estimatesfit1 <- lm(mpg ~ wt + cyl + disp + gear, data = mtcars)fit2 <- update(fit1, . ~ . + hp)fit3 <- update(fit2, . ~ . + am)plot_models(fit1, fit2, fit3, std.est = "std2")## End(Not run)

Plot predicted values and their residuals

Description

This function plots observed and predicted values of the responseof linear (mixed) models for each coefficient and highlights theobserved values according to their distance (residuals) to thepredicted values. This allows to investigate how well actual andpredicted values of the outcome fit across the predictor variables.

Usage

plot_residuals(  fit,  geom.size = 2,  remove.estimates = NULL,  show.lines = TRUE,  show.resid = TRUE,  show.pred = TRUE,  show.ci = FALSE)

Arguments

fit

Fitted linear (mixed) regression model (including objects of classgls orplm).

geom.size

size resp. width of the geoms (bar width, line thickness or point size,depending on plot type and function). Note that bar and bin widths mostlyneed smaller values than dot sizes.

remove.estimates

Numeric vector with indices (order equals to row index ofcoef(fit))or character vector with coefficient names that indicate which estimates should be removedfrom the table output. The first estimate is the intercept, followed by the model predictors.The intercept cannot be removed from the table output!remove.estimates = c(2:4)would remove the 2nd to the 4th estimate (1st to 3rd predictor after intercept) from the output.remove.estimates = "est_name" would remove the estimateest_name. DefaultisNULL, i.e. all estimates are printed.

show.lines

Logical, ifTRUE, a line connecting predicted andresidual values is plotted. Set this argument toFALSE, ifplot-building is too time consuming.

show.resid

Logical, ifTRUE, residual values are plotted.

show.pred

Logical, ifTRUE, predicted values are plotted.

show.ci

Logical, ifTRUE), adds notches to the box plot, which areused to compare groups; if the notches of two boxes do not overlap,medians are considered to be significantly different.

Value

A ggplot-object.

Note

The actual (observed) values have a coloured fill, while the predictedvalues have a solid outline without filling.

Examples

data(efc)# fit modelfit <- lm(neg_c_7 ~ c12hour + e17age + e42dep, data = efc)# plot residuals for all independent variablesplot_residuals(fit)# remove some independent variables from outputplot_residuals(fit, remove.estimates = c("e17age", "e42dep"))

Plot (grouped) scatter plots

Description

Display scatter plot of two variables. Adding a grouping variable tothe scatter plot is possible. Furthermore, fitted lines can be addedfor each group as well as for the overall plot.

Usage

plot_scatter(  data,  x,  y,  grp,  title = "",  legend.title = NULL,  legend.labels = NULL,  dot.labels = NULL,  axis.titles = NULL,  dot.size = 1.5,  label.size = 3,  colors = "metro",  fit.line = NULL,  fit.grps = NULL,  show.rug = FALSE,  show.legend = TRUE,  show.ci = FALSE,  wrap.title = 50,  wrap.legend.title = 20,  wrap.legend.labels = 20,  jitter = 0.05,  emph.dots = FALSE,  grid = FALSE)

Arguments

data

A data frame, or a grouped data frame.

x

Name of the variable for the x-axis.

y

Name of the variable for the y-axis.

grp

Optional, name of the grouping-variable. If not missing, thescatter plot will be grouped. See 'Examples'.

title

Character vector, used as plot title. By default,response_labels is called to retrieve the label ofthe dependent variable, which will be used as title. Usetitle = ""to remove title.

legend.title

Character vector, used as legend title for plots thathave a legend.

legend.labels

character vector with labels for the guide/legend.

dot.labels

Character vector with names for each coordinate pair givenbyx andy, so text labels are added to the plot.Must be of same length asx andy.Ifdot.labels has a different length, data points will be trimmedto matchdot.labels. Ifdot.labels = NULL (default),no labels are printed.

axis.titles

character vector of length one or two, defining the title(s)for the x-axis and y-axis.

dot.size

Numeric, size of the dots that indicate the point estimates.

label.size

Size of text labels if argumentdot.labels is used.

colors

May be a character vector of color values in hex-format, validcolor value names (seedemo("colors")) or a name of a pre-definedcolor palette. Following options are valid for thecolors argument:

  • If not specified, a default color brewer palette will be used, which is suitable for the plot style.

  • If"gs", a greyscale will be used.

  • If"bw", and plot-type is a line-plot, the plot is black/white and uses different line types to distinguish groups (seethis package-vignette).

  • Ifcolors is any valid color brewer palette name, the related palette will be used. UseRColorBrewer::display.brewer.all() to view all available palette names.

  • There are some pre-defined color palettes in this package, seesjPlot-themes for details.

  • Else specify own color values or names as vector (e.g.colors = "#00ff00" orcolors = c("firebrick", "blue")).

fit.line,fit.grps

Specifies the method to add a fitted line accrossthe data points. Possible values are for instance"lm","glm","loess" or"auto". IfNULL, no line is plotted.fit.line adds a fitted line for the complete data, whilefit.grpsadds a fitted line for each subgroup ofgrp.

show.rug

Logical, ifTRUE, a marginal rug plot is displayedin the graph.

show.legend

ForMarginal Effects plots, shows or hides thelegend.

show.ci

Logical, ifTRUE), adds notches to the box plot, which areused to compare groups; if the notches of two boxes do not overlap,medians are considered to be significantly different.

wrap.title

Numeric, determines how many chars of the plot title aredisplayed in one line and when a line break is inserted.

wrap.legend.title

numeric, determines how many chars of the legend's titleare displayed in one line and when a line break is inserted.

wrap.legend.labels

numeric, determines how many chars of the legend labels aredisplayed in one line and when a line break is inserted.

jitter

Numeric, between 0 and 1. Ifshow.data = TRUE, you canadd a small amount of random variation to the location of each data point.jitter then indicates the width, i.e. how much of a bin's widthwill be occupied by the jittered values.

emph.dots

Logical, ifTRUE, overlapping points at same coordinateswill be becomme larger, so point size indicates amount of overlapping.

grid

Logical, ifTRUE, multiple plots are plotted as gridlayout.

Value

A ggplot-object. For grouped data frames, a list of ggplot-objects foreach group in the data.

Examples

# load sample datelibrary(sjmisc)library(sjlabelled)data(efc)# simple scatter plotplot_scatter(efc, e16sex, neg_c_7)# simple scatter plot, increased jitteringplot_scatter(efc, e16sex, neg_c_7, jitter = .4)# grouped scatter plotplot_scatter(efc, c160age, e17age, e42dep)# grouped scatter plot with marginal rug plot# and add fitted line for complete dataplot_scatter(  efc, c12hour, c160age, c172code,  show.rug = TRUE, fit.line = "lm")# grouped scatter plot with marginal rug plot# and add fitted line for each groupplot_scatter(  efc, c12hour, c160age, c172code,  show.rug = TRUE, fit.grps = "loess",  grid = TRUE)

Plot stacked proportional bars

Description

Plot items (variables) of a scale as stacked proportional bars. Thisfunction is useful when several items with identical scale/categoroiesshould be plotted to compare the distribution of answers.

Usage

plot_stackfrq(  items,  title = NULL,  legend.title = NULL,  legend.labels = NULL,  axis.titles = NULL,  axis.labels = NULL,  weight.by = NULL,  sort.frq = NULL,  wrap.title = 50,  wrap.labels = 30,  wrap.legend.title = 30,  wrap.legend.labels = 28,  geom.size = 0.5,  geom.colors = "Blues",  show.prc = TRUE,  show.n = FALSE,  show.total = TRUE,  show.axis.prc = TRUE,  show.legend = TRUE,  grid.breaks = 0.2,  expand.grid = FALSE,  digits = 1,  vjust = "center",  coord.flip = TRUE)

Arguments

items

Data frame, or a grouped data frame, with each column representing one item.

title

character vector, used as plot title. Depending on plot type and function,will be set automatically. Iftitle = "", no title is printed.For effect-plots, may also be a character vector of length > 1,to define titles for each sub-plot or facet.

legend.title

character vector, used as title for the plot legend.

legend.labels

character vector with labels for the guide/legend.

axis.titles

character vector of length one or two, defining the title(s)for the x-axis and y-axis.

axis.labels

character vector with labels used as axis labels. Optionalargument, since in most cases, axis labels are set automatically.

weight.by

Vector of weights that will be applied to weight all cases.Must be a vector of same length as the input vector. Default isNULL, so no weights are used.

sort.frq

Indicates whether theitems should be ordered byby highest count of first or last category ofitems.

"first.asc"

to order ascending by lowest count of first category,

"first.desc"

to order descending by lowest count of first category,

"last.asc"

to order ascending by lowest count of last category,

"last.desc"

to order descending by lowest count of last category,

NULL

(default) for no sorting.

wrap.title

numeric, determines how many chars of the plot title are displayed inone line and when a line break is inserted.

wrap.labels

numeric, determines how many chars of the value, variable or axislabels are displayed in one line and when a line break is inserted.

wrap.legend.title

numeric, determines how many chars of the legend's titleare displayed in one line and when a line break is inserted.

wrap.legend.labels

numeric, determines how many chars of the legend labels aredisplayed in one line and when a line break is inserted.

geom.size

size resp. width of the geoms (bar width, line thickness or point size,depending on plot type and function). Note that bar and bin widths mostlyneed smaller values than dot sizes.

geom.colors

user defined color for geoms. See 'Details' inplot_grpfrq.

show.prc

Logical, whether percentage values should be plotted or not.

show.n

Logical, whether count values hould be plotted or not.

show.total

logical, ifTRUE, adds total number of cases for eachgroup or category to the labels.

show.axis.prc

Logical, ifTRUE (default), the percentage values at the x-axis are shown.

show.legend

logical, ifTRUE, and depending on plot type andfunction, a legend is added to the plot.

grid.breaks

numeric; sets the distance between breaks for the axis,i.e. at everygrid.breaks'th position a major grid is being printed.

expand.grid

logical, ifTRUE, the plot grid is expanded, i.e. there is a small margin betweenaxes and plotting region. Default isFALSE.

digits

Numeric, amount of digits after decimal point when roundingestimates or values.

vjust

character vector, indicating the vertical position of valuelabels. Allowed are same values as forvjust aesthetics fromggplot2: "left", "center", "right", "bottom", "middle", "top" andnew options like "inward" and "outward", which align text towards andaway from the center of the plot respectively.

coord.flip

logical, ifTRUE, the x and y axis are swapped.

Value

A ggplot-object.

Examples

# Data from the EUROFAMCARE sample datasetlibrary(sjmisc)data(efc)# recveive first item of COPE-index scalestart <- which(colnames(efc) == "c82cop1")# recveive first item of COPE-index scaleend <- which(colnames(efc) == "c90cop9")# auto-detection of labelsplot_stackfrq(efc[, start:end])# works on grouped data frames as welllibrary(dplyr)efc |>  group_by(c161sex) |>  select(start:end) |>  plot_stackfrq()

Plot contingency tables

Description

Plot proportional crosstables (contingency tables) of two variables as ggplot diagram.

Usage

plot_xtab(  x,  grp,  type = c("bar", "line"),  margin = c("col", "cell", "row"),  bar.pos = c("dodge", "stack"),  title = "",  title.wtd.suffix = NULL,  axis.titles = NULL,  axis.labels = NULL,  legend.title = NULL,  legend.labels = NULL,  weight.by = NULL,  rev.order = FALSE,  show.values = TRUE,  show.n = TRUE,  show.prc = TRUE,  show.total = TRUE,  show.legend = TRUE,  show.summary = FALSE,  summary.pos = "r",  drop.empty = TRUE,  string.total = "Total",  wrap.title = 50,  wrap.labels = 15,  wrap.legend.title = 20,  wrap.legend.labels = 20,  geom.size = 0.7,  geom.spacing = 0.1,  geom.colors = "Paired",  dot.size = 3,  smooth.lines = FALSE,  grid.breaks = 0.2,  expand.grid = FALSE,  ylim = NULL,  vjust = "bottom",  hjust = "center",  y.offset = NULL,  coord.flip = FALSE)

Arguments

x

A vector of values (variable) describing the bars which make up the plot.

grp

Grouping variable of same length asx, wherexis grouped into the categories represented bygrp.

type

Plot type. may be either"bar" (default) for bar charts,or"line" for line diagram.

margin

Indicates which data of the proportional table should be plotted. Use"row" forcalculating row percentages,"col" for column percentages and"cell" for cell percentages.Ifmargin = "col", an additional bar with the total sum of each columncan be added to the plot (seeshow.total).

bar.pos

Indicates whether bars should be positioned side-by-side (default),or stacked (bar.pos = "stack"). May be abbreviated.

title

character vector, used as plot title. Depending on plot type and function,will be set automatically. Iftitle = "", no title is printed.For effect-plots, may also be a character vector of length > 1,to define titles for each sub-plot or facet.

title.wtd.suffix

Suffix (as string) for the title, ifweight.by is specified,e.g.title.wtd.suffix=" (weighted)". Default isNULL, sotitle will not have a suffix when cases are weighted.

axis.titles

character vector of length one or two, defining the title(s)for the x-axis and y-axis.

axis.labels

character vector with labels used as axis labels. Optionalargument, since in most cases, axis labels are set automatically.

legend.title

character vector, used as title for the plot legend.

legend.labels

character vector with labels for the guide/legend.

weight.by

Vector of weights that will be applied to weight all cases.Must be a vector of same length as the input vector. Default isNULL, so no weights are used.

rev.order

Logical, ifTRUE, order of categories (groups) is reversed.

show.values

Logical, whether values should be plotted or not.

show.n

logical, ifTRUE, adds total number of cases for eachgroup or category to the labels.

show.prc

logical, ifTRUE (default), percentage values are plotted to each barIfFALSE, percentage values are removed.

show.total

Whenmargin = "col", an additional barwith the sum within each category and it's percentages will be addedto each category.

show.legend

logical, ifTRUE, and depending on plot type andfunction, a legend is added to the plot.

show.summary

logical, ifTRUE (default), a summary with chi-squaredstatistics (seechisq.test), Cramer's V or Phi-value etc.is shown. If a cell contains expected values lower than five (or lower than 10if df is 1), the Fisher's exact test (seefisher.test) iscomputed instead of chi-squared test. If the table's matrix is largerthan 2x2, Fisher's exact test with Monte Carlo simulation is computed.

summary.pos

position of the model summary which is printed whenshow.summaryisTRUE. Default is"r", i.e. it's printed to the upper right corner.Use"l" for upper left corner.

drop.empty

Logical, ifTRUE and the variable's values are labeled, values / factorlevels with no occurrence in the data are omitted from the output. IfFALSE,labeled values that have no observations are still printed in the table (with frequency0).

string.total

String for the legend label when a total-column is added. Only appliesifshow.total = TRUE. Default is"Total".

wrap.title

numeric, determines how many chars of the plot title are displayed inone line and when a line break is inserted.

wrap.labels

numeric, determines how many chars of the value, variable or axislabels are displayed in one line and when a line break is inserted.

wrap.legend.title

numeric, determines how many chars of the legend's titleare displayed in one line and when a line break is inserted.

wrap.legend.labels

numeric, determines how many chars of the legend labels aredisplayed in one line and when a line break is inserted.

geom.size

size resp. width of the geoms (bar width, line thickness or point size,depending on plot type and function). Note that bar and bin widths mostlyneed smaller values than dot sizes.

geom.spacing

the spacing between geoms (i.e. bar spacing)

geom.colors

user defined color for geoms. See 'Details' inplot_grpfrq.

dot.size

Dot size, only applies, when argumenttype = "line".

smooth.lines

prints a smooth line curve. Only applies, when argumenttype = "line".

grid.breaks

numeric; sets the distance between breaks for the axis,i.e. at everygrid.breaks'th position a major grid is being printed.

expand.grid

logical, ifTRUE, the plot grid is expanded, i.e. there is a small margin betweenaxes and plotting region. Default isFALSE.

ylim

numeric vector of length two, defining lower and upper axis limitsof the y scale. By default, this argument is set toNULL, i.e. they-axis fits to the required range of the data.

vjust

character vector, indicating the vertical position of valuelabels. Allowed are same values as forvjust aesthetics fromggplot2: "left", "center", "right", "bottom", "middle", "top" andnew options like "inward" and "outward", which align text towards andaway from the center of the plot respectively.

hjust

character vector, indicating the horizontal position of valuelabels. Allowed are same values as forvjust aesthetics fromggplot2: "left", "center", "right", "bottom", "middle", "top" andnew options like "inward" and "outward", which align text towards andaway from the center of the plot respectively.

y.offset

numeric, offset for text labels when their alignment is adjustedto the top/bottom of the geom (seehjust andvjust).

coord.flip

logical, ifTRUE, the x and y axis are swapped.

Value

A ggplot-object.

Examples

# create 4-category-itemsgrp <- sample(1:4, 100, replace = TRUE)# create 3-category-itemsx <- sample(1:3, 100, replace = TRUE)# plot "cross tablulation" of x and grpplot_xtab(x, grp)# plot "cross tablulation" of x and y, including labelsplot_xtab(x, grp, axis.labels = c("low", "mid", "high"),         legend.labels = c("Grp 1", "Grp 2", "Grp 3", "Grp 4"))# plot "cross tablulation" of x and grp# as stacked proportional barsplot_xtab(x, grp, margin = "row", bar.pos = "stack",         show.summary = TRUE, coord.flip = TRUE)# example with vertical labelslibrary(sjmisc)library(sjlabelled)data(efc)sjPlot::set_theme(geom.label.angle = 90)plot_xtab(efc$e42dep, efc$e16sex, vjust = "center", hjust = "bottom")# grouped bars with EUROFAMCARE sample dataset# dataset was importet from an SPSS-file,# see ?sjmisc::read_spssdata(efc)efc.val <- get_labels(efc)efc.var <- get_label(efc)plot_xtab(efc$e42dep, efc$e16sex, title = efc.var['e42dep'],         axis.labels = efc.val[['e42dep']], legend.title = efc.var['e16sex'],         legend.labels = efc.val[['e16sex']])plot_xtab(efc$e16sex, efc$e42dep, title = efc.var['e16sex'],         axis.labels = efc.val[['e16sex']], legend.title = efc.var['e42dep'],         legend.labels = efc.val[['e42dep']])# -------------------------------# auto-detection of labels works here# so no need to specify labels. For# title-auto-detection, use NULL# -------------------------------plot_xtab(efc$e16sex, efc$e42dep, title = NULL)plot_xtab(efc$e16sex, efc$e42dep, margin = "row",         bar.pos = "stack", coord.flip = TRUE)

Save ggplot-figure for print publication

Description

Convenient function to save the last ggplot-figure inhigh quality for publication.

Usage

save_plot(  filename,  fig = ggplot2::last_plot(),  width = 12,  height = 9,  dpi = 300,  theme = ggplot2::theme_get(),  label.color = "black",  label.size = 2.4,  axis.textsize = 0.8,  axis.titlesize = 0.75,  legend.textsize = 0.6,  legend.titlesize = 0.65,  legend.itemsize = 0.5)

Arguments

filename

Name of the output file; filename must end with oneof the following accepted file types: ".png", ".jpg", ".svg" or ".tif".

fig

The plot that should be saved. By default, the last plot is saved.

width

Width of the figure, in centimetres.

height

Height of the figure, in centimetres.

dpi

Resolution in dpi (dots per inch). Ignored for vector formats, such as ".svg".

theme

The default theme to use when saving the plot.

label.color

Color value for labels (axis, plot, etc.).

label.size

Fontsize of value labels inside plot area.

axis.textsize

Fontsize of axis labels.

axis.titlesize

Fontsize of axis titles.

legend.textsize

Fontsize of legend labels.

legend.titlesize

Fontsize of legend title.

legend.itemsize

Size of legend's item (legend key), in centimetres.

Note

This is a convenient function with some default settings that shouldcome close to most of the needs for fontsize and scaling in figureswhen saving them for printing or publishing. It uses cairographicsanti-aliasing (seepng).

For adjusting plot appearance, see alsosjPlot-themes.


Set global theme options for sjp-functions

Description

Set global theme options for sjp-functions.

Usage

set_theme(  base = ggplot2::theme_grey(),  theme.font = NULL,  title.color = "black",  title.size = 1.2,  title.align = "left",  title.vjust = NULL,  geom.outline.color = NULL,  geom.outline.size = 0,  geom.boxoutline.size = 0.5,  geom.boxoutline.color = "black",  geom.alpha = 1,  geom.linetype = 1,  geom.errorbar.size = 0.7,  geom.errorbar.linetype = 1,  geom.label.color = NULL,  geom.label.size = 4,  geom.label.alpha = 1,  geom.label.angle = 0,  axis.title.color = "grey30",  axis.title.size = 1.1,  axis.title.x.vjust = NULL,  axis.title.y.vjust = NULL,  axis.angle.x = 0,  axis.angle.y = 0,  axis.angle = NULL,  axis.textcolor.x = "grey30",  axis.textcolor.y = "grey30",  axis.textcolor = NULL,  axis.linecolor.x = NULL,  axis.linecolor.y = NULL,  axis.linecolor = NULL,  axis.line.size = 0.5,  axis.textsize.x = 1,  axis.textsize.y = 1,  axis.textsize = NULL,  axis.tickslen = NULL,  axis.tickscol = NULL,  axis.ticksmar = NULL,  axis.ticksize.x = NULL,  axis.ticksize.y = NULL,  panel.backcol = NULL,  panel.bordercol = NULL,  panel.col = NULL,  panel.major.gridcol = NULL,  panel.minor.gridcol = NULL,  panel.gridcol = NULL,  panel.gridcol.x = NULL,  panel.gridcol.y = NULL,  panel.major.linetype = 1,  panel.minor.linetype = 1,  plot.backcol = NULL,  plot.bordercol = NULL,  plot.col = NULL,  plot.margins = NULL,  legend.pos = "right",  legend.just = NULL,  legend.inside = FALSE,  legend.size = 1,  legend.color = "black",  legend.title.size = 1,  legend.title.color = "black",  legend.title.face = "bold",  legend.backgroundcol = "white",  legend.bordercol = "white",  legend.item.size = NULL,  legend.item.backcol = "grey90",  legend.item.bordercol = "white")

Arguments

base

base theme where theme is built on. By default, allmetrics fromtheme_gray() are used. See 'Details'.

theme.font

base font family for the plot.

title.color

Color of plot title. Default is"black".

title.size

size of plot title. Default is 1.3.

title.align

alignment of plot title. Must be one of"left" (default),"center" or"right". You may use initial letter only.

title.vjust

numeric, vertical adjustment for plot title.

geom.outline.color

Color of geom outline. Only applies, ifgeom.outline.sizeis larger than 0.

geom.outline.size

size of bar outlines. Default is 0.1. Usesize of0 to remove geom outline.

geom.boxoutline.size

size of outlines and median bar especially for boxplots.Default is 0.5. Use size of0 to remove boxplot outline.

geom.boxoutline.color

Color of outlines and median bar especially for boxplots.Only applies, ifgeom.boxoutline.size is larger than 0.

geom.alpha

specifies the transparancy (alpha value) of geoms

geom.linetype

linetype of line geoms. Default is1 (solid line).

geom.errorbar.size

size (thickness) of error bars. Default is0.8

geom.errorbar.linetype

linetype of error bars. Default is1 (solid line).

geom.label.color

Color of geom's value and annotation labels

geom.label.size

size of geom's value and annotation labels

geom.label.alpha

alpha level of geom's value and annotation labels

geom.label.angle

angle of geom's value and annotation labels

axis.title.color

Color of x- and y-axis title labels

axis.title.size

size of x- and y-axis title labels

axis.title.x.vjust

numeric, vertical adjustment of x-axis-title.

axis.title.y.vjust

numeric, vertical adjustment of y-axis-title.

axis.angle.x

angle for x-axis labels

axis.angle.y

angle for y-axis labels

axis.angle

angle for x- and y-axis labels. If set, overrides bothaxis.angle.x andaxis.angle.y

axis.textcolor.x

Color for x-axis labels. If not specified, a default dark graycolor palette will be used for the labels.

axis.textcolor.y

Color for y-axis labels. If not specified, a default dark graycolor palette will be used for the labels.

axis.textcolor

Color for both x- and y-axis labels.If set, overrides bothaxis.textcolor.x andaxis.textcolor.y

axis.linecolor.x

Color of x-axis border

axis.linecolor.y

Color of y-axis border

axis.linecolor

Color for both x- and y-axis borders.If set, overrides bothaxis.linecolor.x andaxis.linecolor.y.

axis.line.size

size (thickness) of axis lines. Only affected, ifaxis.linecoloris set.

axis.textsize.x

size of x-axis labels

axis.textsize.y

size of y-axis labels

axis.textsize

size for both x- and y-axis labels.If set, overrides bothaxis.textsize.x andaxis.textsize.y.

axis.tickslen

length of axis tick marks

axis.tickscol

Color of axis tick marks

axis.ticksmar

margin between axis labels and tick marks

axis.ticksize.x

size of tick marks at x-axis.

axis.ticksize.y

size of tick marks at y-axis.

panel.backcol

Color of the diagram's background

panel.bordercol

Color of whole diagram border (panel border)

panel.col

Color of both diagram's border and background.If set, overrides bothpanel.bordercol andpanel.backcol.

panel.major.gridcol

Color of the major grid lines of the diagram background

panel.minor.gridcol

Color of the minor grid lines of the diagram background

panel.gridcol

Color for both minor and major grid lines of the diagram background.If set, overrides bothpanel.major.gridcol andpanel.minor.gridcol.

panel.gridcol.x

Seepanel.gridcol.

panel.gridcol.y

Seepanel.gridcol.

panel.major.linetype

line type for major grid lines

panel.minor.linetype

line type for minor grid lines

plot.backcol

Color of the plot's background

plot.bordercol

Color of whole plot's border (panel border)

plot.col

Color of both plot's region border and background.If set, overrides bothplot.backcol andplot.bordercol.

plot.margins

numeric vector of length 4, indicating the top, right,bottom and left margin of the plot region.

legend.pos

position of the legend, if a legend is drawn.

legend outside plot

Use"bottom","top","left" or"right"to position the legend above, below, on the left or right sideof the diagram. Right positioning is default.

legend inside plot

Iflegend.inside = TRUE, legend can be placed insideplot. Use"top left","top right","bottom left"and"bottom right" to position legend in any of these corners,or a two-element numeric vector with values from 0-1. See alsolegend.inside.

legend.just

justification of legend, relative to its position ("center" ortwo-element numeric vector with values from 0-1. By default (outside legend),justification is centered. If legend is inside and justification not specified,legend justification is set according to legend position.

legend.inside

logical, useTRUE to put legend inside the plotting area. Seelegend.pos.

legend.size

text size of the legend. Default is 1. Relative size, sorecommended values are from 0.3 to 2.5

legend.color

Color of the legend labels

legend.title.size

text size of the legend title

legend.title.color

Color of the legend title

legend.title.face

font face of the legend title. By default,"bold" face is used.

legend.backgroundcol

fill color of the legend's background. Default is"white", so no visible background is drawn.

legend.bordercol

Color of the legend's border. Default is"white", so no visible border is drawn.

legend.item.size

size of legend's item (legend key), in centimetres.

legend.item.backcol

fill color of the legend's item-background. Default is"grey90".

legend.item.bordercol

Color of the legend's item-border. Default is"white".

Value

The customized theme object, orNULL, if a ggplot-theme was used.

See Also

sjPlot-themes

Examples

## Not run: library(sjmisc)data(efc)# set sjPlot-defaults, a slightly modification# of the ggplot base themeset_theme()# legends of all plots insideset_theme(legend.pos = "top left", legend.inside = TRUE)plot_xtab(efc$e42dep, efc$e16sex)# Use classic-theme. you may need to# load the ggplot2-library.library(ggplot2)set_theme(base = theme_classic())plot_frq(efc$e42dep)# adjust value labelsset_theme(  geom.label.size = 3.5,  geom.label.color = "#3366cc",  geom.label.angle = 90)# hjust-aes needs adjustment for thisupdate_geom_defaults('text', list(hjust = -0.1))plot_xtab(efc$e42dep, efc$e16sex, vjust = "center", hjust = "center")# Create own theme based on classic-themeset_theme(  base = theme_classic(), axis.linecolor = "grey50",  axis.textcolor = "#6699cc")plot_frq(efc$e42dep)## End(Not run)

Modify plot appearance

Description

Set default plot themes, use pre-defined color scales or modifyplot or table appearance.

Usage

theme_sjplot(base_size = 12, base_family = "")theme_sjplot2(base_size = 12, base_family = "")theme_blank(base_size = 12, base_family = "")theme_538(base_size = 12, base_family = "")font_size(  title,  axis_title.x,  axis_title.y,  labels.x,  labels.y,  offset.x,  offset.y,  base.theme)label_angle(angle.x, angle.y, base.theme)legend_style(inside, pos, justify, base.theme)scale_color_sjplot(palette = "metro", discrete = TRUE, reverse = FALSE, ...)scale_fill_sjplot(palette = "metro", discrete = TRUE, reverse = FALSE, ...)sjplot_pal(palette = "metro", n = NULL)show_sjplot_pals()css_theme(css.theme = "regression")

Arguments

base_size

Base font size.

base_family

Base font family.

title

Font size for plot titles.

axis_title.x

Font size for x-axis titles.

axis_title.y

Font size for y-axis titles.

labels.x

Font size for x-axis labels.

labels.y

Font size for y-axis labels.

offset.x

Offset for x-axis titles.

offset.y

Offset for y-axis titles.

base.theme

Optional ggplot-theme-object, which is needed in case multiplefunctions should be combined, e.g.theme_sjplot() + label_angle().In such cases, uselabel_angle(base.theme = theme_sjplot()).

angle.x

Angle for x-axis labels.

angle.y

Angle for y-axis labels.

inside

Logical, useTRUE to put legend inside the plotting area.See alsopos.

pos

Position of the legend, if a legend is drawn.

Legend outside plot

Use"bottom","top","left" or"right"to position the legend above, below, on the left or right sideof the diagram.

Legend inside plot

Ifinside = TRUE, legend can be placed insideplot. Use"top left","top right","bottom left"and"bottom right" to position legend in any of these corners,or a two-element numeric vector with values from 0-1. See alsoinside.

justify

Justification of legend, relative to its position ("center" ortwo-element numeric vector with values from 0-1.

palette

Character name of color palette.

discrete

Logical, ifTRUE, a discrete colour palette is returned.Else, a gradient palette is returned, where colours of the requested paletteare interpolated usingcolorRampPalette.

reverse

Logical, ifTRUE, order of returned colours is reversed.

...

Further arguments passed down to ggplot'sscale()-functions.

n

Numeric, number of colors to be returned. By default, the completecolour palette is returned.

css.theme

Name of the CSS pre-set theme-style. Can be used for table-functions.

Details

When using thecolors argument in function calls (e.g.plot_model()) or when calling one of the predefined scale-functions(e.g.scale_color_sjplot()), there are pre-defined colour palettesin this package. Useshow_sjplot_pals() to show all availablecolour palettes.

Examples

# prepare dataif (requireNamespace("haven")) {library(sjmisc)data(efc)efc <- to_factor(efc, c161sex, e42dep, c172code)m <- lm(neg_c_7 ~ pos_v_4 + c12hour + e42dep + c172code, data = efc)# create plot-objectp <- plot_model(m)# change themep + theme_sjplot()# change font-sizep + font_size(axis_title.x = 30)# apply color themep + scale_color_sjplot()# show all available colour palettesshow_sjplot_pals()# get colour values from specific palettesjplot_pal(pal = "breakfast club")}

Plot Pearson's Chi2-Test of multiple contingency tables

Description

Plot p-values of Pearson's Chi2-tests for multiple contingency tables as ellipses or tiles.Requires a data frame with dichotomous (dummy) variables.Calculation of Chi2-matrix taken fromTales of R.

Usage

sjp.chi2(  df,  title = "Pearson's Chi2-Test of Independence",  axis.labels = NULL,  wrap.title = 50,  wrap.labels = 20,  show.legend = FALSE,  legend.title = NULL)

Arguments

df

A data frame with (dichotomous) factor variables.

title

character vector, used as plot title. Depending on plot type and function,will be set automatically. Iftitle = "", no title is printed.For effect-plots, may also be a character vector of length > 1,to define titles for each sub-plot or facet.

axis.labels

character vector with labels used as axis labels. Optionalargument, since in most cases, axis labels are set automatically.

wrap.title

numeric, determines how many chars of the plot title are displayed inone line and when a line break is inserted.

wrap.labels

numeric, determines how many chars of the value, variable or axislabels are displayed in one line and when a line break is inserted.

show.legend

logical, ifTRUE, and depending on plot type andfunction, a legend is added to the plot.

legend.title

character vector, used as title for the plot legend.

Value

A ggplot-object.

Examples

# create data frame with 5 dichotomous (dummy) variablesmydf <- data.frame(as.factor(sample(1:2, 100, replace=TRUE)),                   as.factor(sample(1:2, 100, replace=TRUE)),                   as.factor(sample(1:2, 100, replace=TRUE)),                   as.factor(sample(1:2, 100, replace=TRUE)),                   as.factor(sample(1:2, 100, replace=TRUE)))# create variable labelsitems <- list(c("Item 1", "Item 2", "Item 3", "Item 4", "Item 5"))# plot Chi2-contingency-tablesjp.chi2(mydf, axis.labels = items)

Plot polynomials for (generalized) linear regression

Description

This function plots a scatter plot of a termpoly.termagainst a response variablex and adds - depending onthe amount of numeric values inpoly.degree - multiplepolynomial curves. A loess-smoothed line can be added to seewhich of the polynomial curves fits best to the data.

Usage

sjp.poly(  x,  poly.term,  poly.degree,  poly.scale = FALSE,  fun = NULL,  axis.title = NULL,  geom.colors = NULL,  geom.size = 0.8,  show.loess = TRUE,  show.loess.ci = TRUE,  show.p = TRUE,  show.scatter = TRUE,  point.alpha = 0.2,  point.color = "#404040",  loess.color = "#808080")

Arguments

x

A vector, representing the response variable of a linear (mixed) model; ora linear (mixed) model as returned bylm orlmer.

poly.term

Ifx is a vector,poly.term should also be a vector, representingthe polynomial term (independent variabl) in the model; ifx is afitted model,poly.term should be the polynomial term's name as character string.See 'Examples'.

poly.degree

Numeric, or numeric vector, indicating the degree of the polynomial.Ifpoly.degree is a numeric vector, multiple polynomial curves foreach degree are plotted. See 'Examples'.

poly.scale

Logical, ifTRUE,poly.term will be scaled beforelinear regression is computed. Default isFALSE. Scaling the polynomialterm may have an impact on the resulting p-values.

fun

Linear function when modelling polynomial terms. Usefun = "lm"for linear models, orfun = "glm" for generalized linear models.Whenx is not a vector, but a fitted model object, the functionis detected automatically. Ifx is a vector,fun defaultsto"lm".

axis.title

Character vector of length one or two (depending on theplot function and type), used as title(s) for the x and y axis. If notspecified, a default labelling is chosen.Note: Some plot typesmay not support this argument sufficiently. In such cases, use the returnedggplot-object and add axis titles manually withlabs. Useaxis.title = "" to remove axistitles.

geom.colors

user defined color for geoms. See 'Details' inplot_grpfrq.

geom.size

size resp. width of the geoms (bar width, line thickness or point size,depending on plot type and function). Note that bar and bin widths mostlyneed smaller values than dot sizes.

show.loess

Logical, ifTRUE, an additional loess-smoothed line is plotted.

show.loess.ci

Logical, ifTRUE, a confidence region for the loess-smoothed linewill be plotted.

show.p

Logical, ifTRUE (default), p-values for polynomial terms areprinted to the console.

show.scatter

Logical, if TRUE (default), adds a scatter plot of datapoints to the plot.

point.alpha

Alpha value of point-geoms in the scatter plots. Onlyapplies, ifshow.scatter = TRUE.

point.color

Color of of point-geoms in the scatter plots. Only applies,ifshow.scatter = TRUE.

loess.color

Color of the loess-smoothed line. Only applies, ifshow.loess = TRUE.

Details

For each polynomial degree, a simple linear regression onx (resp.the extracted response, ifx is a fitted model) is performed,where only the polynomial termpoly.term is included as independent variable.Thus,lm(y ~ x + I(x^2) + ... + I(x^i)) is repeatedly computedfor all values inpoly.degree, and the predicted values ofthe reponse are plotted against the raw values ofpoly.term.Ifx is a fitted model, other covariates are ignored whenfinding the best fitting polynomial.

This function evaluates raw polynomials,not orthogonal polynomials.Polynomials are computed using thepoly function,with argumentraw = TRUE.

To find out which polynomial degree fits best to the data, a loess-smoothedline (in dark grey) can be added (withshow.loess = TRUE). The polynomial curvesthat comes closest to the loess-smoothed line should be the bestfit to the data.

Value

A ggplot-object.

Examples

library(sjmisc)data(efc)# linear fit. loess-smoothed line indicates a more# or less cubic curvesjp.poly(efc$c160age, efc$quol_5, 1)# quadratic fitsjp.poly(efc$c160age, efc$quol_5, 2)# linear to cubic fitsjp.poly(efc$c160age, efc$quol_5, 1:4, show.scatter = FALSE)# fit sample modelfit <- lm(tot_sc_e ~ c12hour + e17age + e42dep, data = efc)# inspect relationship between predictors and responseplot_model(fit, type = "slope")# "e17age" does not seem to be linear correlated to response# try to find appropiate polynomial. Grey line (loess smoothed)# indicates best fit. Looks like x^4 has the best fit,# however, only x^3 has significant p-values.sjp.poly(fit, "e17age", 2:4, show.scatter = FALSE)## Not run: # fit new modelfit <- lm(tot_sc_e ~ c12hour + e42dep + e17age + I(e17age^2) + I(e17age^3),          data = efc)# plot marginal effects of polynomial termplot_model(fit, type = "pred", terms = "e17age")## End(Not run)

Wrapper to create plots and tables within a pipe-workflow

Description

This function has a pipe-friendly argument-structure, with thefirst argument always being the data, followed by variables thatshould be plotted or printed as table. The function then transformsthe input and calls the requested sjp.- resp. sjt.-functionto create a plot or table.

Bothsjplot() andsjtab() support grouped data frames.

Usage

sjplot(data, ..., fun = c("grpfrq", "xtab", "aov1", "likert"))sjtab(data, ..., fun = c("xtab", "stackfrq"))

Arguments

data

A data frame. May also be a grouped data frame (see 'Note' and'Examples').

...

Names of variables that should be plotted, and also furtherarguments passed down to thesjPlot-functions. See 'Examples'.

fun

Plotting function. Refers to the function name ofsjPlot-functions.See 'Details' and 'Examples'.

Details

Followingfun-values are currently supported:

"grpfrq"

callsplot_grpfrq. The firsttwo variables indata are used (and required) to create the plot.

"likert"

callsplot_likert.datamust be a data frame with items to plot.

"stackfrq"

callstab_stackfrq.data must be a data frame with items to create the table.

"xtab"

callsplot_xtab ortab_xtab.The first two variables indata are used (and required)to create the plot or table.

Value

See related sjp. and sjt.-functions.

Note

The...-argument is used, first, to specify the variables fromdatathat should be plotted, and, second, to name further arguments that areused in the subsequent plotting functions. Refer to the online-help ofsupported plotting-functions to see valid arguments.

data may also be a grouped data frame (seegroup_by)with up to two grouping variables. Plots are created for each subgroup then.

Examples

library(dplyr)data(efc)# Grouped frequenciesefc |> sjplot(e42dep, c172code, fun = "grpfrq")# Grouped frequencies, as box plotsefc |> sjplot(e17age, c172code, fun = "grpfrq",               type = "box", geom.colors = "Set1")## Not run: # table output of grouped data frameefc |>  group_by(e16sex, c172code) |>  select(e42dep, n4pstu, e16sex, c172code) |>  sjtab(fun = "xtab", use.viewer = FALSE) # open all tables in browser## End(Not run)

Summary of correlations as HTML table

Description

Shows the results of a computed correlation as HTML table. Requires eitheradata.frame or a matrix with correlation coefficientsas returned by thecor-function.

Usage

tab_corr(  data,  na.deletion = c("listwise", "pairwise"),  corr.method = c("pearson", "spearman", "kendall"),  title = NULL,  var.labels = NULL,  wrap.labels = 40,  show.p = TRUE,  p.numeric = FALSE,  fade.ns = TRUE,  val.rm = NULL,  digits = 3,  triangle = "both",  string.diag = NULL,  CSS = NULL,  encoding = NULL,  file = NULL,  use.viewer = TRUE,  remove.spaces = TRUE)

Arguments

data

Matrix with correlation coefficients as returned by thecor-function, or adata.frame of variables wherecorrelations between columns should be computed.

na.deletion

Indicates how missing values are treated. May be either"listwise" (default) or"pairwise". May beabbreviated.

corr.method

Indicates the correlation computation method. May be one of"pearson" (default),"spearman" or"kendall".May be abbreviated.

title

String, will be used as table caption.

var.labels

Character vector with variable names, which will be usedto label variables in the output.

wrap.labels

Numeric, determines how many chars of the value, variableor axis labels are displayed in one line and when a line break is inserted.

show.p

Logical, ifTRUE, p-values are also printed.

p.numeric

Logical, ifTRUE, the p-values are printedas numbers. IfFALSE (default), asterisks are used.

fade.ns

Logical, ifTRUE (default), non-significant correlation-valuesappear faded (by using a lighter grey text color). See 'Note'.

val.rm

Specify a number between 0 and 1 to suppress the output of correlation valuesthat are smaller thanval.rm. The absolute correlation values are used, soa correlation value of-.5 would be greater thanval.rm = .4 and thus not beomitted. By default, this argument isNULL, hence all values are shown in the table.If a correlation value is below the specified value ofval.rm, it is still printed tothe HTML table, but made "invisible" with white foreground color. You can use theCSSargument ("css.valueremove") to change color and appearance of those correlation value that are smaller thanthe limit specified byval.rm.

digits

Amount of decimals for estimates

triangle

Indicates whether only the upper right (use"upper"), lower left (use"lower")or both (use"both") triangles of the correlation table is filled with values. Defaultis"both". You can specifiy the inital letter only.

string.diag

A vector with string values of the same length asncol(data) (number ofcorrelated items) that can be used to display content in the diagonal cellswhere row and column item are identical (i.e. the "self-correlation"). By defauilt,this argument isNULL and the diagnal cells are empty.

CSS

Alist with user-defined style-sheet-definitions,according to theofficial CSS syntax.See 'Details' orthis package-vignette.

encoding

Character vector, indicating the charset encoding usedfor variable and value labels. Default is"UTF-8". For WindowsSystems,encoding = "Windows-1252" might be necessary for properdisplay of special characters.

file

Destination file, if the output should be saved as file.IfNULL (default), the output will be saved as temporary file andopened either in the IDE's viewer pane or the default web browser.

use.viewer

Logical, ifTRUE, the HTML table is shown in the IDE'sviewer pane. IfFALSE or no viewer available, the HTML table isopened in a web browser.

remove.spaces

Logical, ifTRUE, leading spaces are removed from all lines in the final stringthat contains the html-data. Use this, if you want to remove parantheses for html-tags. The html-sourcemay look less pretty, but it may help when exporting html-tables to office tools.

Value

Invisibly returns

for further use.

Note

Ifdata is a matrix with correlation coefficients as returned bythecor-function, p-values can't be computed.Thus,show.p,p.numeric andfade.nsonly have an effect ifdata is adata.frame.

Examples

## Not run: if (interactive()) {  # Data from the EUROFAMCARE sample dataset  library(sjmisc)  data(efc)  # retrieve variable and value labels  varlabs <- get_label(efc)  # recveive first item of COPE-index scale  start <- which(colnames(efc) == "c83cop2")  # recveive last item of COPE-index scale  end <- which(colnames(efc) == "c88cop7")  # create data frame with COPE-index scale  mydf <- data.frame(efc[, c(start:end)])  colnames(mydf) <- varlabs[c(start:end)]  # we have high correlations here, because all items  # belong to one factor.  tab_corr(mydf, p.numeric = TRUE)  # auto-detection of labels, only lower triangle  tab_corr(efc[, c(start:end)], triangle = "lower")  # auto-detection of labels, only lower triangle, all correlation  # values smaller than 0.3 are not shown in the table  tab_corr(efc[, c(start:end)], triangle = "lower", val.rm = 0.3)  # auto-detection of labels, only lower triangle, all correlation  # values smaller than 0.3 are printed in blue  tab_corr(efc[, c(start:end)], triangle = "lower",val.rm = 0.3,           CSS = list(css.valueremove = 'color:blue;'))}## End(Not run)

Print data frames as HTML table.

Description

These functions print data frames as HTML-table, showingthe results in RStudio's viewer pane or in a web browser.

Usage

tab_df(  x,  title = NULL,  footnote = NULL,  col.header = NULL,  show.type = FALSE,  show.rownames = FALSE,  show.footnote = FALSE,  alternate.rows = FALSE,  sort.column = NULL,  digits = 2,  encoding = "UTF-8",  CSS = NULL,  file = NULL,  use.viewer = TRUE,  ...)tab_dfs(  x,  titles = NULL,  footnotes = NULL,  col.header = NULL,  show.type = FALSE,  show.rownames = FALSE,  show.footnote = FALSE,  alternate.rows = FALSE,  sort.column = NULL,  digits = 2,  encoding = "UTF-8",  CSS = NULL,  file = NULL,  use.viewer = TRUE,  rnames = NULL,  ...)

Arguments

x

Fortab_df(), a data frame; and fortab_dfs(), alist of data frames.

title,titles,footnote,footnotes

Character vector with tablecaption(s) resp. footnote(s). Fortab_df(), must be a characterof length 1; fortab_dfs(), a character vector of same length asx (i.e. one title or footnote per data frame).

col.header

Character vector with elements used as column header forthe table. IfNULL, column names fromx are used ascolumn header.

show.type

Logical, ifTRUE, adds information about thevariable type to the variable column.

show.rownames

Logical, ifTRUE, adds a column with thedata frame's rowname to the table output.

show.footnote

Logical, ifTRUE,adds a summary footnote belowthe table. Fortab_df(), specify the string infootnote,fortab_dfs() provide a character vector infootnotes.

alternate.rows

Logical, ifTRUE, rows are printed inalternatig colors (white and light grey by default).

sort.column

Numeric vector, indicating the index of the columnthat should sorted. by default, the column is sorted in ascending order.Use negative index for descending order, for instance,sort.column = -3 would sort the third column in descending order.Note that the first column with rownames is not counted.

digits

Numeric, amount of digits after decimal point when roundingvalues.

encoding

Character vector, indicating the charset encoding usedfor variable and value labels. Default is"UTF-8". For WindowsSystems,encoding = "Windows-1252" might be necessary for properdisplay of special characters.

CSS

Alist with user-defined style-sheet-definitions,according to theofficial CSS syntax.See 'Details' orthis package-vignette.

file

Destination file, if the output should be saved as file.IfNULL (default), the output will be saved as temporary file andopened either in the IDE's viewer pane or the default web browser.

use.viewer

Logical, ifTRUE, the HTML table is shown in the IDE'sviewer pane. IfFALSE or no viewer available, the HTML table isopened in a web browser.

...

Currently not used.

rnames

Character vector, can be used to set row names whenshow.rownames=TRUE.

Details

How do I useCSS-argument?

With theCSS-argument, the visual appearance of the tablescan be modified. To get an overview of all style-sheet-classnamesthat are used in this function, see return valuepage.style fordetails. Arguments for this list have following syntax:

  1. the class-name as argument name and

  2. each style-definition must end with a semicolon

You can add style information to the default styles by using a +(plus-sign) as initial character for the argument attributes.Examples:

See further examples inthis package-vignette.

Value

A list with following items:

Note

The HTML tables can either be saved as file and manually opened(use argumentfile) or they can be saved as temporary files andwill be displayed in the RStudio Viewer pane (if working with RStudio)or opened with the default web browser. Displaying resp. opening atemporary file is the default behaviour.

Examples

## Not run: data(iris)data(mtcars)tab_df(iris[1:5, ])tab_dfs(list(iris[1:5, ], mtcars[1:5, 1:5]))# sort 2nd column ascendingtab_df(iris[1:5, ], sort.column = 2)# sort 2nd column descendingtab_df(iris[1:5, ], sort.column = -2)## End(Not run)

Summary of factor analysis as HTML table

Description

Performs a factor analysis on a data frame or matrixand displays the factors as HTMLtable, or saves them as file.

In case a data frame is used asparameter, the Cronbach's Alpha value for each factor scale will be calculated,i.e. all variables with the highest loading for a factor are taken for thereliability test. The result is an alpha value for each factor dimension.

Usage

tab_fa(  data,  rotation = "promax",  method = c("ml", "minres", "wls", "gls", "pa", "minchi", "minrank"),  nmbr.fctr = NULL,  fctr.load.tlrn = 0.1,  sort = FALSE,  title = "Factor Analysis",  var.labels = NULL,  wrap.labels = 40,  show.cronb = TRUE,  show.comm = FALSE,  alternate.rows = FALSE,  digits = 2,  CSS = NULL,  encoding = NULL,  file = NULL,  use.viewer = TRUE,  remove.spaces = TRUE)

Arguments

data

A data frame that should be used to compute a PCA, or aprcomp object.

rotation

Rotation of the factor loadings. May be one of"varimax", "quartimax", "promax", "oblimin", "simplimax", "cluster"or"none".

method

the factoring method to be used."ml" will do a maximum likelihood factor analysis (default)."minres" will do a minimum residual (OLS),"wls" will do a weighted least squares (WLS) solution,"gls" does a generalized weighted least squares (GLS),"pa" will do the principal factor solution,"minchi" will minimize the sample size weighted chi squarewhen treating pairwise correlations with different number ofsubjects per pair."minrank" will do a minimum rank factor analysis.

nmbr.fctr

Number of factors used for calculating the rotation. Bydefault, this value isNULL and the amount of factors iscalculated according to the Kaiser-criteria.

fctr.load.tlrn

Specifies the minimum difference a variable needs to have betweenfactor loadings (components) in order to indicate a clear loading on just one factor and notdiffusing over all factors. For instance, a variable with 0.8, 0.82 and 0.84 factor loadingon 3 possible factors can not be clearly assigned to just one factor and thus would be removedfrom the principal component analysis. By default, the minimum difference of loading valuesbetween the highest and 2nd highest factor should be 0.1

sort

logical, ifTRUE, sort the loadings for each factors(items will be sorted in terms of their greatest loading, in descendingorder)

title

String, will be used as table caption.

var.labels

Character vector with variable names, which will be usedto label variables in the output.

wrap.labels

Numeric, determines how many chars of the value, variableor axis labels are displayed in one line and when a line break is inserted.

show.cronb

Logical, ifTRUE (default), the cronbach's alpha value for each factor scale will be calculated,i.e. all variables with the highest loading for a factor are taken for thereliability test. The result is an alpha value for each factor dimension.Only applies whendata is a data frame.

show.comm

Logical, ifTRUE, show the communality column in the table.

alternate.rows

Logical, ifTRUE, rows are printed inalternatig colors (white and light grey by default).

digits

Amount of decimals for estimates

CSS

Alist with user-defined style-sheet-definitions,according to theofficial CSS syntax.See 'Details' orthis package-vignette.

encoding

Character vector, indicating the charset encoding usedfor variable and value labels. Default is"UTF-8". For WindowsSystems,encoding = "Windows-1252" might be necessary for properdisplay of special characters.

file

Destination file, if the output should be saved as file.IfNULL (default), the output will be saved as temporary file andopened either in the IDE's viewer pane or the default web browser.

use.viewer

Logical, ifTRUE, the HTML table is shown in the IDE'sviewer pane. IfFALSE or no viewer available, the HTML table isopened in a web browser.

remove.spaces

Logical, ifTRUE, leading spaces are removed from all lines in the final stringthat contains the html-data. Use this, if you want to remove parantheses for html-tags. The html-sourcemay look less pretty, but it may help when exporting html-tables to office tools.

Value

Invisibly returns

for further use.

Note

This method for factor analysis relies on the functionsfa andfa.parallel from the psych package.

Examples

## Not run: # Data from the EUROFAMCARE sample datasetlibrary(sjmisc)library(GPArotation)data(efc)# recveive first item of COPE-index scalestart <- which(colnames(efc) == "c82cop1")# recveive last item of COPE-index scaleend <- which(colnames(efc) == "c90cop9")# auto-detection of labelsif (interactive()) {  tab_fa(efc[, start:end])}## End(Not run)

Summary of item analysis of an item scale as HTML table

Description

This function performs an item analysis with certain statistics that areuseful for scale or index development. The resulting tables are shown in theviewer pane resp. webbrowser or can be saved as file. Following statistics arecomputed for each item of a data frame:

Optional, following statistics can be computed as well:

Iffactor.groups is notNULL, the data framedf will besplitted into groups, assuming thatfactor.groups indicate those columnsof the data frame that belong to a certain factor (see return value of functiontab_pcaas example for retrieving factor groups for a scale and see examples for more details).

Usage

tab_itemscale(  df,  factor.groups = NULL,  factor.groups.titles = "auto",  scale = FALSE,  min.valid.rowmean = 2,  alternate.rows = TRUE,  sort.column = NULL,  show.shapiro = FALSE,  show.kurtosis = FALSE,  show.corr.matrix = TRUE,  CSS = NULL,  encoding = NULL,  file = NULL,  use.viewer = TRUE,  remove.spaces = TRUE)sjt.itemanalysis(  df,  factor.groups = NULL,  factor.groups.titles = "auto",  scale = FALSE,  min.valid.rowmean = 2,  alternate.rows = TRUE,  sort.column = NULL,  show.shapiro = FALSE,  show.kurtosis = FALSE,  show.corr.matrix = TRUE,  CSS = NULL,  encoding = NULL,  file = NULL,  use.viewer = TRUE,  remove.spaces = TRUE)

Arguments

df

A data frame with items.

factor.groups

If notNULL,df will be splitted into sub-groups,where the item analysis is carried out for each of these groups. Must be a vector of samelength asncol(df), where each item in this vector represents the group number ofthe related columns ofdf. Iffactor.groups = "auto", a principalcomponent analysis with Varimax rotation is performed, and the resultinggroups for the components are used as group index. See 'Examples'.

factor.groups.titles

Titles for each factor group that will be used as table caption for eachcomponent-table. Must be a character vector of same length aslength(unique(factor.groups)).Default is"auto", which means that each table has a standard captionComponent x.UseNULL to suppress table captions.

scale

Logical, ifTRUE, the data frame's vectors will be scaled when calculating theCronbach's Alpha value (seeitem_reliability). Recommended, whenthe variables have different measures / scales.

min.valid.rowmean

Minimum amount of valid values to compute row means for index scores.Default is 2, i.e. the return valuesindex.scores anddf.index.scores arecomputed for those items that have at leastmin.valid.rowmean per case (observation, ortechnically, row). Seemean_n for details.

alternate.rows

Logical, ifTRUE, rows are printed inalternatig colors (white and light grey by default).

sort.column

Numeric vector, indicating the index of the columnthat should sorted. by default, the column is sorted in ascending order.Use negative index for descending order, for instance,sort.column = -3 would sort the third column in descending order.Note that the first column with rownames is not counted.

show.shapiro

Logical, ifTRUE, a Shapiro-Wilk normality test is computed for each item.Seeshapiro.test for details.

show.kurtosis

Logical, ifTRUE, the kurtosis for each item will also be shown (seekurtosianddescribe in thepsych-package for more details.

show.corr.matrix

Logical, ifTRUE (default), a correlation matrix of each component'sindex score is shown. Only applies iffactor.groups is notNULL anddf hasmore than one group. First, for each case (df's row), the sum of all variables (df's columns) isscaled (using thescale-function) and represents a "total score" foreach component (a component is represented by each group offactor.groups).After that, each case (df's row) has a scales sum score for each component.Finally, a correlation of these "scale sum scores" is computed.

CSS

Alist with user-defined style-sheet-definitions,according to theofficial CSS syntax.See 'Details' orthis package-vignette.

encoding

Character vector, indicating the charset encoding usedfor variable and value labels. Default is"UTF-8". For WindowsSystems,encoding = "Windows-1252" might be necessary for properdisplay of special characters.

file

Destination file, if the output should be saved as file.IfNULL (default), the output will be saved as temporary file andopened either in the IDE's viewer pane or the default web browser.

use.viewer

Logical, ifTRUE, the HTML table is shown in the IDE'sviewer pane. IfFALSE or no viewer available, the HTML table isopened in a web browser.

remove.spaces

Logical, ifTRUE, leading spaces are removed from all lines in the final stringthat contains the html-data. Use this, if you want to remove parantheses for html-tags. The html-sourcemay look less pretty, but it may help when exporting html-tables to office tools.

Value

Invisibly returns

Iffactor.groups = NULL, each list contains only one elment, since just onetable is printed for the complete scale indicated bydf. Iffactor.groupsis a vector of group-index-values, the lists contain elements for each sub-group.

Note

References

Examples

# Data from the EUROFAMCARE sample datasetlibrary(sjmisc)library(sjlabelled)data(efc)# retrieve variable and value labelsvarlabs <- get_label(efc)# recveive first item of COPE-index scalestart <- which(colnames(efc) == "c82cop1")# recveive last item of COPE-index scaleend <- which(colnames(efc) == "c90cop9")# create data frame with COPE-index scalemydf <- data.frame(efc[, start:end])colnames(mydf) <- varlabs[start:end]## Not run: if (interactive()) {  tab_itemscale(mydf)  # auto-detection of labels  tab_itemscale(efc[, start:end])  # Compute PCA on Cope-Index, and perform a  # item analysis for each extracted factor.  indices <- tab_pca(mydf)$factor.index  tab_itemscale(mydf, factor.groups = indices)  # or, equivalent  tab_itemscale(mydf, factor.groups = "auto")}## End(Not run)

Print regression models as HTML table

Description

tab_model() creates HTML tables from regression models.

Usage

tab_model(  ...,  transform,  show.intercept = TRUE,  show.est = TRUE,  show.ci = 0.95,  show.ci50 = FALSE,  show.se = NULL,  show.std = NULL,  std.response = TRUE,  show.p = TRUE,  show.stat = FALSE,  show.df = FALSE,  show.zeroinf = TRUE,  show.r2 = TRUE,  show.icc = TRUE,  show.re.var = TRUE,  show.ngroups = TRUE,  show.fstat = FALSE,  show.aic = FALSE,  show.aicc = FALSE,  show.dev = FALSE,  show.loglik = FALSE,  show.obs = TRUE,  show.reflvl = FALSE,  terms = NULL,  rm.terms = NULL,  order.terms = NULL,  keep = NULL,  drop = NULL,  title = NULL,  pred.labels = NULL,  dv.labels = NULL,  wrap.labels = 25,  bootstrap = FALSE,  iterations = 1000,  seed = NULL,  vcov.fun = NULL,  vcov.args = NULL,  string.pred = "Predictors",  string.est = "Estimate",  string.std = "std. Beta",  string.ci = "CI",  string.se = "std. Error",  string.std_se = "standardized std. Error",  string.std_ci = "standardized CI",  string.p = "p",  string.std.p = "std. p",  string.df = "df",  string.stat = "Statistic",  string.std.stat = "std. Statistic",  string.resp = "Response",  string.intercept = "(Intercept)",  strings = NULL,  ci.hyphen = "&nbsp;&ndash;&nbsp;",  minus.sign = "&#45;",  collapse.ci = FALSE,  collapse.se = FALSE,  linebreak = TRUE,  col.order = c("est", "se", "std.est", "std.se", "ci", "std.ci", "ci.inner", "ci.outer",    "stat", "std.stat", "p", "std.p", "df.error", "response.level"),  digits = 2,  digits.p = 3,  digits.rsq = 3,  digits.re = 2,  emph.p = TRUE,  p.val = NULL,  df.method = NULL,  p.style = c("numeric", "stars", "numeric_stars", "scientific", "scientific_stars"),  p.threshold = c(0.05, 0.01, 0.001),  p.adjust = NULL,  case = "parsed",  auto.label = TRUE,  prefix.labels = c("none", "varname", "label"),  bpe = "median",  CSS = css_theme("regression"),  file = NULL,  use.viewer = TRUE,  encoding = "UTF-8")

Arguments

...

One or more regression models, including glm's or mixed models.May also be alist with fitted models. See 'Examples'.

transform

A character vector, naming a function that will be appliedon estimates and confidence intervals. By default,transform willautomatically use"exp" as transformation for applicable classes ofmodel (e.g. logistic or poisson regression). Estimates of linearmodels remain untransformed. UseNULL if you want the raw,non-transformed estimates.

show.intercept

Logical, ifTRUE, the intercepts are printed.

show.est

Logical, ifTRUE, the estimates are printed.

show.ci

Either logical, and ifTRUE, the confidence intervalsis printed to the table; ifFALSE, confidence intervals areomitted. Or numeric, between 0 and 1, indicating the range of theconfidence intervals.

show.ci50

Logical, ifTRUE, for Bayesian models, a secondcredible interval is added to the table output.

show.se

Logical, ifTRUE, the standard errors arealso printed. If robust standard errors are required, use argumentsvcov.fun, andvcov.args (seestandard_error for details).

show.std

Indicates whether standardized beta-coefficients shouldalso printed, and if yes, which type of standardization is done.See 'Details'.

std.response

Logical, whether the response variable will also bestandardized if standardized coefficients are requested. Setting bothstd.response = TRUE andshow.std = TRUE will behave as ifthe complete data was standardized before fitting the model.

show.p

Logical, ifTRUE, p-values are also printed.

show.stat

Logical, ifTRUE, the coefficients' test statisticis also printed.

show.df

Logical, ifTRUE andp.val = "kr", the p-valuesfor linear mixed models are based on df with Kenward-Rogers approximation.These df-values are printed. Seep_value for details.

show.zeroinf

Logical, ifTRUE and model has a zero-inflatedmodel part, this is also printed to the table.

show.r2

Logical, ifTRUE, the r-squared value is also printed.Depending on the model, these might be pseudo-r-squared values, or Bayesianr-squared etc. Seer2 for details.

show.icc

Logical, ifTRUE, prints the intraclass correlationcoefficient for mixed models. Seeicc for details.

show.re.var

Logical, ifTRUE, prints the random effect variancesfor mixed models. Seeget_variance for details.

show.ngroups

Logical, ifTRUE, shows number of random effects groupsfor mixed models.

show.fstat

Logical, ifTRUE, the F-statistics for each model isprinted in the table summary. This option is not supported by all model types.

show.aic

Logical, ifTRUE, the AIC value for each model is printedin the table summary.

show.aicc

Logical, ifTRUE, the second-order AIC value for each modelis printed in the table summary.

show.dev

Logical, ifTRUE, shows the deviance of the model.

show.loglik

Logical, ifTRUE, shows the log-Likelihood of the model.

show.obs

Logical, ifTRUE, the number of observations per model isprinted in the table summary.

show.reflvl

Logical, ifTRUE, an additional row is inserted tothe table before each predictor of typefactor, which willindicate the reference level of the related factor.

terms

Character vector with names of those terms (variables) that shouldbe printed in the table. All other terms are removed from the output. IfNULL, all terms are printed. Note that the term names must matchthe names of the model's coefficients. For factors, this means thatthe variable name is suffixed with the related factor level, and eachcategory counts as one term. E.g.rm.terms = "t_name [2,3]"would remove the terms"t_name2" and"t_name3" (assumingthat the variablet_name is categorical and has at leastthe factor levels2 and3). Another example for theiris-dataset:terms = "Species" would not work, insteaduseterms = "Species [versicolor,virginica]".

rm.terms

Character vector with names that indicate which terms shouldbe removed from the output Counterpart toterms.rm.terms ="t_name" would remove the termt_name. Default isNULL, i.e.all terms are used. For factors, levels that should be removed from the plotneed to be explicitly indicated in square brackets, and match the model'scoefficient names, e.g.rm.terms = "t_name [2,3]" would remove the terms"t_name2" and"t_name3" (assuming that the variablet_namewas categorical and has at least the factor levels2 and3).

order.terms

Numeric vector, indicating in which order the coefficientsshould be plotted. See examples inthis package-vignette.

keep,drop

Character containing a regular expression pattern thatdescribes the parameters that should be included (forkeep) or excluded(fordrop) in the returned data frame.keep may also be anamed list of regular expressions. All non-matching parameters will beremoved from the output. Ifkeep has more than one element, thesewill be merged with anOR operator into a regular expression patternlike this:"(one|two|three)". See further details in?parameters::model_parameters.

title

String, will be used as table caption.

pred.labels

Character vector with labels of predictor variables.If notNULL,pred.labels will be used in the firsttable column with the predictors' names. By default, ifauto.label = TRUEanddata is labelled,term_labels is called to retrieve the labelsof the coefficients, which will be used as predictor labels. If data isnot labelled,format_parameters()is used to create pretty labels. Ifpred.labels = "" orauto.label = FALSE, the rawvariable names as used in the model formula are used as predictorlabels. Ifpred.labels is a named vector, predictor labels (bydefault, the names of the model's coefficients) will be matched with thenames ofpred.labels. This ensures that labels always match therelated predictor in the table, no matter in which way the predictorsare sorted. See 'Examples'.

dv.labels

Character vector with labels of dependent variables of allfitted models. Ifdv.labels = "", the row with names of dependentvariables is omitted from the table.

wrap.labels

Numeric, determines how many chars of the value, variableor axis labels are displayed in one line and when a line break is inserted.

bootstrap

Logical, ifTRUE, returns bootstrapped estimates..

iterations

Numeric, number of bootstrap iterations (default is 1000).

seed

Numeric, the number of the seed to replicate bootstrapped estimates. IfNULL, uses random seed.

vcov.fun

Variance-covariance matrix used to compute uncertaintyestimates (e.g., for robust standard errors). This argument accepts acovariance matrix, a function which returns a covariance matrix, or astring which identifies the function to be used to compute the covariancematrix. Seemodel_parameters().

vcov.args

List of arguments to be passed to the function identified bythevcov.fun argument. This function is typically supplied by thesandwich orclubSandwich packages. Please refer to theirdocumentation (e.g.,?sandwich::vcovHAC) to see the list ofavailable arguments.

string.pred

Character vector,used as headline for the predictor column.Default is"Predictors".

string.est

Character vector, used for the column heading of coefficients.Default is based on the response scale, e.g. for logistic regression models,"Odds Ratios" will be chosen, while for Poisson models it is"Incidence Rate Ratios" etc. Default if not specified is"Estimate".

string.std

Character vector, used for the column heading of standardized beta coefficients. Default is"std. Beta".

string.ci

Character vector, used for the column heading of confidence interval values. Default is"CI".

string.se

Character vector, used for the column heading of standard error values. Default is"std. Error".

string.std_se

Character vector, used for the column heading of standard error of standardized coefficients. Default is"standardized std. Error".

string.std_ci

Character vector, used for the column heading of confidence intervals of standardized coefficients. Default is"standardized std. Error".

string.p

String value, used for the column heading of p values. Default is"p".

string.std.p

Character vector, used for the column heading of p values. Default is"std. p".

string.df

Character vector, used for the column heading of degrees of freedom. Default is"df".

string.stat

Character vector, used for the test statistic. Default is"Statistic".

string.std.stat

Character vector, used for the test statistic. Default is"std. Statistic".

string.resp

Character vector, used for the column heading of of the response level for multinominal or categorical models. Default is"Response".

string.intercept

Character vector, used as name for the intercept parameter. Default is"(Intercept)".

strings

Named character vector, as alternative to arguments likestring.ciorstring.p etc. The name (lhs) must be one of the string-indicator fromthe aforementioned arguments, while the value (rhs) is the string that is usedas column heading. E.g.,strings = c(ci = "Conf.Int.", se = "std. Err")would be equivalent to settingstring.ci = "Conf.Int.", string.se = "std. Err".

ci.hyphen

Character vector, indicating the hyphen for confidence interval range.May be an HTML entity. See 'Examples'.

minus.sign

string, indicating the minus sign for negative numbers.May be an HTML entity. See 'Examples'.

collapse.ci

Logical, ifFALSE, the CI values are shown ina separate table column.

collapse.se

Logical, ifFALSE, the SE values are shown ina separate table column.

linebreak

Logical, ifTRUE andcollapse.ci = FALSE orcollapse.se = FALSE, inserts a line break between estimate andCI resp. SE values. IfFALSE, values are printed in the same lineas estimate values.

col.order

Character vector, indicating which columns should be printedand in which order. Column names that are excluded fromcol.orderare not shown in the table output. However, column names that are included,are only shown in the table when the related argument (likeshow.estfor"estimate") is set toTRUE or another valid value.Table columns are printed in the order as they appear incol.order.

digits

Amount of decimals for estimates

digits.p

Amount of decimals for p-values

digits.rsq

Amount of decimals for r-squared values

digits.re

Amount of decimals for random effects part of the summary table.

emph.p

Logical, ifTRUE, significant p-values are shown bold faced.

df.method,p.val

Method for computing degrees of freedom for p-values,standard errors and confidence intervals (CI). Only applies to mixed models.Usedf.method = "wald" for a faster, but less precise computation.This will use the residual degrees of freedom (as returned bydf.residual())for linear mixed models, andInf degrees if freedom for all othermodel families.df.method = "kenward" (ordf.method = "kr")uses Kenward-Roger approximation for the degrees of freedom.df.method = "satterthwaite" uses Satterthwaite's approximation and"ml1" uses a "m-l-1" heuristic seedegrees_of_freedomfor details). Useshow.df = TRUE to show the approximated degrees of freedomfor each coefficient.

p.style

Character, indicating if p-values should be printed asnumeric value ("numeric"), as 'stars' (asterisks) only ("stars"),or scientific ("scientific"). Scientific and numeric style can becombined with "stars", e.g."numeric_stars"

p.threshold

Numeric vector of length 3, indicating the treshold forannotating p-values with asterisks. Only applies ifp.style = "asterisk".

p.adjust

String value, if notNULL, indicates the methodto adjust p-values. Seep.adjust for details.

case

Desired target case. Labels will automatically converted into thespecified character case. Seesnakecase::to_any_case() for moredetails on this argument. By default, ifcase is not specified,it will be set to"parsed", unlessprefix.labels is not"none". Ifprefix.labels is either"label" (or"l") or"varname" (or"v") andcase is notspecified, it will be set toNULL - this is a more convenientdefault when prefixing labels.

auto.label

Logical, ifTRUE (the default),anddata is labelled,term_labels is called to retrieve the labelsof the coefficients, which will be used as predictor labels. If data isnot labelled,format_parameters()is used to create pretty labels. Ifauto.label = FALSE,original variable names and value labels (factor levels) are used.

prefix.labels

Indicates whether the value labels of categorical variablesshould be prefixed, e.g. with the variable name or variable label. Seeargumentprefix interm_labels fordetails.

bpe

ForStan-models (fitted with therstanarm- orbrms-package), the Bayesian point estimate is, by default, the medianof the posterior distribution. Usebpe to define other functions tocalculate the Bayesian point estimate.bpe needs to be a characternaming the specific function, which is passed to thefun-argument intypical_value. So,bpe = "mean" wouldcalculate the mean value of the posterior distribution.

CSS

Alist with user-defined style-sheet-definitions,according to theofficial CSS syntax.See 'Details' orthis package-vignette.

file

Destination file, if the output should be saved as file.IfNULL (default), the output will be saved as temporary file andopened either in the IDE's viewer pane or the default web browser.

use.viewer

Logical, ifTRUE, the HTML table is shown in the IDE'sviewer pane. IfFALSE or no viewer available, the HTML table isopened in a web browser.

encoding

Character vector, indicating the charset encoding usedfor variable and value labels. Default is"UTF-8". For WindowsSystems,encoding = "Windows-1252" might be necessary for properdisplay of special characters.

Details

Standardized Estimates

Default standardization is done by completely refitting the model on thestandardized data. Hence, this approach is equal to standardizing thevariables before fitting the model, which is particularly recommended forcomplex models that include interactions or transformations (e.g.,polynomial or spline terms). Whenshow.std = "std2", standardizationof estimates follows Gelman's (2008) suggestion, rescaling the estimates bydividing them by two standard deviations instead of just one. Resultingcoefficients are then directly comparable for untransformed binarypredictors. For backward compatibility reasons,show.std also may bea logical value; ifTRUE, normal standardized estimates are printed(same effect asshow.std = "std"). Useshow.std = NULL(default) orshow.std = FALSE, if no standardization is required.

How do I useCSS-argument?

With theCSS-argument, the visual appearance of the tablescan be modified. To get an overview of all style-sheet-classnamesthat are used in this function, see return valuepage.style for details.Arguments for this list have following syntax:

  1. the class-names with"css."-prefix as argument name and

  2. each style-definition must end with a semicolon

You can add style information to the default styles by using a + (plus-sign) asinitial character for the argument attributes. Examples:

Value

Invisibly returns

for further use.

Note

The HTML tables can either be saved as file and manually opened (use argumentfile) orthey can be saved as temporary files and will be displayed in the RStudio Viewer pane (if working with RStudio)or opened with the default web browser. Displaying resp. opening a temporary file is thedefault behaviour (i.e.file = NULL).

Examples are shown in these three vignettes:Summary of Regression Models as HTML Table,Summary of Mixed Models as HTML Table andSummary of Bayesian Models as HTML Table.


Summary of principal component analysis as HTML table

Description

Performes a principle component analysis on a data frame or matrix(with varimax or oblimin rotation) and displays the factor solution as HTMLtable, or saves them as file.

In case a data frame is used asparameter, the Cronbach's Alpha value for each factor scale will be calculated,i.e. all variables with the highest loading for a factor are taken for thereliability test. The result is an alpha value for each factor dimension.

Usage

tab_pca(  data,  rotation = c("varimax", "quartimax", "promax", "oblimin", "simplimax", "cluster",    "none"),  nmbr.fctr = NULL,  fctr.load.tlrn = 0.1,  title = "Principal Component Analysis",  var.labels = NULL,  wrap.labels = 40,  show.cronb = TRUE,  show.msa = FALSE,  show.var = FALSE,  alternate.rows = FALSE,  digits = 2,  string.pov = "Proportion of Variance",  string.cpov = "Cumulative Proportion",  CSS = NULL,  encoding = NULL,  file = NULL,  use.viewer = TRUE,  remove.spaces = TRUE)

Arguments

data

A data frame that should be used to compute a PCA, or aprcomp object.

rotation

Rotation of the factor loadings. May be one of"varimax", "quartimax", "promax", "oblimin", "simplimax", "cluster"or"none".

nmbr.fctr

Number of factors used for calculating the rotation. Bydefault, this value isNULL and the amount of factors iscalculated according to the Kaiser-criteria.

fctr.load.tlrn

Specifies the minimum difference a variable needs to have betweenfactor loadings (components) in order to indicate a clear loading on just one factor and notdiffusing over all factors. For instance, a variable with 0.8, 0.82 and 0.84 factor loadingon 3 possible factors can not be clearly assigned to just one factor and thus would be removedfrom the principal component analysis. By default, the minimum difference of loading valuesbetween the highest and 2nd highest factor should be 0.1

title

String, will be used as table caption.

var.labels

Character vector with variable names, which will be usedto label variables in the output.

wrap.labels

Numeric, determines how many chars of the value, variableor axis labels are displayed in one line and when a line break is inserted.

show.cronb

Logical, ifTRUE (default), the cronbach's alpha value for each factor scale will be calculated,i.e. all variables with the highest loading for a factor are taken for thereliability test. The result is an alpha value for each factor dimension.Only applies whendata is a data frame.

show.msa

Logical, ifTRUE, shows an additional column with the measure of sampling adequacy accordingdor each component.

show.var

Logical, ifTRUE, the proportions of variances for each component as well as cumulativevariance are shown in the table footer.

alternate.rows

Logical, ifTRUE, rows are printed inalternatig colors (white and light grey by default).

digits

Amount of decimals for estimates

string.pov

String for the table row that contains the proportions of variances. By default,"Proportion of Variance" will be used.

string.cpov

String for the table row that contains the cumulative variances. By default,"Cumulative Proportion" will be used.

CSS

Alist with user-defined style-sheet-definitions,according to theofficial CSS syntax.See 'Details' orthis package-vignette.

encoding

Character vector, indicating the charset encoding usedfor variable and value labels. Default is"UTF-8". For WindowsSystems,encoding = "Windows-1252" might be necessary for properdisplay of special characters.

file

Destination file, if the output should be saved as file.IfNULL (default), the output will be saved as temporary file andopened either in the IDE's viewer pane or the default web browser.

use.viewer

Logical, ifTRUE, the HTML table is shown in the IDE'sviewer pane. IfFALSE or no viewer available, the HTML table isopened in a web browser.

remove.spaces

Logical, ifTRUE, leading spaces are removed from all lines in the final stringthat contains the html-data. Use this, if you want to remove parantheses for html-tags. The html-sourcemay look less pretty, but it may help when exporting html-tables to office tools.

Value

Invisibly returns

for further use.

Examples

## Not run: # Data from the EUROFAMCARE sample datasetlibrary(sjmisc)data(efc)# recveive first item of COPE-index scalestart <- which(colnames(efc) == "c82cop1")# recveive last item of COPE-index scaleend <- which(colnames(efc) == "c90cop9")# auto-detection of labelsif (interactive()) {  tab_pca(efc[, start:end])}## End(Not run)

Summary of stacked frequencies as HTML table

Description

Shows the results of stacked frequencies (such as likert scales) as HTML table.This function is useful when several items with identical scale/categoriesshould be printed as table to compare their distributions (e.g.when plotting scales like SF, Barthel-Index, Quality-of-Life-scales etc.).

Usage

tab_stackfrq(  items,  weight.by = NULL,  title = NULL,  var.labels = NULL,  value.labels = NULL,  wrap.labels = 20,  sort.frq = NULL,  alternate.rows = FALSE,  digits = 2,  string.total = "N",  string.na = "NA",  show.n = FALSE,  show.total = FALSE,  show.na = FALSE,  show.skew = FALSE,  show.kurtosis = FALSE,  digits.stats = 2,  file = NULL,  encoding = NULL,  CSS = NULL,  use.viewer = TRUE,  remove.spaces = TRUE)

Arguments

items

Data frame, or a grouped data frame, with each column representing one item.

weight.by

Vector of weights that will be applied to weight all cases.Must be a vector of same length as the input vector. Default isNULL, so no weights are used.

title

String, will be used as table caption.

var.labels

Character vector with variable names, which will be usedto label variables in the output.

value.labels

Character vector (orlist of character vectors)with value labels of the supplied variables, which will be usedto label variable values in the output.

wrap.labels

Numeric, determines how many chars of the value, variableor axis labels are displayed in one line and when a line break is inserted.

sort.frq

logical, indicates whether theitems should be ordered byby highest count of first or last category ofitems.

  • Use"first.asc" to order ascending by lowest count of first category,

  • "first.desc" to order descending by lowest count of first category,

  • "last.asc" to order ascending by lowest count of last category,

  • "last.desc" to order descending by lowest count of last category,

  • orNULL (default) for no sorting.

alternate.rows

Logical, ifTRUE, rows are printed inalternatig colors (white and light grey by default).

digits

Numeric, amount of digits after decimal point when roundingvalues.

string.total

label for the total N column.

string.na

label for the missing column/row.

show.n

logical, ifTRUE, adds total number of cases for eachgroup or category to the labels.

show.total

logical, ifTRUE, an additional column with eachitem's total N is printed.

show.na

logical, ifTRUE,NA's (missing values)are added to the output.

show.skew

logical, ifTRUE, an additional column with each item's skewness is printed.The skewness is retrieved from thedescribe-functionof thepsych-package.

show.kurtosis

Logical, ifTRUE, the kurtosis for each item will also be shown (seekurtosianddescribe in thepsych-package for more details.

digits.stats

amount of digits for rounding the skewness and kurtosis valuess.Default is 2, i.e. skewness and kurtosis values have 2 digits after decimal point.

file

Destination file, if the output should be saved as file.IfNULL (default), the output will be saved as temporary file andopened either in the IDE's viewer pane or the default web browser.

encoding

Character vector, indicating the charset encoding usedfor variable and value labels. Default is"UTF-8". For WindowsSystems,encoding = "Windows-1252" might be necessary for properdisplay of special characters.

CSS

Alist with user-defined style-sheet-definitions,according to theofficial CSS syntax.See 'Details' orthis package-vignette.

use.viewer

Logical, ifTRUE, the HTML table is shown in the IDE'sviewer pane. IfFALSE or no viewer available, the HTML table isopened in a web browser.

remove.spaces

Logical, ifTRUE, leading spaces are removed from all lines in the final stringthat contains the html-data. Use this, if you want to remove parantheses for html-tags. The html-sourcemay look less pretty, but it may help when exporting html-tables to office tools.

Value

Invisibly returns

for further use.

Examples

# -------------------------------# random sample# -------------------------------# prepare data for 4-category likert scale, 5 itemslikert_4 <- data.frame(  as.factor(sample(1:4, 500, replace = TRUE, prob = c(0.2, 0.3, 0.1, 0.4))),  as.factor(sample(1:4, 500, replace = TRUE, prob = c(0.5, 0.25, 0.15, 0.1))),  as.factor(sample(1:4, 500, replace = TRUE, prob = c(0.25, 0.1, 0.4, 0.25))),  as.factor(sample(1:4, 500, replace = TRUE, prob = c(0.1, 0.4, 0.4, 0.1))),  as.factor(sample(1:4, 500, replace = TRUE, prob = c(0.35, 0.25, 0.15, 0.25))))# create labelslevels_4 <- c("Independent", "Slightly dependent",              "Dependent", "Severely dependent")# create item labelsitems <- c("Q1", "Q2", "Q3", "Q4", "Q5")# plot stacked frequencies of 5 (ordered) item-scales## Not run: if (interactive()) {  tab_stackfrq(likert_4, value.labels = levels_4, var.labels = items)  # -------------------------------  # Data from the EUROFAMCARE sample dataset  #  Auto-detection of labels  # -------------------------------  data(efc)  # recveive first item of COPE-index scale  start <- which(colnames(efc) == "c82cop1")  # recveive first item of COPE-index scale  end <- which(colnames(efc) == "c90cop9")  tab_stackfrq(efc[, c(start:end)], alternate.rows = TRUE)  tab_stackfrq(efc[, c(start:end)], alternate.rows = TRUE,               show.n = TRUE, show.na = TRUE)  # --------------------------------  # User defined style sheet  # --------------------------------  tab_stackfrq(efc[, c(start:end)], alternate.rows = TRUE,               show.total = TRUE, show.skew = TRUE, show.kurtosis = TRUE,               CSS = list(css.ncol = "border-left:1px dotted black;",                          css.summary = "font-style:italic;"))}## End(Not run)

Summary of contingency tables as HTML table

Description

Shows contingency tables as HTML file in browser or viewer pane, or saves them as file.

Usage

tab_xtab(  var.row,  var.col,  weight.by = NULL,  title = NULL,  var.labels = NULL,  value.labels = NULL,  wrap.labels = 20,  show.obs = TRUE,  show.cell.prc = FALSE,  show.row.prc = FALSE,  show.col.prc = FALSE,  show.exp = FALSE,  show.legend = FALSE,  show.na = FALSE,  show.summary = TRUE,  drop.empty = TRUE,  statistics = c("auto", "cramer", "phi", "spearman", "kendall", "pearson", "fisher"),  string.total = "Total",  digits = 1,  tdcol.n = "black",  tdcol.expected = "#339999",  tdcol.cell = "#993333",  tdcol.row = "#333399",  tdcol.col = "#339933",  emph.total = FALSE,  emph.color = "#f8f8f8",  prc.sign = "&nbsp;&#37;",  hundret = "100.0",  CSS = NULL,  encoding = NULL,  file = NULL,  use.viewer = TRUE,  remove.spaces = TRUE,  ...)sjt.xtab(  var.row,  var.col,  weight.by = NULL,  title = NULL,  var.labels = NULL,  value.labels = NULL,  wrap.labels = 20,  show.obs = TRUE,  show.cell.prc = FALSE,  show.row.prc = FALSE,  show.col.prc = FALSE,  show.exp = FALSE,  show.legend = FALSE,  show.na = FALSE,  show.summary = TRUE,  drop.empty = TRUE,  statistics = c("auto", "cramer", "phi", "spearman", "kendall", "pearson", "fisher"),  string.total = "Total",  digits = 1,  tdcol.n = "black",  tdcol.expected = "#339999",  tdcol.cell = "#993333",  tdcol.row = "#333399",  tdcol.col = "#339933",  emph.total = FALSE,  emph.color = "#f8f8f8",  prc.sign = "&nbsp;&#37;",  hundret = "100.0",  CSS = NULL,  encoding = NULL,  file = NULL,  use.viewer = TRUE,  remove.spaces = TRUE,  ...)

Arguments

var.row

Variable that should be displayed in the table rows.

var.col

Cariable that should be displayed in the table columns.

weight.by

Vector of weights that will be applied to weight all cases.Must be a vector of same length as the input vector. Default isNULL, so no weights are used.

title

String, will be used as table caption.

var.labels

Character vector with variable names, which will be usedto label variables in the output.

value.labels

Character vector (orlist of character vectors)with value labels of the supplied variables, which will be usedto label variable values in the output.

wrap.labels

Numeric, determines how many chars of the value, variableor axis labels are displayed in one line and when a line break is inserted.

show.obs

Logical, ifTRUE, observed values are shown

show.cell.prc

Logical, ifTRUE, cell percentage values are shown

show.row.prc

Logical, ifTRUE, row percentage values are shown

show.col.prc

Logical, ifTRUE, column percentage values are shown

show.exp

Logical, ifTRUE, expected values are also shown

show.legend

logical, ifTRUE, and depending on plot type andfunction, a legend is added to the plot.

show.na

logical, ifTRUE,NA's (missing values)are added to the output.

show.summary

Logical, ifTRUE, a summary row withchi-squared statistics, degrees of freedom and Cramer's V or Phicoefficient and p-value for the chi-squared statistics.

drop.empty

Logical, ifTRUE and the variable's values are labeled, values / factorlevels with no occurrence in the data are omitted from the output. IfFALSE,labeled values that have no observations are still printed in the table (with frequency0).

statistics

Name of measure of association that should be computed. Maybe one of"auto","cramer","phi","spearman","kendall","pearson" or"fisher". Seextab_statistics.

string.total

Character label for the total column / row header

digits

Amount of decimals for estimates

tdcol.n

Color for highlighting count (observed) values in table cells. Default is black.

tdcol.expected

Color for highlighting expected values in table cells. Default is cyan.

tdcol.cell

Color for highlighting cell percentage values in table cells. Default is red.

tdcol.row

Color for highlighting row percentage values in table cells. Default is blue.

tdcol.col

Color for highlighting column percentage values in table cells. Default is green.

emph.total

Logical, ifTRUE, the total column and row will be emphasized with adifferent background color. Seeemph.color.

emph.color

Logical, ifemph.total = TRUE, this color value will be usedfor painting the background of the total column and row. Default is a light grey.

prc.sign

The percentage sign that is printed in the table cells, in HTML-format.Default is"&nbsp;%", hence the percentage sign has a non-breaking-space afterthe percentage value.

hundret

Default value that indicates the 100-percent column-sums (since rounding valuesmay lead to non-exact results). Default is"100.0".

CSS

Alist with user-defined style-sheet-definitions,according to theofficial CSS syntax.See 'Details' orthis package-vignette.

encoding

String, indicating the charset encoding used for variable andvalue labels. Default isNULL, so encoding will be auto-detecteddepending on your platform (e.g.,"UTF-8" for Unix and"Windows-1252" forWindows OS). Change encoding if specific chars are not properly displayed (e.g. German umlauts).

file

Destination file, if the output should be saved as file.IfNULL (default), the output will be saved as temporary file andopened either in the IDE's viewer pane or the default web browser.

use.viewer

Logical, ifTRUE, the HTML table is shown in the IDE'sviewer pane. IfFALSE or no viewer available, the HTML table isopened in a web browser.

remove.spaces

Logical, ifTRUE, leading spaces are removed from all lines in the final stringthat contains the html-data. Use this, if you want to remove parantheses for html-tags. The html-sourcemay look less pretty, but it may help when exporting html-tables to office tools.

...

Other arguments, currently passed down to the test statistics functionschisq.test() orfisher.test().

Value

Invisibly returns

for further use.

Examples

# prepare sample data setdata(efc)# print simple cross table with labels## Not run: if (interactive()) {  tab_xtab(efc$e16sex, efc$e42dep)  # print cross table with manually set  # labels and expected values  tab_xtab(    efc$e16sex,    efc$e42dep,    var.labels = c("Elder's gender", "Elder's dependency"),    show.exp = TRUE  )  # print minimal cross table with labels, total col/row highlighted  tab_xtab(efc$e16sex, efc$e42dep, show.cell.prc = FALSE, emph.total = TRUE)  # User defined style sheet  tab_xtab(efc$e16sex, efc$e42dep,           CSS = list(css.table = "border: 2px solid;",                      css.tdata = "border: 1px solid;",                      css.horline = "border-bottom: double blue;"))  # ordinal data, use Kendall's tau  tab_xtab(efc$e42dep, efc$quol_5, statistics = "kendall")  # calculate Spearman's rho, with continuity correction  tab_xtab(    efc$e42dep,    efc$quol_5,    statistics = "spearman",    exact = FALSE,    continuity = TRUE  )}## End(Not run)

View structure of labelled data frames

Description

Save (or show) content of an imported SPSS, SAS or Stata data file,or any similar labelleddata.frame, as HTML table.This quick overview shows variable ID number, name, label,type and associated value labels. The result can beconsidered as "codeplan" of the data frame.

Usage

view_df(  x,  weight.by = NULL,  alternate.rows = TRUE,  show.id = TRUE,  show.type = FALSE,  show.values = TRUE,  show.string.values = FALSE,  show.labels = TRUE,  show.frq = FALSE,  show.prc = FALSE,  show.wtd.frq = FALSE,  show.wtd.prc = FALSE,  show.na = FALSE,  max.len = 15,  sort.by.name = FALSE,  wrap.labels = 50,  verbose = FALSE,  CSS = NULL,  encoding = NULL,  file = NULL,  use.viewer = TRUE,  remove.spaces = TRUE)

Arguments

x

A (labelled) data frame, imported byread_spss,read_sas orread_stata function,or any similar labelled data frame (seeset_labelandset_labels).

weight.by

Name of variable inx that indicated the vector ofweights that will be applied to weight all observations. Default isNULL, so no weights are used.

alternate.rows

Logical, ifTRUE, rows are printed inalternatig colors (white and light grey by default).

show.id

Logical, ifTRUE (default), the variable ID is shown inthe first column.

show.type

Logical, ifTRUE, adds information about thevariable type to the variable column.

show.values

Logical, ifTRUE (default), the variable valuesare shown as additional column.

show.string.values

Logical, ifTRUE, elements of character vectorsare also shown. By default, these are omitted due to possibly overlengthytables.

show.labels

Logical, ifTRUE (default), the value labels areshown as additional column.

show.frq

Logical, ifTRUE, an additional column withfrequencies for each variable is shown.

show.prc

Logical, ifTRUE, an additional column with percentageof frequencies for each variable is shown.

show.wtd.frq

Logical, ifTRUE, an additional column with weightedfrequencies for each variable is shown. Weights strem fromweight.by.

show.wtd.prc

Logical, ifTRUE, an additional column with weightedpercentage of frequencies for each variable is shown. Weights strem fromweight.by.

show.na

logical, ifTRUE,NA's (missing values)are added to the output.

max.len

Numeric, indicates how many values and value labels per variableare shown. Useful for variables with many different values, where the outputcan be truncated.

sort.by.name

Logical, ifTRUE, rows are sorted according to thevariable names. By default, rows (variables) are ordered according to theirorder in the data frame.

wrap.labels

Numeric, determines how many chars of the value, variableor axis labels are displayed in one line and when a line break is inserted.

verbose

Logical, ifTRUE, a progress bar is displayedwhile creating the output.

CSS

Alist with user-defined style-sheet-definitions,according to theofficial CSS syntax.See 'Details' orthis package-vignette.

encoding

Character vector, indicating the charset encoding usedfor variable and value labels. Default is"UTF-8". For WindowsSystems,encoding = "Windows-1252" might be necessary for properdisplay of special characters.

file

Destination file, if the output should be saved as file.IfNULL (default), the output will be saved as temporary file andopened either in the IDE's viewer pane or the default web browser.

use.viewer

Logical, ifTRUE, the HTML table is shown in the IDE'sviewer pane. IfFALSE or no viewer available, the HTML table isopened in a web browser.

remove.spaces

Logical, ifTRUE, leading spaces are removed from all lines in the final stringthat contains the html-data. Use this, if you want to remove parantheses for html-tags. The html-sourcemay look less pretty, but it may help when exporting html-tables to office tools.

Value

Invisibly returns

for further use.

Examples

## Not run: # init datasetdata(efc)# view variablesview_df(efc)# view variables w/o values and value labelsview_df(efc, show.values = FALSE, show.labels = FALSE)# view variables including variable typed, orderd by nameview_df(efc, sort.by.name = TRUE, show.type = TRUE)# User defined style sheetview_df(efc,        CSS = list(css.table = "border: 2px solid;",                   css.tdata = "border: 1px solid;",                   css.arc = "color:blue;"))## End(Not run)

[8]ページ先頭

©2009-2025 Movatter.jp