plot_bars: Plot bar-plots of numerical variables

Description Usage Arguments Examples

Description

This function creates nicely formatted, standardised bar-plots.

Usage

1
2
3
4
5
6
7
plot_bars(df, x, y = NULL, y_prop = FALSE, x_type = "num",
  fill = NULL, facet = NULL, binwidth = NULL, position = "stack",
  stat = "count", angle = 0, title = TRUE, subtitle = NULL,
  caption = NULL, lab_x = "Value range", lab_y = "Proportion",
  legend = TRUE, vline = c(NaN), alpha = 1, fct_order = FALSE,
  quantile_low = 0.025, quantile_high = 0.975,
  palette = "cartography", theme_type = "grey")

Arguments

df

A data frame

x

A numeric/ categorical variable for which the bar graph is to be plotted

y

A numeric variable which contains summarised y values, used only with stat ="identity"

y_prop

A logical variable to choose between counts/proportion on y axis, Defaults to FALSE (proportion)

x_type

Character identifier for type of the variable x defined above: "num" for numeric (plots histogram) and "char" for character (plots bar chart). Defauls to "num"

fill

Select an additional grouping variable to be used for plotting. Defaults to NULL

facet

Select an additional faceting variable to create facets. Defaults to NULL

binwidth

Select binwidth, defaults to NULL and let's ggplot select the optimal binwidth

position

Select the position of the barplot from: For numeric variables : "stack" (default), "dodge" or "fill".

stat

Character identifier for whether the data is already grouped ("identity") or if the function needs to aggregate data at the level of x ("count")

angle

Select the rotation angle for the x axis labels. Defaults to 0

title

Should the plot title appear automatically. Defaults to TRUE

subtitle

Text that is displayed on the subtitle. Defaults to NULL

caption

Text that is displayed on the caption. Defaults to NULL

lab_x

Text that is displayed on the x axis. Defaults to "Level"

lab_y

Text that is displayed on the y axis. Defaults to "Value range"

legend

Should the plot legend appear automatically. Defaults to TRUE

vline

Should any horizontal lines be added to the plot. Defaults to c(NaN)

alpha

Select plot fill transparency. Defaults to 1

fct_order

Should the factors be reordered by their frequency? Defaults to FALSE

quantile_low

Select lower percentile for outliers exclusion. Defaults to 2.5%

quantile_high

Select upper percentile for outliers exclusion. Defaults to 97.5%

palette

Select a color palette from colors available in the select_palette function or provide your own as a nammed vector

theme_type

Select a theme type from themes available in the aider_theme function

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
data <- recipes::credit_data %>%
 first_to_lower()

df_sum <- data %>%
 group_by(marital) %>%
 summarise(mean_inc = mean(income, na.rm = TRUE))

data %>%
 plot_bars(x = income,
           x_type = "num",
           fill = marital,
           facet = job)
data %>%
 plot_bars(x = income,
           x_type = "num",
           fill = marital,
           facet = job,
           position = "stack",
           binwidth = 50,
           vline = 45,
           angle = 45,
           alpha = .7,
           palette = "berlin")

data %>%
 plot_bars(x = job,
          x_type = "char",
          y_prop = FALSE) # for generating counts

data %>%
 plot_bars(x = job,
          x_type = "char",
          position = "dodge",
          fill = marital,
          facet = status)

data %>%
 plot_bars(x = job,
           x_type = "char",
           y_prop = TRUE,
           position = "fill",
           fill = marital,
           facet = status)  # for generating proportions

df_sum %>%
 plot_bars(x = marital,
           y = mean_inc,
           x_type = "char",
           stat ="identity")

konradsemsch/aider documentation built on May 22, 2019, 2:40 p.m.