-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSales_Report.Rmd
364 lines (259 loc) · 7.39 KB
/
Sales_Report.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
---
title: "Sales Performance Analysis 2010-2011"
output:
flexdashboard::flex_dashboard:
# orientation: rows
vertical_layout: fill
runtime: shiny
---
```{r global, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(tidyverse)
library(plotly)
library(ggmap)
library(knitr)
library(leaflet)
library(DT)
library(sp)
library(rnaturalearth)
library(highcharter)
library(shiny)
library(factoextra)
library(plotly)
library(sf)
library(tmap)
# import the data
# import the data
dashboard_df<-read_csv("Data/Main_Dashboard_Data.csv")
df_KMeans<-read_csv("Data/KMeans_Data.csv")
# amend the data types
dashboard_df$Price<- round(dashboard_df$Price)
# Map set up
map <- ne_countries()
#names(map)[names(map) == "iso_a3"] <- "ISO3"
#names(map)[names(map) == "name"] <- "NAME"
CountryData<- aggregate(Price ~ Country, dashboard_df, sum)
map$Revenue <- CountryData$Price[match(map$name, CountryData$Country)]
# scale the K-means data
KM_df <-
df_KMeans %>%
subset(select = - CustomerId) %>%
scale()
```
Product Performance {data-icon="fa-chart-line"}
==========================================================
Column {data-width= 200}
--------------------------------------
### Number of Transactions
```{r}
AnnualSales <- dashboard_df %>%
distinct(Invoice) %>%
nrow()
valueBox(value= AnnualSales,icon = "fa-usd", caption ="Number of Transactions",color = "Coral")
```
### Gross Annual Revenue
```{r}
AnnualRevenue <- scales::dollar(sum(dashboard_df$Price)-sum((filter(dashboard_df,dashboard_df$Quantity < 0)$Price)))
valueBox(value = AnnualRevenue,icon = "fa-usd",caption = "Gross Annual Revenue", color = 'CornflowerBlue')
```
### Refunds
```{r}
refund <- round((sum((filter(dashboard_df,dashboard_df$Quantity < 0)$Price))/sum(dashboard_df$Price))*100,1)
gauge(refund, min = 0, max = 100, symbol = '%', gaugeSectors(
success = c(80, 100), warning = c(40, 79), danger = c(0, 39)
))
```
### Global Product Performance
```{r}
revenue<- aggregate(cbind(Quantity,Price) ~ Description, dashboard_df, sum)%>%
filter(Quantity>0 & Price>0)
renderDT ({
DT::datatable(revenue,
colnames=c('Product Description' = 'Description', 'Revenue' = 'Price'),
fillContainer=TRUE,
rownames = FALSE, options = list(
pageLength = 20,
lengthMenu = c(10,20,50)
))
})
```
Column {.sidebar data-width=100}
-------------------------------------
```{r}
minvalue <- floor(min(map$Revenue, na.rm = TRUE))
maxvalue <- ceiling(max(map$Revenue, na.rm = TRUE))
sliderInput("rangevalues",
label = "Revenue values:",
min = minvalue, max = maxvalue,
value = c(minvalue, maxvalue)
)
```
Row {data-height=650}
-------------------------------------
### Map
```{r}
pal <- colorBin(
palette = "viridis", domain = map$Revenue ,
#bins = seq(0, max(map$Revenue, na.rm = TRUE) + 10, by = 10)
)
map$labels <- paste0(
"<strong> Country: </strong> ",
map$name, "<br/> ",
"<strong> Revenue: </strong> ",
map$Revenue , "<br/> "
) %>%
lapply(htmltools::HTML)
mapFiltered <- reactive({
rowsinrangeslider <- which(map$Revenue >= input$rangevalues[1] &
map$Revenue <= input$rangevalues[2])
map[rowsinrangeslider, ]
})
renderLeaflet({
if (nrow(mapFiltered()) == 0) {
return(NULL)
}
leaflet(mapFiltered()) %>%
addTiles() %>%
setView(lng = 0, lat = 30, zoom = 2) %>%
addPolygons(
fillColor = ~ pal(Revenue),
color = "white",
fillOpacity = 0.7,
label = ~labels,
highlight = highlightOptions(
color = "black",
bringToFront = TRUE
)
) %>%
leaflet::addLegend(
pal = pal, values = ~Revenue,
opacity = 0.7, title = "Revenue"
)
})
```
Row {data-height=350}
-------------------------------------
### Revenue monthly performance
```{r}
time_revenue<-aggregate(Price ~ InvoiceDate, dashboard_df, sum)
renderHighchart({
time_revenue%>%
hchart('line', hcaes(x = 'InvoiceDate', y = 'Price'))
})
```
### Product demand monthly performance
```{r}
time_demand<-aggregate(Quantity ~ InvoiceDate, dashboard_df, sum)
renderHighchart({
time_demand%>%
hchart('line', hcaes(x = 'InvoiceDate', y = 'Quantity'))
})
```
RFM Customer Segmentation {data-icon="fas fa-users"}
===================================================
Column {.sidebar data-width=100}
-------------------------------------
```{r}
numericInput('clusters', 'Number of Segments', 3,
min = 2, max = 8)
```
Row
-------------------------------------
### Segments
```{r}
palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
"#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
clusters <- reactive({
kmeans(KM_df, input$clusters, nstart = 25)
})
renderPlot({
#par(mar = c(5.1, 4.1, 0, 1))
fviz_cluster(clusters(), geom = "point", data = KM_df)
})
```
Row
-------------------------------------
### Segments Summary
```{r}
# exclude the customer id column
summary_K2 <- reactive({ df_KMeans%>%
subset(select = -CustomerId)%>%
mutate(Segment = clusters()$cluster) %>%
group_by(Segment) %>%
summarise_all(list(n="length", mean="mean"))%>%
subset(select = -c(recency_n, frequency_n))%>%
rename(segment_size = total_value_n )
})
# transpose the summary table to facilitate the visualisation
summary_K_transposed2 <- reactive({ gather(summary_K2(),
key = "feature",
value = "value",
-Segment)
})
renderTable({
head(summary_K_transposed2(), n=32)
})
```
### Segments Visualisation
```{r}
renderPlot({
ggplot(summary_K_transposed2(),aes(x=reorder(Segment,-value),y=value, fill=as.factor(Segment)))+
geom_col()+
facet_grid(.~feature)+
labs(x="Segments", subtitle = paste("Total N of customers: ", sum(summary_K2()$segment_size)))
})
```
Market Analysis {data-icon="fas fa-globe"}
===================================================
Column {data-width= 600}
-------------------------------------
### Non UK Market segment performance
```{r}
segment_K <- reactive({ df_KMeans%>%
mutate(Segment = clusters()$cluster) %>%
group_by(Segment)%>%
inner_join(y = dashboard_df )%>%
distinct(CustomerId, .keep_all = TRUE)%>%
select(Segment, Country)
})
# Split the segment int o2 for UK and non-UK countries
segmented_noUK<-reactive({segment_K()})
renderPlot(segmented_noUK()%>%
filter(Country != "United Kingdom")%>%
ggplot(aes(Country, Segment, fill = as.factor(Segment)))+
geom_col()+
labs(title="Segments performance for the rest of the world", ylab = "Segment"))
```
### UK Market segment performance
```{r}
segmented_UK<-reactive({segment_K()})
renderPlot(segmented_UK()%>%
filter(Country == "United Kingdom")%>%
ggplot(aes(Segment,fill = as.factor(Segment)))+
geom_bar()+
labs(title="Segments performance for UK"))
```
Column {data-width=350}
-------------------------------------
### Product performance per segment
```{r}
segment_product <- reactive({ df_KMeans%>%
mutate(Segment = clusters()$cluster) %>%
group_by(Segment)%>%
inner_join(y = dashboard_df )%>%
filter(Quantity>0 & Price>0)%>%
select(Segment,Description, Price, Quantity )%>%
group_by(Segment,Description) %>%
summarise_each(funs(sum))
})
renderDT ({
DT::datatable(segment_product(),
colnames=c('Product Description' = 'Description', 'Revenue' = 'Price'),
fillContainer=TRUE,
rownames = FALSE, options = list(
pageLength = 20,
lengthMenu = c(10, 15, 20,50,100)
))
})
```