Skip to contents

[Experimental] See proportion() for more details on the way proportions and confidence intervals are computed.

Usage

plot_proportions(
  data,
  condition,
  by = NULL,
  geom = "bar",
  ...,
  show_overall = TRUE,
  overall_label = "Overall",
  show_ci = TRUE,
  conf_level = 0.95,
  ci_color = "black",
  show_pvalues = TRUE,
  pvalues_test = c("fisher", "chisq"),
  pvalues_labeller = scales::label_pvalue(add_p = TRUE),
  pvalues_size = 3.5,
  show_labels = TRUE,
  labels_labeller = scales::label_percent(1),
  labels_size = 3.5,
  labels_color = "black",
  show_overall_line = FALSE,
  overall_line_type = "dashed",
  overall_line_color = "black",
  overall_line_width = 0.5,
  facet_labeller = ggplot2::label_wrap_gen(width = 50, multi_line = TRUE),
  flip = FALSE,
  return_data = FALSE
)

Arguments

data

A data frame, data frame extension (e.g. a tibble), or a survey design object.

condition

<data-masking> A condition defining a proportion (see examples).

by

<tidy-select> List of variables to group by (comparison is done separately for each variable).

geom

Geometry to use for plotting proportions ("bar" by default).

...

Additional arguments passed to the geom defined by geom.

show_overall

Display "Overall" column?

overall_label

Label for the overall column.

show_ci

Display confidence intervals?

conf_level

Confidence level for the confidence intervals.

ci_color

Color of the error bars representing confidence intervals.

show_pvalues

Display p-values in the top-left corner?

pvalues_test

Test to compute p-values for data frames: "fisher" for stats::fisher.test() (with simulate.p.value = TRUE) or "chisq" for stats::chisq.test(). Has no effect on survey objects for those survey::svychisq() is used.

pvalues_labeller

Labeller function for p-values.

pvalues_size

Text size for p-values.

show_labels

Display proportion labels?

labels_labeller

Labeller function for proportion labels.

labels_size

Size of proportion labels.

labels_color

Color of proportion labels.

show_overall_line

Add an overall line?

overall_line_type

Line type of the overall line.

overall_line_color

Color of the overall line.

overall_line_width

Line width of the overall line.

facet_labeller

Labeller function for strip labels.

flip

Flip x and y axis?

return_data

Return data used instead of the plot?

Examples

titanic |>
  plot_proportions(
    Survived == "Yes",
    overall_label = "All",
    labels_color = "white"
  )


titanic |>
  plot_proportions(
    Survived == "Yes",
    by = c(Class, Sex),
    fill = "lightblue"
  )


# \donttest{
titanic |>
  plot_proportions(
    Survived == "Yes",
    by = c(Class, Sex),
    fill = "lightblue",
    flip = TRUE
  )


titanic |>
  plot_proportions(
    Survived == "Yes",
    by = c(Class, Sex),
    geom = "point",
    color = "red",
    size = 3,
    show_labels = FALSE
  )


titanic |>
  plot_proportions(
    Survived == "Yes",
    by = c(Class, Sex),
    geom = "area",
    fill = "lightgreen",
    show_overall = FALSE
  )


titanic |>
  plot_proportions(
    Survived == "Yes",
    by = c(Class, Sex),
    geom = "line",
    color = "purple",
    ci_color = "darkblue",
    show_overall = FALSE
  )


titanic |>
  plot_proportions(
    Survived == "Yes",
    by = -Survived,
    mapping = ggplot2::aes(fill = variable),
    color = "black",
    show.legend = FALSE,
    show_overall_line = TRUE,
    show_pvalues = FALSE
 )


# works with survey object
titanic |>
  srvyr::as_survey() |>
  plot_proportions(
    Survived == "Yes",
    by = c(Class, Sex),
    fill = "darksalmon",
    color = "black",
    show_overall_line = TRUE
 )

# }