epi_plot_bar: Barplot wrapper function using ggplot2

Description Usage Arguments Value Note Author(s) See Also Examples

View source: R/epi_plot_bar.R

Description

epi_plot_bar() wraps ggplot2 barplot for one or two variables with a number of set preferences, see below.

Usage

1
2
3
epi_plot_bar(df = NULL, var_x = NULL, var_y = "", fill = NULL,
  bar_colour = "black", guides_fill = FALSE, y_lab = "Count",
  x_lab = var_x, ...)

Arguments

df

data.frame with x var to plot

var_x

Variable to plot on x-axis, pass as a string.

var_y

Variable to plot on y-axis, pass as a string.

fill

Interior colour used to fill. If only passing var_x it defaults to 'black'. If passing both var_y and var_x, it uses var_x.

bar_colour

Aesthetics for ggplot2. Default is 'black' if only var_x.

guides_fill

Fill for ggplot2 guides. Default is FALSE.

y_lab

y-axis label. Default is 'Count'.

x_lab

x-axis label. Default is x_var.

...

pass further arguments to ggplot2::geom_bar()

Value

Prints a ggplot2 barplot

Note

For other options, save as object and build on the layers. var_x and var_y are passed to ggplot2::aes_string. For colour and fill see ggplot2::fill for further information. epi_plot_bar() for one variable uses stat = 'count' and is coloured according to the x variable passed, black borders for bars and no legend by default. For two variables it assumes you want to colour according to the x variable and that stat = 'identity' is what's needed. No legend by default. stat = 'identity' uses the height of the bar to represent the value of the passed column.

Author(s)

Antonio Berlanga-Taylor <https://github.com/AntonioJBT/episcout>

See Also

epi_plot_list, epi_plots_to_grid, epi_plot_box, epi_plot_hist, ggplot, geom_bar.

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
## Not run: 
# Set up an example:
set.seed(12345)
n <- 20
df <- data.frame(var_id = rep(1:(n / 2), each = 2),
                 var_to_rep = rep(c("Pre", "Post"), n / 2),
                              x = rnorm(n),
                              y = rbinom(n, 1, 0.50),
                              z = rpois(n, 2),
	                            w = sample(1:20, 20)
                 )
df$id_unique <- paste0(df[['var_id']], '_', df[['var_to_rep']])
df[, 'var_id'] <- as.character(df[, 'var_id'])
df[, 'y'] <- as.factor(df[, 'y'])
str(df)

# Barplot for single variable:
summary(df$var_to_rep)
plot_bar <- epi_plot_bar(df, 'var_to_rep')
plot_bar

# Barplot for two variables side by side:
df_bar <- reshape2::melt(df[, c('w', 'z', 'id_unique')], id.vars = 'id_unique')
epi_head_and_tail(df, cols = 7)
epi_head_and_tail(df_bar, cols = 3)
plot_bar <- epi_plot_bar(df_bar,
                         var_x = 'id_unique',
                         var_y = 'value',
                         fill = 'variable') +
	theme(axis.text.x = element_text(angle = 90, hjust = 1))
plot_bar
# Which should be the same as:
ggplot(df_bar, aes(x = id_unique, y = value, fill = variable)) +
       geom_bar(stat = 'identity', position = 'dodge') +
 	theme(axis.text.x = element_text(angle = 90, hjust = 1))

## End(Not run)

AntonioJBT/episcout documentation built on Nov. 7, 2019, 5:34 p.m.