library(tidyverse) library(ggrapid) knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/", warning = FALSE, message = FALSE )
ggrapid enables creation of the most common ggplot-based visualizations fast and with just a few lines of code. In practice the package offers wrappers of some of the most common ggplot geoms such as: geom_density, geom_boxplot, geom_bar etc. ggrapid comes handy when you'd like to do an initial and quick EDA (Exploratory Data Analysis) over various columns of your dataset programatically, without the need of writing a lot of custom ggplot code.
# Install development version from GitHub devtools::install_github("konradsemsch/ggrapid")
ggrapid offers a couple wrappers around the most commonly used functions in the course of doing an EDA:
plot_density
plot_boxplot
plot_deciles
(with calculate_decile_table
)plot_correlation
plot_bars
plot_line
diamonds %>% plot_density(x = carat)
diamonds %>% plot_boxplot(x = cut, y = carat, fill = cut)
diamonds %>% filter(cut %in% c("Ideal", "Premium")) %>% calculate_decile_table(price, cut, "Ideal") %>% plot_deciles()
diamonds %>% plot_correlation()
diamonds %>% plot_bars(x = carat, x_type = "num", fill = cut)
tibble( time = 1:20, value = rnorm(20, 0.5, 2) ) %>% plot_line( x = time, y = value )
The most commonly implemented ggplot2 arguments across all main ggrapid functions ensure that you can build your basic EDA file without making additional changes or custom functions. Those arguments are mainly (might slightly differ across functions):
diamonds %>% plot_density(x = carat)
diamonds %>% plot_density(x = carat, fill = cut, position = "stack")
diamonds %>% plot_density(x = carat, fill = cut, position = "fill")
diamonds %>% plot_density(x = carat, fill = cut, facet = cut, title = "Write your title here", subtitle = "Write your subtitle here", caption = "Write your caption here", lab_x = "Carat", alpha = .5, vline = 1)
You can easily iterate across selected columns and create a set of plots for your EDA file:
library(recipes) credit_data_nested <- credit_data %>% select(-one_of("Home", "Marital", "Records", "Job")) %>% # removing categorical variables gather(variable, variable_value, one_of("Seniority", "Time", "Age", "Expenses", # selecting variables to gather "Income", "Assets", "Debt", "Amount", "Price")) %>% nest(-variable) %>% mutate( decile_table = map(data, ~calculate_decile_table( .x, binning = variable_value, grouping = Status, top_level = "bad", format = FALSE ) ), plot_deciles = pmap(list(x = decile_table, y = variable), ~plot_deciles( .x, title = glue::glue("Decile plot of {.y}"), quantile_low = 0, quantile_high = 1, lab_x = "Decile", lab_y = "Bad rate, %" ) ), plot_boxplot = pmap(list(x = data, y = variable), ~plot_boxplot( .x, x = Status, y = variable_value, fill = Status, title = glue::glue("Box plot of {.y} by Status"), quantile_low = 0.01, quantile_high = 0.99, lab_x = "Performance", caption = "Removed 1% of observations from each side", palette = "inv_binary" ) ), plot_density = pmap(list(x = data, y = variable), ~plot_density( .x, x = variable_value, fill = Status, title = glue::glue("Box plot of {.y} by Status"), quantile_low = 0.01, quantile_high = 0.99, lab_x = "Performance", caption = "Removed 1% of observations from each side", palette = "inv_binary" ) ) )
This will give you the following structure. Each row represents an individual variable and columns are the different plots you would like to inspect:
credit_data_nested[1:3, ]
Creating a standardised EDA file is just as easy as doing something like this:
r glue::glue("Variable: {credit_data_nested$variable[[1]]}")
{.tabset .tabset-fade .tabset-pills}credit_data_nested$decile_table[[1]]
credit_data_nested$plot_deciles[[1]]
credit_data_nested$plot_boxplot[[1]]
credit_data_nested$plot_density[[1]]
r glue::glue("Variable: {credit_data_nested$variable[[2]]}")
{.tabset .tabset-fade .tabset-pills}credit_data_nested$decile_table[[2]]
credit_data_nested$plot_deciles[[2]]
credit_data_nested$plot_boxplot[[2]]
credit_data_nested$plot_density[[2]]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.