gg_prop_plot: Creates a proportions ggplot of one or two variables.

Description Usage Arguments Value Examples

View source: R/gg_prop_plot.R

Description

The function uses a data set and a variable (or two variables) to generate a ggplot with proportions. It uses the "original" data set, i.e., it assumes that no prior 'count' or aggregations were performed. It detects if the data has a prior grouping variable. If a 'group_by' of one variable is detected, the function will use the grouping variable on the x-axis and the input variable as the fill aesthetics. If no prior grouping variable is detected, the function will use the input variable as the x-axis and the proportions of values as y-axis. The function fails if a more than one variable grouping is detected.

Usage

1
2
3
4
5
6
7
8
9
gg_prop_plot(
  data,
  vari,
  title = NULL,
  subtitle = NULL,
  caption = NULL,
  arrange_desc = T,
  add_labels = T
)

Arguments

data

The data set

vari

The variable on which proportions are calculated

title

The plot's title

subtitle

The plot's subtitle

caption

The plot's caption

arrange_desc

Should the plot arrange the bars in descending order (relative to proportions)

add_labels

Should labels be added on the bars (geom_label is used)

Value

A ggplot2 object

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
data <-
  tibble(fruit =
           c("apple", "orange", "orange", "paer", "mango", "mango", "mango"),
         gender =
           c("M", "M", "F", "F", "M", "F", "F"),
         favorite_color =
           c("Blue", "Red", "Green", "Blue", "Red", "Green", "Blue")
  )

data %>%
  gg_prop_plot(fruit)

data %>%
  gg_prop_plot(fruit, arrange_desc = F)

data %>%
  group_by(gender) %>%
  gg_prop_plot(fruit)

data %>%
  group_by(favorite_color, gender) %>%
  gg_prop_plot(fruit)

sarid-ins/saridr documentation built on Nov. 10, 2020, 9:07 p.m.