ggrapid: Create neat & complete ggplot visualizations with as little code as possible

ggrapid: Create neat & complete ggplot visualizations with as little code as possible

This is my note for learning {ggrapid}: Create neat & complete ggplot visualizations with as little code as possible. ggrapid offers a couple wrappers around the most commonly used ggplot functions in the course of doing an EDA or building a report:

  • plot_density
  • plot_boxplot
  • plot_deciles (with calculate_decile_table)
  • plot_correlation
  • plot_bars
  • plot_line

Density Plot

R
1
2
3
4
5
# devtools::install_github('konradsemsch/ggrapid')
library(tidyverse)
library(ggrapid)
diamonds %>%
plot_density(x = carat)

Box Plot

R
1
2
3
4
5
diamonds %>%
plot_boxplot(
x = cut, y = carat,
fill = cut
)

Decile Plot

R
1
2
3
4
5
diamonds %>%
dplyr::filter(cut %in% c("Ideal", "Premium")) %>%
calculate_decile_table(price, cut, "Ideal") %>%
plot_deciles() +
theme_ipsum(base_family = enfont)

Correlation

R
1
2
diamonds %>%
plot_correlation()

Bar Plot

R
1
2
3
4
5
6
# devtools::install_github('konradsemsch/aider')
diamonds %>%
plot_bars(x = carat,
x_type = "num",
fill = cut) +
theme_ipsum(base_family = enfont)

Line Plot

R
1
2
3
4
5
6
7
8
9
tibble(
time = 1:20,
value = rnorm(20, 0.5, 2)
) %>%
plot_line(
x = time,
y = value
) +
theme_ipsum(base_family = enfont)

Main arguments

R
1
2
3
4
5
6
7
diamonds %>%
plot_density(
x = carat,
fill = cut,
position = "stack"
) +
theme_ipsum(base_family = enfont)

R
1
2
3
4
5
6
7
diamonds %>%
plot_density(
x = carat,
fill = cut,
position = "fill"
) +
theme_ipsum(base_family = enfont)

R
1
2
3
4
5
6
7
8
9
10
11
12
diamonds %>%
plot_density(
x = carat,
fill = cut,
facet = cut,
title = "Density Plot",
subtitle = "https://czxa.top",
caption = "Source: ggplot2::diamonds",
alpha = 0.5,
vline = 1
) +
theme_ipsum(base_family = enfont)

Complete usage

R
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
50
51
52
53
library(recipes)
credit_data_nested <- credit_data %>%
select(-one_of("Home", "Marital", "Records", "Job")) %>%
gather(variable, variable_value,
one_of("Seniority", "Time", "Age", "Expenses",
"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("Density 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"
))
)
R
1
2
3
4
5
library(cowplot)
plot_grid(credit_data_nested$plot_deciles[[1]],
credit_data_nested$plot_boxplot[[1]],
credit_data_nested$plot_density[[1]],
nrow = 1)

# R

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×