-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.R
More file actions
284 lines (238 loc) · 7.57 KB
/
utils.R
File metadata and controls
284 lines (238 loc) · 7.57 KB
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
# Ajouter les fonctions spécifiques à l'application ici
tech_sales_data <- read_excel("data/Tech Sales Data.xlsx")
# Define the function
plot_total_sales_single_column <- function(data) {
# Calculate total sales
total_sales <- data %>%
summarize(Total_Sales = sum(Sales, na.rm = TRUE)) %>%
pull(Total_Sales)
# Create the Plotly column chart
plot <- plot_ly(
x = "Total Sales",
y = total_sales,
type = "bar",
text = ~paste0("$", format(total_sales, big.mark = " ")),
marker = list(color = "darkorange")
) %>%
layout(
title = "Total Sales",
xaxis = list(title = ""),
yaxis = list(title = "Total Sales"),
bargap = 0.2
)
return(plot)
}
# Example usage
# plot_total_sales_single_column(tech_sales_data)
# Define the function
plot_sales_by_store_donut <- function(data) {
# Summarize sales by store
sales_by_store <- data %>%
group_by(Store) %>%
summarize(Total_Sales = sum(Sales, na.rm = TRUE)) %>%
arrange(desc(Total_Sales))
# Create the Plotly donut chart
plot <- plot_ly(
sales_by_store,
labels = ~Store,
values = ~Total_Sales,
type = "pie",
hole = 0.5
) %>%
layout(
title = "Sales by Store",
showlegend = TRUE,
annotations = list(
list(
text = "",
font = list(size = 20),
showarrow = FALSE,
x = 0.5,
y = 0.5
)
)
)
return(plot)
}
# Example usage
# plot_sales_by_store_donut(tech_sales_data)
# Define the function
plot_sales_by_store_region <- function(data, region) {
# Filter the data for the specified region and aggregate sales by store
region_data <- data %>%
filter(Region == region) %>%
group_by(Store) %>%
summarise(Total_Sales = sum(Sales, na.rm = TRUE)) %>%
arrange(desc(Total_Sales))
# Create the stacked column chart
fig <- plot_ly(
region_data,
x = ~"Total Sales",
y = ~Total_Sales,
type = 'bar',
name = ~Store,
text = ~paste0("$", format(Total_Sales, big.mark = " ")),
textposition = "inside",
marker = list(colors = c("rgb(55, 83, 109)", "rgb(255, 127, 14)", "rgb(26, 118, 255)"))
)
# Customize the layout
fig <- fig %>% layout(
#title = paste("Total Sales by Store in", region),
title = "",
xaxis = list(title = ""),
yaxis = list(title = "Total Sales"),
barmode = 'stack',
showlegend = FALSE
)
# Return the chart
fig
}
# Example usage
# plot_sales_by_store_region(tech_sales_data, "England")
# Define the function
plot_top_sales_month <- function(data) {
# Aggregate sales by month
monthly_sales <- data %>%
mutate(Month = as.Date(format(Month, "%Y-%m-01"))) %>% # Ensure Month is standardized to the first day of each month
group_by(Month) %>%
summarise(Total_Sales = sum(Sales, na.rm = TRUE)) %>%
arrange(desc(Total_Sales))
# Identify the top sales month
top_month <- monthly_sales %>%
slice_max(Total_Sales, n = 1)
# Create the column chart
fig <- plot_ly(
top_month,
x = ~format(Month, "%B %Y"), # Format the month for display
y = ~Total_Sales,
type = 'bar',
text = ~paste0("$", format(Total_Sales, big.mark = " ")),
textposition = "inside",
marker = list(color = "rgb(55, 83, 109)")
)
# Customize the layout
fig <- fig %>% layout(
title = "Top Sales Month",
xaxis = list(title = "Month"),
yaxis = list(title = "Total Sales"),
showlegend = FALSE
)
# Return the chart
fig
}
# Example usage
# plot_top_sales_month(tech_sales_data)
# Define the function
plot_category_trends <- function(data) {
# Add "All Regions" as a combined region
all_regions_data <- data %>%
group_by(Category, Month) %>%
summarise(Total_Sales = sum(Sales, na.rm = TRUE), .groups = "drop") %>%
mutate(Region = "All Regions")
# Combine with the original data
combined_data <- data %>%
mutate(Month = as.Date(format(Month, "%Y-%m-01"))) %>% # Standardize Month
bind_rows(all_regions_data)
# Aggregate sales by Region, Category, and Month
trend_data <- combined_data %>%
group_by(Region, Category, Month) %>%
summarise(Total_Sales = sum(Sales, na.rm = TRUE), .groups = "drop")
# Create the ggplot object
gg <- ggplot(trend_data, aes(x = Month, y = Total_Sales, group = Category)) +
geom_line(aes(color = Region, linewidth = 0.6)) + # Replace `size` with `linewidth`
geom_point(size = 1) +
facet_grid(Category ~ Region, scales = "free_y") +
scale_color_manual(values = c(
"All Regions" = "black",
"England" = "blue",
"Scotland" = "brown",
"NI" = "gold",
"Wales" = "green"
)) +
labs(
title = "Category Sales Trends Across Regions",
x = "Month",
y = "Total Sales"
) +
theme_void() +
theme(legend.position = "none")
# Convert ggplot object to an interactive plotly object
ggplotly(gg)
}
# Example usage
# plot_category_trends(tech_sales_data)
# Define the function
plot_top_managers <- function(data) {
# Aggregate sales by manager
top_managers <- data %>%
group_by(Manager) %>%
summarise(Total_Sales = sum(Sales, na.rm = TRUE), .groups = "drop") %>%
arrange(desc(Total_Sales)) %>%
slice_head(n = 5) # Select top 5 managers
# Create the Plotly bar chart
fig <- plot_ly(
top_managers,
x = ~reorder(Manager, Total_Sales), # Reorder managers by sales
y = ~Total_Sales,
type = "bar",
text = ~paste0("$", format(Total_Sales, big.mark = " ")), # Add formatted sales as text
textposition = "auto",
marker = list(color = "rgb(55, 83, 109)")
)
# Customize layout
fig <- fig %>% layout(
title = "Top 5 Managers by Sales",
xaxis = list(title = "Manager", tickangle = -45), # Rotate manager names
yaxis = list(title = "Total Sales"),
showlegend = FALSE
)
return(fig)
}
# Example usage
# plot_top_managers(tech_sales_data)
# Define the function
plot_sales_by_category_store <- function(data) {
# Aggregate sales by Category and Store
sales_data <- data %>%
group_by(Category, Store) %>%
summarise(Total_Sales = sum(Sales, na.rm = TRUE), .groups = "drop")
# Create the ggplot object
gg <- ggplot(sales_data, aes(x = Total_Sales, y = reorder(Category, Total_Sales), fill = Store)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.8)) +
scale_x_continuous(labels = scales::label_number(scale_cut = scales::cut_short_scale(), big.mark = " ")) +
scale_fill_manual(values = c("blue", "orange")) + # Customize store colors
labs(
title = "Sales by Category and Store",
x = "$",
y = "Category",
fill = "Store"
) +
theme_minimal() +
theme(
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(color = "gray80"),
plot.title = element_text(face = "bold", size = 16, hjust = 0.5, color = "white"),
plot.background = element_rect(fill = "#333333"),
panel.background = element_rect(fill = "#333333"),
axis.title = element_text(color = "white"),
axis.text = element_text(color = "white"),
legend.text = element_text(color = "white"),
legend.title = element_text(color = "white"),
legend.position = "bottom"
)
# Convert ggplot object to Plotly
ggplotly(gg) %>%
layout(
title = list(
text = "SALES BY CATEGORY AND STORE",
font = list(size = 16, color = "white"),
x = 0.5
),
plot_bgcolor = "#333333",
paper_bgcolor = "#333333",
xaxis = list(title = "Sales ($)", tickformat = ","),
yaxis = list(title = "")
)
}
# Example usage
# plot_sales_by_category_store(tech_sales_data)