epi_plot_hist: Histogram wrapper function using ggplot2

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

View source: R/epi_plot_hist.R

Description

epi_plot_hist()

Usage

1
epi_plot_hist(df = NULL, var_x = "", ...)

Arguments

df

data.frame with x var to plot

var_x

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

...

passes arguments to ggplot2::geom_histogram()

Value

Prints a ggplot2 histogram

Note

For other options, save as object and build on the layers. var_x is passed to ggplot2::aes_string

Author(s)

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

See Also

epi_plot_list, epi_plots_to_grid, ggplot, geom_histogram, epi_plot_cow_save.

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
## Not run: 
library(ggplot2)
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)
                 )
df
df$x # continuous variable
my_hist_plot <- epi_plot_hist(df, 'x') # pass with quotes as using ggplot2::aes_string()
my_hist_plot
# Change the bins:
my_hist_plot <- epi_plot_hist(df, 'x', breaks = seq(-3, 3, by = 1))
my_hist_plot
# Add titles and axis names:
my_hist_plot <- my_hist_plot +
  labs(title = "Histogram for X") +
  labs(x = "X", y = "Count")
my_hist_plot
# Add axis limits:
my_hist_plot <- my_hist_plot +
  xlim(c(-4, 4)) +
  ylim(c(0, 10))
my_hist_plot
# Histogram with density curve:
my_hist_plot <- my_hist_plot + geom_density(col = 2)
my_hist_plot
# Histogram overlaid with kernel density curve:
# http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/
my_hist_plot <- my_hist_plot +
# Density instead of count on y-axis:
  geom_histogram(aes( y = ..density..),
                 binwidth = 0.5,
                 colour = "black",
                 fill = "white") +
  geom_density(alpha = 0.2, fill = "#FF6666") + # Overlay with transparent density plot
  ylab('Density')
my_hist_plot

## End(Not run)

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