ggstats/ 0000755 0001762 0000144 00000000000 14527062732 011736 5 ustar ligges users ggstats/NAMESPACE 0000644 0001762 0000144 00000001754 14466120077 013163 0 ustar ligges users # Generated by roxygen2: do not edit by hand
export("%>%")
export(PositionLikert)
export(PositionLikertCount)
export(StatCross)
export(StatProp)
export(StatWeightedMean)
export(augment_chisq_add_phi)
export(geom_stripped_cols)
export(geom_stripped_rows)
export(ggcoef_compare)
export(ggcoef_model)
export(ggcoef_multicomponents)
export(ggcoef_multinom)
export(ggcoef_plot)
export(ggcoef_table)
export(gglikert)
export(gglikert_data)
export(gglikert_stacked)
export(ggsurvey)
export(label_number_abs)
export(label_percent_abs)
export(position_likert)
export(position_likert_count)
export(signif_stars)
export(stat_cross)
export(stat_prop)
export(stat_weighted_mean)
export(weighted.median)
export(weighted.quantile)
import(ggplot2)
importFrom(dplyr,.data)
importFrom(dplyr,sym)
importFrom(ggplot2,after_stat)
importFrom(lifecycle,deprecate_soft)
importFrom(lifecycle,deprecated)
importFrom(magrittr,"%>%")
importFrom(stats,weighted.mean)
importFrom(stats,weights)
ggstats/README.md 0000644 0001762 0000144 00000011111 14526731562 013213 0 ustar ligges users
# `ggstats`: extension to `ggplot2` for plotting stats
[](https://lifecycle.r-lib.org/articles/stages.html#stable)
[](https://github.com/larmarange/ggstats/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/larmarange/ggstats?branch=main)
[](https://CRAN.R-project.org/package=ggstats)
[](https://zenodo.org/badge/latestdoi/547360047)
The `ggstats` package provides new statistics, new geometries and new
positions for `ggplot2` and a suite of functions to facilitate the
creation of statistical plots.
## Installation & Documentation
To install **stable version**:
``` r
install.packages("ggstats")
```
Documentation of stable version:
To install **development version**:
``` r
remotes::install_github("larmarange/ggstats")
```
Documentation of development version:
## Plot model coefficients
``` r
library(ggstats)
mod1 <- lm(Fertility ~ ., data = swiss)
ggcoef_model(mod1)
```
``` r
ggcoef_table(mod1)
```
## Comparing several models
``` r
mod2 <- step(mod1, trace = 0)
mod3 <- lm(Fertility ~ Agriculture + Education * Catholic, data = swiss)
models <- list(
"Full model" = mod1,
"Simplified model" = mod2,
"With interaction" = mod3
)
ggcoef_compare(models, type = "faceted")
```
## Compute custom proportions
``` r
library(ggplot2)
ggplot(as.data.frame(Titanic)) +
aes(x = Class, fill = Survived, weight = Freq, by = Class) +
geom_bar(position = "fill") +
geom_text(stat = "prop", position = position_fill(.5)) +
facet_grid(~Sex)
```
## Compute weighted mean
``` r
data(tips, package = "reshape")
ggplot(tips) +
aes(x = day, y = total_bill, fill = sex) +
stat_weighted_mean(geom = "bar", position = "dodge") +
ylab("Mean total bill per day and sex")
```
## Compute cross-tabulation statistics
``` r
ggplot(as.data.frame(Titanic)) +
aes(
x = Class, y = Survived, weight = Freq,
size = after_stat(observed), fill = after_stat(std.resid)
) +
stat_cross(shape = 22) +
scale_fill_steps2(breaks = c(-3, -2, 2, 3), show.limits = TRUE) +
scale_size_area(max_size = 20)
```
## Plot survey objects taking into account weights
``` r
library(survey, quietly = TRUE)
#>
#> Attachement du package : 'survey'
#> L'objet suivant est masqué depuis 'package:graphics':
#>
#> dotchart
dw <- svydesign(
ids = ~1,
weights = ~Freq,
data = as.data.frame(Titanic)
)
ggsurvey(dw) +
aes(x = Class, fill = Survived) +
geom_bar(position = "fill") +
ylab("Weighted proportion of survivors")
```
## Plot Likert-type items
``` r
library(dplyr)
#>
#> Attachement du package : 'dplyr'
#> Les objets suivants sont masqués depuis 'package:stats':
#>
#> filter, lag
#> Les objets suivants sont masqués depuis 'package:base':
#>
#> intersect, setdiff, setequal, union
likert_levels <- c(
"Strongly disagree",
"Disagree",
"Neither agree nor disagree",
"Agree",
"Strongly agree"
)
set.seed(42)
df <-
tibble(
q1 = sample(likert_levels, 150, replace = TRUE),
q2 = sample(likert_levels, 150, replace = TRUE, prob = 5:1),
q3 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
q4 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
q5 = sample(c(likert_levels, NA), 150, replace = TRUE),
q6 = sample(likert_levels, 150, replace = TRUE, prob = c(1, 0, 1, 1, 0))
) %>%
mutate(across(everything(), ~ factor(.x, levels = likert_levels)))
gglikert(df)
```
ggstats/man/ 0000755 0001762 0000144 00000000000 14504775450 012514 5 ustar ligges users ggstats/man/stat_weighted_mean.Rd 0000644 0001762 0000144 00000011520 14415736053 016632 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/stat_weighted_mean.R
\docType{data}
\name{stat_weighted_mean}
\alias{stat_weighted_mean}
\alias{StatWeightedMean}
\title{Compute weighted y mean}
\usage{
stat_weighted_mean(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE
)
}
\arguments{
\item{mapping}{Set of aesthetic mappings created by \code{\link[ggplot2:aes]{aes()}}. If specified and
\code{inherit.aes = TRUE} (the default), it is combined with the default mapping
at the top level of the plot. You must supply \code{mapping} if there is no plot
mapping.}
\item{data}{The data to be displayed in this layer. There are three
options:
If \code{NULL}, the default, the data is inherited from the plot
data as specified in the call to \code{\link[ggplot2:ggplot]{ggplot()}}.
A \code{data.frame}, or other object, will override the plot
data. All objects will be fortified to produce a data frame. See
\code{\link[ggplot2:fortify]{fortify()}} for which variables will be created.
A \code{function} will be called with a single argument,
the plot data. The return value must be a \code{data.frame}, and
will be used as the layer data. A \code{function} can be created
from a \code{formula} (e.g. \code{~ head(.x, 10)}).}
\item{geom}{Override the default connection with \code{\link[ggplot2:geom_point]{ggplot2::geom_point()}}.}
\item{position}{Position adjustment, either as a string naming the adjustment
(e.g. \code{"jitter"} to use \code{position_jitter}), or the result of a call to a
position adjustment function. Use the latter if you need to change the
settings of the adjustment.}
\item{...}{Other arguments passed on to \code{\link[ggplot2:layer]{layer()}}. These are
often aesthetics, used to set an aesthetic to a fixed value, like
\code{colour = "red"} or \code{size = 3}. They may also be parameters
to the paired geom/stat.}
\item{na.rm}{If \code{FALSE}, the default, missing values are removed with
a warning. If \code{TRUE}, missing values are silently removed.}
\item{orientation}{The orientation of the layer. The default (\code{NA})
automatically determines the orientation from the aesthetic mapping. In the
rare event that this fails it can be given explicitly by setting \code{orientation}
to either \code{"x"} or \code{"y"}. See the \emph{Orientation} section for more detail.}
\item{show.legend}{logical. Should this layer be included in the legends?
\code{NA}, the default, includes if any aesthetics are mapped.
\code{FALSE} never includes, and \code{TRUE} always includes.
It can also be a named logical vector to finely select the aesthetics to
display.}
\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics,
rather than combining with them. This is most useful for helper functions
that define both data and aesthetics and shouldn't inherit behaviour from
the default plot specification, e.g. \code{\link[ggplot2:borders]{borders()}}.}
}
\value{
A \code{ggplot2} plot with the added statistic.
}
\description{
This statistic will compute the mean of \strong{y} aesthetic for
each unique value of \strong{x}, taking into account \strong{weight}
aesthetic if provided.
}
\section{Computed variables}{
\describe{
\item{y}{weighted y (numerator / denominator)}
\item{numerator}{numerator}
\item{denominator}{denominator}
}
}
\examples{
\dontshow{if (requireNamespace("reshape")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
\dontshow{\}) # examplesIf}
library(ggplot2)
data(tips, package = "reshape")
ggplot(tips) +
aes(x = day, y = total_bill) +
geom_point()
ggplot(tips) +
aes(x = day, y = total_bill) +
stat_weighted_mean()
\donttest{
ggplot(tips) +
aes(x = day, y = total_bill, group = 1) +
stat_weighted_mean(geom = "line")
ggplot(tips) +
aes(x = day, y = total_bill, colour = sex, group = sex) +
stat_weighted_mean(geom = "line")
ggplot(tips) +
aes(x = day, y = total_bill, fill = sex) +
stat_weighted_mean(geom = "bar", position = "dodge")
# computing a proportion on the fly
if (requireNamespace("scales")) {
ggplot(tips) +
aes(x = day, y = as.integer(smoker == "Yes"), fill = sex) +
stat_weighted_mean(geom = "bar", position = "dodge") +
scale_y_continuous(labels = scales::percent)
}
}
library(ggplot2)
# taking into account some weights
if (requireNamespace("scales")) {
d <- as.data.frame(Titanic)
ggplot(d) +
aes(
x = Class, y = as.integer(Survived == "Yes"),
weight = Freq, fill = Sex
) +
geom_bar(stat = "weighted_mean", position = "dodge") +
scale_y_continuous(labels = scales::percent) +
labs(y = "Survived")
}
}
\seealso{
\code{vignette("stat_weighted_mean")}
}
\keyword{datasets}
ggstats/man/label_number_abs.Rd 0000644 0001762 0000144 00000002166 14527052140 016251 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/label_number_abs.R
\name{label_number_abs}
\alias{label_number_abs}
\alias{label_percent_abs}
\title{Label absolute values}
\usage{
label_number_abs(..., hide_below = NULL)
label_percent_abs(..., hide_below = NULL)
}
\arguments{
\item{...}{arguments passed to \code{\link[scales:label_number]{scales::label_number()}} or
\code{\link[scales:label_percent]{scales::label_percent()}}}
\item{hide_below}{if provided, values below \code{hide_below} will be masked
(i.e. an empty string \code{""} will be returned)}
}
\value{
A "labelling" function, , i.e. a function that takes a vector and
returns a character vector of same length giving a label for each input value.
}
\description{
Label absolute values
}
\examples{
x <- c(-0.2, -.05, 0, .07, .25, .66)
scales::label_number()(x)
label_number_abs()(x)
scales::label_percent()(x)
label_percent_abs()(x)
label_percent_abs(hide_below = .1)(x)
}
\seealso{
\code{\link[scales:label_number]{scales::label_number()}}, \code{\link[scales:label_percent]{scales::label_percent()}}
}
ggstats/man/ggstats-package.Rd 0000644 0001762 0000144 00000001431 14467450345 016050 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ggstats-package.R
\docType{package}
\name{ggstats-package}
\alias{ggstats}
\alias{ggstats-package}
\title{ggstats: Extension to 'ggplot2' for Plotting Stats}
\description{
Provides new statistics, new geometries and new positions for 'ggplot2' and a suite of functions to facilitate the creation of statistical plots.
}
\seealso{
Useful links:
\itemize{
\item \url{https://larmarange.github.io/ggstats/}
\item \url{https://github.com/larmarange/ggstats}
\item Report bugs at \url{https://github.com/larmarange/ggstats/issues}
}
}
\author{
\strong{Maintainer}: Joseph Larmarange \email{joseph@larmarange.net} (\href{https://orcid.org/0000-0001-7097-700X}{ORCID})
}
\keyword{internal}
ggstats/man/ggcoef_model.Rd 0000644 0001762 0000144 00000044064 14505244316 015416 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ggcoef_model.R
\name{ggcoef_model}
\alias{ggcoef_model}
\alias{ggcoef_table}
\alias{ggcoef_compare}
\alias{ggcoef_multinom}
\alias{ggcoef_multicomponents}
\alias{ggcoef_plot}
\title{Plot model coefficients}
\usage{
ggcoef_model(
model,
tidy_fun = broom.helpers::tidy_with_broom_or_parameters,
tidy_args = NULL,
conf.int = TRUE,
conf.level = 0.95,
exponentiate = FALSE,
variable_labels = NULL,
term_labels = NULL,
interaction_sep = " * ",
categorical_terms_pattern = "{level}",
add_reference_rows = TRUE,
no_reference_row = NULL,
intercept = FALSE,
include = dplyr::everything(),
add_pairwise_contrasts = FALSE,
pairwise_variables = broom.helpers::all_categorical(),
keep_model_terms = FALSE,
pairwise_reverse = TRUE,
emmeans_args = list(),
significance = 1 - conf.level,
significance_labels = NULL,
show_p_values = TRUE,
signif_stars = TRUE,
return_data = FALSE,
...
)
ggcoef_table(
model,
tidy_fun = broom.helpers::tidy_with_broom_or_parameters,
tidy_args = NULL,
conf.int = TRUE,
conf.level = 0.95,
exponentiate = FALSE,
variable_labels = NULL,
term_labels = NULL,
interaction_sep = " * ",
categorical_terms_pattern = "{level}",
add_reference_rows = TRUE,
no_reference_row = NULL,
intercept = FALSE,
include = dplyr::everything(),
add_pairwise_contrasts = FALSE,
pairwise_variables = broom.helpers::all_categorical(),
keep_model_terms = FALSE,
pairwise_reverse = TRUE,
emmeans_args = list(),
significance = 1 - conf.level,
significance_labels = NULL,
show_p_values = FALSE,
signif_stars = FALSE,
table_stat = c("estimate", "ci", "p.value"),
table_header = NULL,
table_text_size = 3,
table_stat_label = NULL,
ci_pattern = "{conf.low}, {conf.high}",
table_witdhs = c(3, 2),
plot_title = NULL,
...
)
ggcoef_compare(
models,
type = c("dodged", "faceted"),
tidy_fun = broom.helpers::tidy_with_broom_or_parameters,
tidy_args = NULL,
conf.int = TRUE,
conf.level = 0.95,
exponentiate = FALSE,
variable_labels = NULL,
term_labels = NULL,
interaction_sep = " * ",
categorical_terms_pattern = "{level}",
add_reference_rows = TRUE,
no_reference_row = NULL,
intercept = FALSE,
include = dplyr::everything(),
add_pairwise_contrasts = FALSE,
pairwise_variables = broom.helpers::all_categorical(),
keep_model_terms = FALSE,
pairwise_reverse = TRUE,
emmeans_args = list(),
significance = 1 - conf.level,
significance_labels = NULL,
return_data = FALSE,
...
)
ggcoef_multinom(
model,
type = c("dodged", "faceted", "table"),
y.level_label = NULL,
tidy_fun = broom.helpers::tidy_with_broom_or_parameters,
tidy_args = NULL,
conf.int = TRUE,
conf.level = 0.95,
exponentiate = FALSE,
variable_labels = NULL,
term_labels = NULL,
interaction_sep = " * ",
categorical_terms_pattern = "{level}",
add_reference_rows = TRUE,
no_reference_row = NULL,
intercept = FALSE,
include = dplyr::everything(),
significance = 1 - conf.level,
significance_labels = NULL,
return_data = FALSE,
table_stat = c("estimate", "ci", "p.value"),
table_header = NULL,
table_text_size = 3,
table_stat_label = NULL,
ci_pattern = "{conf.low}, {conf.high}",
table_witdhs = c(3, 2),
...
)
ggcoef_multicomponents(
model,
type = c("dodged", "faceted", "table"),
component_col = "component",
component_label = NULL,
tidy_fun = broom.helpers::tidy_with_broom_or_parameters,
tidy_args = NULL,
conf.int = TRUE,
conf.level = 0.95,
exponentiate = FALSE,
variable_labels = NULL,
term_labels = NULL,
interaction_sep = " * ",
categorical_terms_pattern = "{level}",
add_reference_rows = TRUE,
no_reference_row = NULL,
intercept = FALSE,
include = dplyr::everything(),
significance = 1 - conf.level,
significance_labels = NULL,
return_data = FALSE,
table_stat = c("estimate", "ci", "p.value"),
table_header = NULL,
table_text_size = 3,
table_stat_label = NULL,
ci_pattern = "{conf.low}, {conf.high}",
table_witdhs = c(3, 2),
...
)
ggcoef_plot(
data,
x = "estimate",
y = "label",
exponentiate = FALSE,
point_size = 2,
point_stroke = 2,
point_fill = "white",
colour = NULL,
colour_guide = TRUE,
colour_lab = "",
colour_labels = ggplot2::waiver(),
shape = "significance",
shape_values = c(16, 21),
shape_guide = TRUE,
shape_lab = "",
errorbar = TRUE,
errorbar_height = 0.1,
errorbar_coloured = FALSE,
stripped_rows = TRUE,
strips_odd = "#11111111",
strips_even = "#00000000",
vline = TRUE,
vline_colour = "grey50",
dodged = FALSE,
dodged_width = 0.8,
facet_row = "var_label",
facet_col = NULL,
facet_labeller = "label_value"
)
}
\arguments{
\item{model}{a regression model object}
\item{tidy_fun}{option to specify a custom tidier function}
\item{tidy_args}{Additional arguments passed to
\code{\link[broom.helpers:tidy_plus_plus]{broom.helpers::tidy_plus_plus()}} and to \code{tidy_fun}}
\item{conf.int}{should confidence intervals be computed? (see \code{\link[broom:reexports]{broom::tidy()}})}
\item{conf.level}{the confidence level to use for the confidence
interval if \code{conf.int = TRUE}; must be strictly greater than 0
and less than 1; defaults to 0.95, which corresponds to a 95
percent confidence interval}
\item{exponentiate}{if \code{TRUE} a logarithmic scale will
be used for x-axis}
\item{variable_labels}{a named list or a named vector of custom variable labels}
\item{term_labels}{a named list or a named vector of custom term labels}
\item{interaction_sep}{separator for interaction terms}
\item{categorical_terms_pattern}{a \link[glue:glue]{glue pattern} for
labels of categorical terms with treatment or sum contrasts
(see \code{\link[broom.helpers:model_list_terms_levels]{model_list_terms_levels()}})}
\item{add_reference_rows}{should reference rows be added?}
\item{no_reference_row}{variables (accepts \link[dplyr:select]{tidyselect} notation)
for those no reference row should be added, when \code{add_reference_rows = TRUE}}
\item{intercept}{should the intercept(s) be included?}
\item{include}{variables to include. Accepts \link[dplyr:select]{tidyselect}
syntax. Use \code{-} to remove a variable. Default is \code{everything()}.
See also \code{\link[broom.helpers:all_continuous]{all_continuous()}}, \code{\link[broom.helpers:all_categorical]{all_categorical()}}, \code{\link[broom.helpers:all_dichotomous]{all_dichotomous()}}
and \code{\link[broom.helpers:all_interaction]{all_interaction()}}}
\item{add_pairwise_contrasts}{apply \code{\link[broom.helpers:tidy_add_pairwise_contrasts]{tidy_add_pairwise_contrasts()}}?
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}}}
\item{pairwise_variables}{variables to add pairwise contrasts
(accepts \link[dplyr:select]{tidyselect} notation)}
\item{keep_model_terms}{keep original model terms for variables where
pairwise contrasts are added? (default is \code{FALSE})}
\item{pairwise_reverse}{determines whether to use \code{"pairwise"} (if \code{TRUE})
or \code{"revpairwise"} (if \code{FALSE}), see \code{\link[emmeans:contrast]{emmeans::contrast()}}}
\item{emmeans_args}{list of additional parameter to pass to
\code{\link[emmeans:emmeans]{emmeans::emmeans()}} when computing pairwise contrasts}
\item{significance}{level (between 0 and 1) below which a
coefficient is consider to be significantly different from 0
(or 1 if \code{exponentiate = TRUE}), \code{NULL} for not highlighting
such coefficients}
\item{significance_labels}{optional vector with custom labels
for significance variable}
\item{show_p_values}{if \code{TRUE}, add p-value to labels}
\item{signif_stars}{if \code{TRUE}, add significant stars to labels}
\item{return_data}{if \code{TRUE}, will return the data.frame used
for plotting instead of the plot}
\item{...}{parameters passed to \code{\link[=ggcoef_plot]{ggcoef_plot()}}}
\item{table_stat}{statistics to display in the table, use any column name
returned by the tidier or \code{"ci"} for confidence intervals formatted
according to \code{ci_pattern}}
\item{table_header}{optional custom headers for the table}
\item{table_text_size}{text size for the table}
\item{table_stat_label}{optional named list of labeller functions for the
displayed statistic (see examples)}
\item{ci_pattern}{glue pattern for confidence intervals in the table}
\item{table_witdhs}{relative widths of the forest plot and the coefficients
table}
\item{plot_title}{an optional plot title}
\item{models}{named list of models}
\item{type}{a dodged plot, a faceted plot or multiple table plots?}
\item{y.level_label}{an optional named vector for labeling \code{y.level}
(see examples)}
\item{component_col}{name of the component column}
\item{component_label}{an optional named vector for labeling components}
\item{data}{a data frame containing data to be plotted,
typically the output of \code{ggcoef_model()}, \code{ggcoef_compare()}
or \code{ggcoef_multinom()} with the option \code{return_data = TRUE}}
\item{x, y}{variables mapped to x and y axis}
\item{point_size}{size of the points}
\item{point_stroke}{thickness of the points}
\item{point_fill}{fill colour for the points}
\item{colour}{optional variable name to be mapped to
colour aesthetic}
\item{colour_guide}{should colour guide be displayed
in the legend?}
\item{colour_lab}{label of the colour aesthetic in the legend}
\item{colour_labels}{labels argument passed to
\code{\link[ggplot2:scale_colour_discrete]{ggplot2::scale_colour_discrete()}} and
\code{\link[ggplot2:discrete_scale]{ggplot2::discrete_scale()}}}
\item{shape}{optional variable name to be mapped to the
shape aesthetic}
\item{shape_values}{values of the different shapes to use in
\code{\link[ggplot2:scale_manual]{ggplot2::scale_shape_manual()}}}
\item{shape_guide}{should shape guide be displayed in the legend?}
\item{shape_lab}{label of the shape aesthetic in the legend}
\item{errorbar}{should error bars be plotted?}
\item{errorbar_height}{height of error bars}
\item{errorbar_coloured}{should error bars be colored as the points?}
\item{stripped_rows}{should stripped rows be displayed in the background?}
\item{strips_odd}{color of the odd rows}
\item{strips_even}{color of the even rows}
\item{vline}{should a vertical line be drawn at 0 (or 1 if
\code{exponentiate = TRUE})?}
\item{vline_colour}{colour of vertical line}
\item{dodged}{should points be dodged (according to the colour aesthetic)?}
\item{dodged_width}{width value for \code{\link[ggplot2:position_dodge]{ggplot2::position_dodge()}}}
\item{facet_row}{variable name to be used for row facets}
\item{facet_col}{optional variable name to be used for column facets}
\item{facet_labeller}{labeller function to be used for labeling facets;
if labels are too long, you can use \code{\link[ggplot2:labellers]{ggplot2::label_wrap_gen()}} (see
examples), more information in the documentation of \code{\link[ggplot2:facet_grid]{ggplot2::facet_grid()}}}
}
\value{
A \code{ggplot2} plot or a \code{tibble} if \code{return_data = TRUE}.
}
\description{
\code{ggcoef_model()}, \code{ggcoef_table()}, \code{ggcoef_multinom()},
\code{ggcoef_multicomponents()} and \code{ggcoef_compare()}
use \code{\link[broom.helpers:tidy_plus_plus]{broom.helpers::tidy_plus_plus()}}
to obtain a \code{tibble} of the model coefficients,
apply additional data transformation and then pass the
produced \code{tibble} to \code{ggcoef_plot()} to generate the plot.
}
\details{
For more control, you can use the argument \code{return_data = TRUE} to
get the produced \code{tibble}, apply any transformation of your own and
then pass your customized \code{tibble} to \code{ggcoef_plot()}.
}
\section{Functions}{
\itemize{
\item \code{ggcoef_table()}: a variation of \code{\link[=ggcoef_model]{ggcoef_model()}} adding a table
with estimates, confidence intervals and p-values
\item \code{ggcoef_compare()}: designed for displaying several models on the same
plot.
\item \code{ggcoef_multinom()}: a variation of \code{\link[=ggcoef_model]{ggcoef_model()}} adapted to
multinomial logistic regressions performed with \code{\link[nnet:multinom]{nnet::multinom()}}.
\item \code{ggcoef_multicomponents()}: a variation of \code{\link[=ggcoef_model]{ggcoef_model()}} adapted to
multi-component models such as zero-inflated models or beta regressions.
\code{\link[=ggcoef_multicomponents]{ggcoef_multicomponents()}} has been tested with \code{pscl::zeroinfl()},
\code{pscl::hurdle()} and \code{betareg::betareg()}
\item \code{ggcoef_plot()}: plot a tidy \code{tibble} of coefficients
}}
\examples{
mod <- lm(Sepal.Length ~ Sepal.Width + Species, data = iris)
ggcoef_model(mod)
ggcoef_table(mod)
\donttest{
ggcoef_table(mod, table_stat = c("estimate", "ci"))
ggcoef_table(
mod,
table_stat_label = list(
estimate = scales::label_number(.001)
)
)
ggcoef_table(mod, table_text_size = 5, table_witdhs = c(1, 1))
# a logistic regression example
d_titanic <- as.data.frame(Titanic)
d_titanic$Survived <- factor(d_titanic$Survived, c("No", "Yes"))
mod_titanic <- glm(
Survived ~ Sex * Age + Class,
weights = Freq,
data = d_titanic,
family = binomial
)
# use 'exponentiate = TRUE' to get the Odds Ratio
ggcoef_model(mod_titanic, exponentiate = TRUE)
ggcoef_table(mod_titanic, exponentiate = TRUE)
# display intercepts
ggcoef_model(mod_titanic, exponentiate = TRUE, intercept = TRUE)
# customize terms labels
ggcoef_model(
mod_titanic,
exponentiate = TRUE,
show_p_values = FALSE,
signif_stars = FALSE,
add_reference_rows = FALSE,
categorical_terms_pattern = "{level} (ref: {reference_level})",
interaction_sep = " x "
) +
ggplot2::scale_y_discrete(labels = scales::label_wrap(15))
# display only a subset of terms
ggcoef_model(mod_titanic, exponentiate = TRUE, include = c("Age", "Class"))
# do not change points' shape based on significance
ggcoef_model(mod_titanic, exponentiate = TRUE, significance = NULL)
# a black and white version
ggcoef_model(
mod_titanic,
exponentiate = TRUE,
colour = NULL, stripped_rows = FALSE
)
# show dichotomous terms on one row
ggcoef_model(
mod_titanic,
exponentiate = TRUE,
no_reference_row = broom.helpers::all_dichotomous(),
categorical_terms_pattern =
"{ifelse(dichotomous, paste0(level, ' / ', reference_level), level)}",
show_p_values = FALSE
)
}
\dontshow{if (requireNamespace("reshape")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
\donttest{
data(tips, package = "reshape")
mod_simple <- lm(tip ~ day + time + total_bill, data = tips)
ggcoef_model(mod_simple)
# custom variable labels
# you can use the labelled package to define variable labels
# before computing model
if (requireNamespace("labelled")) {
tips_labelled <- tips \%>\%
labelled::set_variable_labels(
day = "Day of the week",
time = "Lunch or Dinner",
total_bill = "Bill's total"
)
mod_labelled <- lm(tip ~ day + time + total_bill, data = tips_labelled)
ggcoef_model(mod_labelled)
}
# you can provide custom variable labels with 'variable_labels'
ggcoef_model(
mod_simple,
variable_labels = c(
day = "Week day",
time = "Time (lunch or dinner ?)",
total_bill = "Total of the bill"
)
)
# if labels are too long, you can use 'facet_labeller' to wrap them
ggcoef_model(
mod_simple,
variable_labels = c(
day = "Week day",
time = "Time (lunch or dinner ?)",
total_bill = "Total of the bill"
),
facet_labeller = ggplot2::label_wrap_gen(10)
)
# do not display variable facets but add colour guide
ggcoef_model(mod_simple, facet_row = NULL, colour_guide = TRUE)
# works also with with polynomial terms
mod_poly <- lm(
tip ~ poly(total_bill, 3) + day,
data = tips,
)
ggcoef_model(mod_poly)
# or with different type of contrasts
# for sum contrasts, the value of the reference term is computed
if (requireNamespace("emmeans")) {
mod2 <- lm(
tip ~ day + time + sex,
data = tips,
contrasts = list(time = contr.sum, day = contr.treatment(4, base = 3))
)
ggcoef_model(mod2)
}
}
\dontshow{\}) # examplesIf}
\donttest{
# Use ggcoef_compare() for comparing several models on the same plot
mod1 <- lm(Fertility ~ ., data = swiss)
mod2 <- step(mod1, trace = 0)
mod3 <- lm(Fertility ~ Agriculture + Education * Catholic, data = swiss)
models <- list(
"Full model" = mod1,
"Simplified model" = mod2,
"With interaction" = mod3
)
ggcoef_compare(models)
ggcoef_compare(models, type = "faceted")
# you can reverse the vertical position of the point by using a negative
# value for dodged_width (but it will produce some warnings)
ggcoef_compare(models, dodged_width = -.9)
}
\dontshow{if (requireNamespace("nnet")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
\donttest{
# specific function for nnet::multinom models
mod <- nnet::multinom(Species ~ ., data = iris)
ggcoef_multinom(mod, exponentiate = TRUE)
ggcoef_multinom(mod, type = "faceted")
ggcoef_multinom(
mod,
type = "faceted",
y.level_label = c("versicolor" = "versicolor\n(ref: setosa)")
)
}
\dontshow{\}) # examplesIf}
\dontshow{if (requireNamespace("pscl")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf}
\donttest{
library(pscl)
data("bioChemists", package = "pscl")
mod <- zeroinfl(art ~ fem * mar | fem + mar, data = bioChemists)
ggcoef_multicomponents(mod)
ggcoef_multicomponents(mod, type = "f")
ggcoef_multicomponents(mod, type = "t")
ggcoef_multicomponents(
mod,
type = "t",
component_label = c(conditional = "Count", zero_inflated = "Zero-inflated")
)
mod2 <- zeroinfl(art ~ fem + mar | 1, data = bioChemists)
ggcoef_multicomponents(mod2, type = "t")
}
\dontshow{\}) # examplesIf}
}
\seealso{
\code{vignette("ggcoef_model")}
}
ggstats/man/gglikert.Rd 0000644 0001762 0000144 00000017604 14504775450 014623 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/gglikert.R
\name{gglikert}
\alias{gglikert}
\alias{gglikert_data}
\alias{gglikert_stacked}
\title{Plotting Likert-type items}
\usage{
gglikert(
data,
include = dplyr::everything(),
weights = NULL,
y = ".question",
variable_labels = NULL,
sort = c("none", "ascending", "descending"),
sort_method = c("prop", "mean", "median"),
sort_prop_include_center = totals_include_center,
exclude_fill_values = NULL,
add_labels = TRUE,
labels_size = 3.5,
labels_color = "black",
labels_accuracy = 1,
labels_hide_below = 0.05,
add_totals = TRUE,
totals_size = labels_size,
totals_color = "black",
totals_accuracy = labels_accuracy,
totals_fontface = "bold",
totals_include_center = FALSE,
totals_hjust = 0.1,
y_reverse = TRUE,
y_label_wrap = 50,
reverse_likert = FALSE,
width = 0.9,
facet_rows = NULL,
facet_cols = NULL,
facet_label_wrap = 50
)
gglikert_data(
data,
include = dplyr::everything(),
weights = NULL,
variable_labels = NULL,
sort = c("none", "ascending", "descending"),
sort_method = c("prop", "mean", "median"),
sort_prop_include_center = TRUE,
exclude_fill_values = NULL
)
gglikert_stacked(
data,
include = dplyr::everything(),
weights = NULL,
y = ".question",
variable_labels = NULL,
sort = c("none", "ascending", "descending"),
sort_method = c("prop", "mean", "median"),
sort_prop_include_center = FALSE,
add_labels = TRUE,
labels_size = 3.5,
labels_color = "black",
labels_accuracy = 1,
labels_hide_below = 0.05,
add_median_line = FALSE,
y_reverse = TRUE,
y_label_wrap = 50,
reverse_fill = TRUE,
width = 0.9
)
}
\arguments{
\item{data}{a data frame}
\item{include}{variables to include, accept \link[dplyr:select]{tidy-select}
syntax}
\item{weights}{optional variable name of a weighting variable,
accept \link[dplyr:select]{tidy-select} syntax}
\item{y}{name of the variable to be plotted on \code{y} axis (relevant when
\code{.question} is mapped to "facets, see examples),
accept \link[dplyr:select]{tidy-select} syntax}
\item{variable_labels}{a named list or a named vector of custom variable
labels}
\item{sort}{should variables be sorted?}
\item{sort_method}{method used to sort the variables: \code{"prop"} sort according
to the proportion of answers higher than the centered level, \code{"mean"}
considers answer as a score and sort according to the mean score, \code{"median"}
used the median and the majority judgment rule for tie-breaking.}
\item{sort_prop_include_center}{when sorting with \code{"prop"} and if the number
of levels is uneven, should half of the central level be taken into account
to compute the proportion?}
\item{exclude_fill_values}{Vector of values that should not be displayed
(but still taken into account for computing proportions),
see \code{\link[=position_likert]{position_likert()}}}
\item{add_labels}{should percentage labels be added to the plot?}
\item{labels_size}{size of the percentage labels}
\item{labels_color}{color of the percentage labels}
\item{labels_accuracy}{accuracy of the percentages, see
\code{\link[scales:label_percent]{scales::label_percent()}}}
\item{labels_hide_below}{if provided, values below will be masked, see
\code{\link[=label_percent_abs]{label_percent_abs()}}}
\item{add_totals}{should the total proportions of negative and positive
answers be added to plot? \strong{This option is not compatible with facets!}}
\item{totals_size}{size of the total proportions}
\item{totals_color}{color of the total proportions}
\item{totals_accuracy}{accuracy of the total proportions, see
\code{\link[scales:label_percent]{scales::label_percent()}}}
\item{totals_fontface}{font face of the total proportions}
\item{totals_include_center}{if the number of levels is uneven, should half
of the center level be added to the total proportions?}
\item{totals_hjust}{horizontal adjustment of totals labels on the x axis}
\item{y_reverse}{should the y axis be reversed?}
\item{y_label_wrap}{number of characters per line for y axis labels, see
\code{\link[scales:label_wrap]{scales::label_wrap()}}}
\item{reverse_likert}{if \code{TRUE}, will reverse the default stacking order,
see \code{\link[=position_likert]{position_likert()}}}
\item{width}{bar width, see \code{\link[ggplot2:geom_bar]{ggplot2::geom_bar()}}}
\item{facet_rows, facet_cols}{A set of variables or expressions quoted by
\code{\link[ggplot2:vars]{ggplot2::vars()}} and defining faceting groups on the rows or columns
dimension (see examples)}
\item{facet_label_wrap}{number of characters per line for facet labels, see
\code{\link[ggplot2:labellers]{ggplot2::label_wrap_gen()}}}
\item{add_median_line}{add a vertical line at 50\%?}
\item{reverse_fill}{if \code{TRUE}, will reverse the default stacking order,
see \code{\link[ggplot2:position_stack]{ggplot2::position_fill()}}}
}
\value{
A \code{ggplot2} plot or a \code{tibble}.
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#experimental}{\figure{lifecycle-experimental.svg}{options: alt='[Experimental]'}}}{\strong{[Experimental]}}
}
\details{
Combines several factor variables using the same list of ordered levels
(e.g. Likert-type scales) into a unique data frame and generates a centered
bar plot.
You could use \code{gglikert_data()} to just produce the dataset to be plotted.
If variable labels have been defined (see \code{\link[labelled:var_label]{labelled::var_label()}}), they will
be considered. You can also pass custom variables labels with the
\code{variable_labels} argument.
}
\examples{
library(ggplot2)
library(dplyr)
likert_levels <- c(
"Strongly disagree",
"Disagree",
"Neither agree nor disagree",
"Agree",
"Strongly agree"
)
set.seed(42)
df <-
tibble(
q1 = sample(likert_levels, 150, replace = TRUE),
q2 = sample(likert_levels, 150, replace = TRUE, prob = 5:1),
q3 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
q4 = sample(likert_levels, 150, replace = TRUE, prob = 1:5),
q5 = sample(c(likert_levels, NA), 150, replace = TRUE),
q6 = sample(likert_levels, 150, replace = TRUE, prob = c(1, 0, 1, 1, 0))
) \%>\%
mutate(across(everything(), ~ factor(.x, levels = likert_levels)))
gglikert(df)
gglikert(df, include = q1:3)
gglikert(df, sort = "ascending")
\donttest{
gglikert(df, sort = "ascending", sort_prop_include_center = TRUE)
gglikert(df, sort = "ascending", sort_method = "mean")
gglikert(df, reverse_likert = TRUE)
gglikert(df, add_totals = FALSE, add_labels = FALSE)
gglikert(
df,
totals_include_center = TRUE,
totals_hjust = .25,
totals_size = 4.5,
totals_fontface = "italic",
totals_accuracy = .01,
labels_accuracy = 1,
labels_size = 2.5,
labels_hide_below = .25
)
gglikert(df, exclude_fill_values = "Neither agree nor disagree")
if (require("labelled")) {
df \%>\%
set_variable_labels(
q1 = "First question",
q2 = "Second question"
) \%>\%
gglikert(
variable_labels = c(
q4 = "a custom label",
q6 = "a very very very very very very very very very very long label"
),
y_label_wrap = 25
)
}
# Facets
df_group <- df
df_group$group <- sample(c("A", "B"), 150, replace = TRUE)
gglikert(df_group, q1:q6, facet_rows = vars(group))
gglikert(df_group, q1:q6, facet_cols = vars(group))
gglikert(df_group, q1:q6, y = "group", facet_rows = vars(.question))
}
gglikert_stacked(df, q1:q6)
gglikert_stacked(df, q1:q6, add_median_line = TRUE, sort = "asc")
\donttest{
gglikert_stacked(df_group, q1:q6, y = "group", add_median_line = TRUE) +
facet_grid(rows = vars(.question))
}
}
\seealso{
\code{vignette("gglikert")}, \code{\link[=position_likert]{position_likert()}}, \code{\link[=stat_prop]{stat_prop()}}
}
ggstats/man/figures/ 0000755 0001762 0000144 00000000000 14526731562 014160 5 ustar ligges users ggstats/man/figures/README-unnamed-chunk-7-1.png 0000644 0001762 0000144 00000012325 14526731560 020661 0 ustar ligges users PNG
IHDR MR/ PLTE : f :f : f 333: : :: f:f:f:MMMMMnMMMnMnMf f :f ff: f::ff:fffffffnMMnMnnMnnnnnnMMMnMnMM: :::fff۶nMnnnMȫf f:fffې۶ȎMې:ېfnvmfȎې pHYs od pIDATx흍V@[R3>Ru$bC8/AO=ֻXۤE=}Ke ' Ai:
B!P4
&2m
n
uZu;%z P[J RJuRBE(-@QGo)%N
:zK)!P[J RJuRBE(-@QGo)%:zK)!P[J &@#mJ@c!Фh,@BhR4M
BI!X4)&@c!Фh,@BhR4M
BI!X4)&@c!Фh,bh,J[J hRB(Fo)%J[J hRB(Fo)%J[J hRt˲s" @kꓣlp/;}ƾH%Z=wUۻz{M_IZc|>+,{6
+QVJ07"PVWЫ7S4D:}?z*8
4@}6jjM?Ӿcڀ@S!$ݕh*T)J4MCw%
զn
ơMjS7@] @Bq讄@SAM!8tWB Pmh+!T6uS4ݕh*TZpSզ%@wxI;%z Pmj^NTZpSզ%@wxI;%z Pmj^NTZpSզ%@wxI;%z Pmj^NTZpSզ%@wxI;%z Pmj^NTZpSզ%@wxI;%z Pmj^NTZpS@7GĕtD-%`զ%@wxI;%z Pmj^NTZpSզ%@wxI;%z Pmj^NTZpSզ%@wxI;%z Pmj^NTZpSզ%@QVQTQTQTK:G'Pj@/?Nҁfރ(Uj@5m8ZV]JԲ@ՌʦZԲZabqTyzNE]?'l&Ӽx哪LF^vnWm;vz u@IQo#<
Hh84w
{uMu0b]˟)%4-wozh9*
q;͔^^㝷@-Á&P5&P5&P5{uMtlB/UCҁf`^&ZV]m%P{a(ZV:z~0~@-^#EfT6ҁfcw˨=j@/X]LVBk:]Lk & _@+g_XR:,~{@uH@G\ţ-˟/(ţ =\>;CGeuS+h{:rs떽fM!8ģ7|\
Xqz
Բz@@X
1G4
/WPw?ݛ@QVhcum5M5M5Mu0жMPWEZV]'PԢ:O_dn@Qwo=?;ɮs{M^=j^-hqCYvQÜHP$z
jYݱC@F^SL
,]fL2cEXF!PԵfT6բr uTͨlrUgFeS%>tVQVK:ߞ^(ZVw 5cP+4_AC/'Pj@ԶZ6IeVyPUjjYMjFeSMjFeSMjFeSMjFeS@Of> PNzGjY@vC|>̹jAN_-՝\Ane5Mu'=9ˮ4>=Z|@Բ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@Ռʦ@ՌʦZAwIP%`E][`%PjU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*jU3*nz9[|@՝p/;}@-;ۓl@-;YvQ9 |gcſlpSӪ-+cc_cT6y6*7)i-'!5ſ >oUnnSӪ-kmUnnSӪ[
6k'WMNnD:zK)!P[J RJuRBE(-@QGo)%:zK)!P[J RJuRz"u!Pԝ@Qw@@i:
B!P4)~ysC$\n q}tiwfTjw]e5))H= gK
͔[U-բ~}+苽Xh>dxQȋ7=S0t.'DZ>_L+zTm@E! Cr+@N(,幘@`(}sZ -L/׳bXKzE
8?t0ٓ%oފWINCi:
B!P4
@2,Wօ@E кZAyzv6?7ޒ[@kp9|9a?vl(2֠8a.ԋ'S@5?,{>
ZE<|-G4h
C|ą:##|IkQC6qOaqi?{*#|