-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflexdashboard.Rmd
239 lines (212 loc) · 8.47 KB
/
flexdashboard.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
---
title: "Dashboard Example"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
---
```{r setup, include=FALSE}
library(dplyr)
library(ggplot2)
library(choroplethr)
library(choroplethrZip)
library(choroplethrMaps)
setwd("C:/Users/tyler/Desktop/DATA FILES")
#read in data
sales <- read.csv("train.csv", stringsAsFactors=FALSE)%>%
mutate(Postal.Code = sprintf("%05d", Postal.Code))
#summarize sales and transactions by levels
#total
Totalsales <- sales %>%
mutate(DATE = as.Date(Order.Date, "%d/%m/%Y"),
MONTH = month.abb[as.numeric(format(DATE, "%m"))],
YEAR = as.numeric(format(DATE, "%Y"))
)%>%
group_by(YEAR, MONTH)%>%
summarise(Tot.Sales = sum(Sales),
Tot.Transactions = n())
#region
Regionsales <- sales %>%
mutate(DATE = as.Date(Order.Date, "%d/%m/%Y"),
MONTH = month.abb[as.numeric(format(DATE, "%m"))],
YEAR = as.numeric(format(DATE, "%Y")))%>%
group_by(YEAR, MONTH, Region)%>%
summarise(Sales = sum(Sales),
Transactions = n())%>%
left_join(., Totalsales, by = c("YEAR", "MONTH"))%>%
mutate(`Sales Distribution` = Sales/Tot.Sales,
`Transaction Distribution` = Transactions/Tot.Transactions)
#state
Statesales <- sales %>%
mutate(DATE = as.Date(Order.Date, "%d/%m/%Y"),
MONTH = month.abb[as.numeric(format(DATE, "%m"))],
YEAR = as.numeric(format(DATE, "%Y")))%>%
group_by(YEAR, MONTH, State)%>%
summarise(Sales = sum(Sales),
Transactions = n())%>%
left_join(., Totalsales, by = c("YEAR", "MONTH"))%>%
mutate(`Sales Distribution` = Sales/Tot.Sales,
`Transaction Distribution` = Transactions/Tot.Transactions)
#segment
Segmentsales <- sales %>%
mutate(DATE = as.Date(Order.Date, "%d/%m/%Y"),
MONTH = month.abb[as.numeric(format(DATE, "%m"))],
YEAR = as.numeric(format(DATE, "%Y")))%>%
group_by(YEAR, MONTH, Segment)%>%
summarise(Sales = sum(Sales),
Transactions = n())%>%
left_join(., Totalsales, by = c("YEAR", "MONTH"))%>%
mutate(`Sales Distribution` = Sales/Tot.Sales,
`Transaction Distribution` = Transactions/Tot.Transactions)
#category
Categorysales <- sales %>%
mutate(DATE = as.Date(Order.Date, "%d/%m/%Y"),
MONTH = month.abb[as.numeric(format(DATE, "%m"))],
YEAR = as.numeric(format(DATE, "%Y")))%>%
group_by(YEAR, MONTH, Category)%>%
summarise(Sales = sum(Sales),
Transactions = n())%>%
left_join(., Totalsales, by = c("YEAR", "MONTH"))%>%
mutate(`Sales Distribution` = Sales/Tot.Sales,
`Transaction Distribution` = Transactions/Tot.Transactions)
#sub Category
Sub.Categorysales <- sales %>%
mutate(DATE = as.Date(Order.Date, "%d/%m/%Y"),
MONTH = month.abb[as.numeric(format(DATE, "%m"))],
YEAR = as.numeric(format(DATE, "%Y")))%>%
group_by(YEAR, MONTH, Sub.Category)%>%
summarise(Sales = sum(Sales),
Transactions = n())%>%
left_join(., Totalsales, by = c("YEAR", "MONTH"))%>%
mutate(`Sales Distribution` = Sales/Tot.Sales,
`Transaction Distribution` = Transactions/Tot.Transactions)
#Plots
region.sales.plot <- Regionsales %>%
filter(YEAR >=2017) %>%
mutate(YEAR = as.factor(YEAR))%>%
ggplot() +
geom_line(aes(x = MONTH, y = `Sales Distribution`, group = YEAR, color = YEAR), size = 1)+
facet_wrap(.~Region)+
scale_x_discrete(limits = month.abb)+
labs(title = "Monthly Sales Distribution by Region")+
theme_bw()+
theme(axis.text.x = element_text(angle = 90))
region.tx.plot <- Regionsales %>%
filter(YEAR >=2017) %>%
mutate(YEAR = as.factor(YEAR))%>%
ggplot() +
geom_line(aes(x = MONTH, y = `Transaction Distribution`, group = YEAR, color = YEAR), size = 1)+
facet_wrap(.~Region)+
scale_x_discrete(limits = month.abb)+
labs(title = "Monthly Transaction Distribution by Region")+
theme_bw()+
theme(axis.text.x = element_text(angle = 90))
state.sales.plot <- Statesales %>%
filter(YEAR >=2017) %>%
mutate(YEAR = as.factor(YEAR))%>%
ggplot() +
geom_line(aes(x = MONTH, y = `Sales Distribution`, group = YEAR, color = YEAR), size = 1)+
facet_wrap(.~State)+
scale_x_discrete(limits = month.abb)+
# labs(title = "Monthly Sales Distribution by State")+
geom_rect(data = subset(Statesales, State %in% c("Indiana" ,"Washington")),
fill = "yellow", alpha = 0.005, xmin = -Inf,xmax = Inf,
ymin = -Inf,ymax = Inf)+
theme_bw()+
theme(axis.text.x = element_text(angle = 90))
category.plot <- Sub.Categorysales %>%
filter(YEAR >=2017) %>%
mutate(YEAR = as.factor(YEAR))%>%
ggplot() +
geom_line(aes(x = MONTH, y = `Sales Distribution`, group = YEAR, color = YEAR), size = 1)+
facet_wrap(.~Sub.Category)+
scale_x_discrete(limits = month.abb)+
# labs(title = "Monthly Category Sales Distribution")+
geom_rect(data = subset(Sub.Categorysales, Sub.Category == "Copiers"),
fill = "yellow", alpha = 0.005, xmin = -Inf,xmax = Inf,
ymin = -Inf,ymax = Inf)+
theme_bw()+
theme(axis.text.x = element_text(angle = 90))
category.plot2 <- Sub.Categorysales %>%
filter(YEAR >=2017) %>%
mutate(YEAR = as.factor(YEAR))%>%
ggplot() +
geom_line(aes(x = MONTH, y = `Transaction Distribution`, group = YEAR, color = YEAR), size = 1)+
facet_wrap(.~Sub.Category)+
scale_x_discrete(limits = month.abb)+
labs(title = "Monthly Category Transaction Distribution")+
geom_rect(data = subset(Sub.Categorysales, Sub.Category == "Copiers"),
fill = "yellow", alpha = 0.005, xmin = -Inf,xmax = Inf,
ymin = -Inf,ymax = Inf)+
theme_bw()+
theme(axis.text.x = element_text(angle = 90))
##################################################################################3
#shipping time
shipping <- sales %>%
mutate(Order.Date = as.Date(Order.Date, "%d/%m/%Y"),
Ship.Date = as.Date(Ship.Date, "%d/%m/%Y"))%>%
mutate(Days.to.ship = Ship.Date - Order.Date)%>%
group_by(State, City, Postal.Code)%>%
summarise(`Average Days to Ship` = round(mean(Days.to.ship),1))
shippingmap2017 <- sales %>%
mutate(DATE = as.Date(Order.Date, "%d/%m/%Y"),
YEAR = as.numeric(format(DATE, "%Y")),
Order.Date = as.Date(Order.Date, "%d/%m/%Y"),
Ship.Date = as.Date(Ship.Date, "%d/%m/%Y"))%>%
filter(YEAR ==2017)%>%
mutate(Days.to.ship = Ship.Date - Order.Date)%>%
group_by(State)%>%
summarise(`value` = as.integer(round(mean(Days.to.ship),1)))%>%
rename(region = State)%>%
mutate(region = tolower(region))
shippingmap2018 <- sales %>%
mutate(DATE = as.Date(Order.Date, "%d/%m/%Y"),
YEAR = as.numeric(format(DATE, "%Y")),
Order.Date = as.Date(Order.Date, "%d/%m/%Y"),
Ship.Date = as.Date(Ship.Date, "%d/%m/%Y"))%>%
filter(YEAR ==2018)%>%
mutate(Days.to.ship = Ship.Date - Order.Date)%>%
group_by(State)%>%
summarise(`value` = as.integer(round(mean(Days.to.ship),1)))%>%
rename(region = State)%>%
mutate(region = tolower(region))
#average days to ship state map
statemap2017 <- state_choropleth(shippingmap2017,
title = "2017",
num_colors = 1,
legend = "Days to Ship")
#average days to ship state map
statemap2018 <- state_choropleth(shippingmap2018,
title = "2018",
num_colors = 1,
legend = "Days to Ship")
```
Region
======================================================================
### Monthly Sales & Transaction Distribution by Region
```{r figures-side, fig.show="hold", out.width="50%"}
region.sales.plot
region.tx.plot
```
State
======================================================================
### Monthly Sales Distribution by State
```{r echo=FALSE, error=TRUE, fig.height=16, fig.width=16, message=FALSE, warning=FALSE}
state.sales.plot
```
Category
======================================================================
### Monthly Sales Distribution by Category
```{r echo=FALSE, error=TRUE, fig.height=8, fig.width=14, message=FALSE, warning=FALSE}
category.plot
```
Shipping
======================================================================
### Average Number of Days to Ship
```{r fig.show="hold", out.width="50%"}
statemap2017
statemap2018
```
Code
======================================================================
### Average Number of Days to Ship