-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
270 lines (215 loc) · 6.31 KB
/
README.Rmd
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE, message=FALSE, warning=FALSE}
library(tidyverse)
library(ggrapid)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/",
warning = FALSE,
message = FALSE
)
```
# ggrapid: Create neat & complete ggplot visualizations with as little code as possible
## Overview
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.
## Installation
```{r, eval = FALSE}
# Install development version from GitHub
devtools::install_github("konradsemsch/ggrapid")
```
## Main functions
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```
#### Density plot
```{r}
diamonds %>%
plot_density(x = carat)
```
#### Box-plot
```{r}
diamonds %>%
plot_boxplot(x = cut,
y = carat,
fill = cut)
```
#### Decile plot
```{r}
diamonds %>%
filter(cut %in% c("Ideal", "Premium")) %>%
calculate_decile_table(price, cut, "Ideal") %>%
plot_deciles()
```
#### Correlation
```{r}
diamonds %>%
plot_correlation()
```
#### Barplot
```{r}
diamonds %>%
plot_bars(x = carat,
x_type = "num",
fill = cut)
```
#### Lineplot
```{r}
tibble(
time = 1:20,
value = rnorm(20, 0.5, 2)
) %>%
plot_line(
x = time,
y = value
)
```
## Main arguments
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):
* fill
* facet
* position
* ticks
* angle
* title
* subtitle
* caption
* lab_x
* lab_y
* legend
* vline/ hline
* alpha
* quantile_low
* quantile_high
* theme_type
* palette
```{r}
diamonds %>%
plot_density(x = carat)
```
```{r}
diamonds %>%
plot_density(x = carat,
fill = cut,
position = "stack")
```
```{r}
diamonds %>%
plot_density(x = carat,
fill = cut,
position = "fill")
```
```{r}
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)
```
## Complete usage
You can easily iterate across selected columns and create a set of plots for your EDA file:
```{r}
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:
```{r}
credit_data_nested[1:3, ]
```
## Exemplary EDA format
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}
#### Decile analysis
```{r}
credit_data_nested$decile_table[[1]]
```
```{r}
credit_data_nested$plot_deciles[[1]]
```
#### Aditional plots
```{r}
credit_data_nested$plot_boxplot[[1]]
```
```{r}
credit_data_nested$plot_density[[1]]
```
### `r glue::glue("Variable: {credit_data_nested$variable[[2]]}")` {.tabset .tabset-fade .tabset-pills}
#### Decile analysis
```{r}
credit_data_nested$decile_table[[2]]
```
```{r}
credit_data_nested$plot_deciles[[2]]
```
#### Aditional plots
```{r}
credit_data_nested$plot_boxplot[[2]]
```
```{r}
credit_data_nested$plot_density[[2]]
```