-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy path06-visualizations.Rmd
222 lines (179 loc) · 5.06 KB
/
06-visualizations.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
```{r db-visualizations, include = FALSE}
eval_viz <- FALSE
if(Sys.getenv("GLOBAL_EVAL") != "") eval_viz <- Sys.getenv("GLOBAL_EVAL")
```
```{r, eval = eval_viz, include = FALSE}
library(connections)
library(dplyr)
library(dbplyr)
library(dbplot)
library(ggplot2)
library(leaflet)
library(config)
```
# Data Visualizations
## Simple plot
*Practice pushing the calculations to the database*
1. Load the `connections`, `dplyr`, `dbplyr`, and `config` libraries
```{r, eval = eval_viz}
library(connections)
library(dplyr)
library(dbplyr)
library(config)
```
2. Use `connection_open()` to open a Database connection
```{r, eval = eval_viz}
con <- connection_open(
RPostgres::Postgres(),
host = "localhost",
user = get("user", config = "dev"),
password = get("pwd", config = "dev"),
port = 5432,
dbname = "postgres",
bigint = "integer"
)
```
3. Use `tbl()` to create a pointer to the **v_orders** table
```{r, eval = eval_viz}
orders <-
```
4. Use `collect()` bring back the aggregated results into a "pass-through" variable called `by_year`
```{r, eval = eval_viz}
by_year <- orders %>%
count(date_year) %>%
collect()
```
5. Preview the `by_year` variable
```{r, eval = eval_viz}
```
6. Load the `ggplot2` library
```{r, eval = eval_viz}
library(ggplot2)
```
7. Plot results using `ggplot2`
```{r, eval = eval_viz}
ggplot(by_year) +
geom_col(aes(date_year, n))
```
8. Using the code in this section, create a single piped code set which also creates the plot
```{r, eval = eval_viz}
```
## Plot in one code segment
*Practice going from `dplyr` to `ggplot2` without using pass-through variable, great for EDA*
1. Summarize the order totals in a new variable called `sales`
```{r, eval = eval_viz}
orders %>%
summarise(sales = sum(order_total))
```
2. Summarize the order totals grouped by `date_year` in a new variable called `sales`
```{r, eval = eval_viz}
orders %>%
group_by(date_year) %>%
summarise(sales = sum(order_total))
```
3. Summarize the order totals grouped by `date_year` in a new variable called `sales` and plot the results
```{r, eval = eval_viz}
```
4. Switch the calculation to reflect the average of the order sale total
```{r, eval = eval_viz}
```
## Create a histogram
*Use the `dbplot` package to easily create a histogram*
1. Load the `dbplot` package
```{r, eval = eval_viz}
library(dbplot)
```
2. Use the `dbplot_histogram()` to build the histogram
```{r, eval = eval_viz}
orders %>%
dbplot_histogram(order_total)
```
3. Adjust the `binwidth` to 10
```{r, eval = eval_viz}
```
## Raster plot
*Use `dbplot`'s raster graph*
1. Use a `dbplot_raster()` to visualize `order_qty` versus `order_total`
```{r, eval = eval_viz}
orders %>%
dbplot_raster(order_qty, order_total)
```
2. Change the plot's resolution to 10
```{r, eval = eval_viz}
```
## Using the `compute` functions
1. Use the `db_compute_raster()` function to get the underlying results that feed the plot
```{r, eval = eval_viz}
locations <- orders %>%
db_compute_raster2(customer_lon, customer_lat, resolution = 10)
```
2. Preview the `locations` variable
```{r, eval = eval_viz}
locations
```
3. Load the `leaflet` library
```{r, eval = eval_viz}
library(leaflet)
```
4. Pipe `location` into the `leaflet()` function, and then pipe that into the `addTiles()` function
```{r, eval = eval_viz}
locations %>%
leaflet() %>%
addTiles()
```
5. Add the `addRectangles()` function using the longitude and latitude variables
```{r, eval = eval_viz}
locations %>%
leaflet() %>%
addTiles() %>%
addRectangles(
~customer_lon,
~customer_lat,
~customer_lon_2,
~customer_lat_2
)
```
6. Add the `fillOpacity` argument to the `addRectangles()` step, use ``n()`` as the value for it
```{r, eval = eval_viz}
locations %>%
leaflet() %>%
addTiles() %>%
addRectangles(
~customer_lon,
~customer_lat,
~customer_lon_2,
~customer_lat_2,
fillOpacity = ~`n()`
)
```
7. Modify `fillOpacity` to be calculated as a percentage against the maximum number of orders
```{r, eval = eval_viz}
locations %>%
leaflet() %>%
addTiles() %>%
addRectangles(
~customer_lon,
~customer_lat,
~customer_lon_2,
~customer_lat_2,
fillOpacity = ~(`n()` / max(`n()`))
)
```
8. Add the `popup` argument with the following instruction as its value: `~paste0("<p>No of orders: ", `n()`,"</p>")`
```{r, eval = eval_viz}
locations %>%
leaflet() %>%
addTiles() %>%
addRectangles(
~customer_lon,
~customer_lat,
~customer_lon_2,
~customer_lat_2,
fillOpacity = ~(`n()` / max(`n()`)),
popup = ~paste0("<p>No of orders: ", `n()`,"</p>")
)
```
9. Disconnect from the database using `connection_close`
```{r}
connection_close(con)
```