---
title: "Adidas Sales 2021 USA"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
mathjax: NULL
social: menu
theme:
version: 4
bootswatch: minty
source_code: embed
css: ./includes/footer.css
includes:
after_body: ./includes/footer.html
runtime: flex
---
```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
library(ggplot2)
library(ggpubr)
library(scales)
library(glue)
library(plotly)
library(lubridate)
library(readxl)
library(rgdal)
library(leaflet)
library(geojsonio)
library(htmltools)
library(htmlwidgets)
# Settingan Agar tidak muncul numeric value
options(scipen = 9999)
# read data
data <- read_excel('./adidas_analytic_US_Dashboard/assets/Adidas US Sales Datasets.xlsx', skip = 2)
states <- geojsonio::geojson_read("https://rstudio.github.io/leaflet/json/us-states.geojson", what = "sp")
data_map <- merge(x = data, y= states, by.x = 'State', by.y = 'name')
```
```{r}
data$Retailer <- factor(data$Retailer)
data$`Sales Method` <- factor(data$`Sales Method`)
data$Region <- factor(data$Region)
data$State <- factor(data$State)
data$City <- factor(data$City)
data$Product <- factor(data$Product)
data$year_invoice <- year(data$`Invoice Date`)
data$month_invoice <- month(data$`Invoice Date`)
data$day_invoice <- day(data$`Invoice Date`)
data_filter <- data %>% select(Region, State, City)
total_sales <- sum(data$`Total Sales`)
```
Column {data-width=300}
-----------------------------------------------------------------------
###
```{r}
html = glue("<span style='font-size: 16px'>Total Transaction in 2021 </span><br/><span style='font-size: 14px;'>(+ {round(((nrow(data[data$year_invoice == 2021, ])/nrow(data[data$year_invoice == 2020, ]) * 100) - 100), 2)} %)</span> <span style='font-size: 14px'>Since last year</span>")
valueBox(comma(nrow(data[data$year_invoice == 2021, ]), big.mark = "."),
caption = HTML(html),
icon="fa-file-invoice-dollar")
```
###
```{r}
html = glue("<span style='font-size: 16px'>Total Sales in 2021 </span><br/><span style='font-size: 14px;'>(+ {round(((sum(data[data$year_invoice == 2021,]$`Total Sales`)/sum(data[data$year_invoice == 2020,]$`Total Sales`) * 100) - 100), 2)} %)</span> <span style='font-size: 14px'>Since last year</span>")
valueBox(comma(sum(data[data$year_invoice == 2021,]$`Total Sales`), big.mark = ".", prefix = "USD "),
caption = HTML(html),
icon="fa-money-bill")
```
###
```{r}
html = glue("<span style='font-size: 16px'>Total Unit Sold in 2021 </span><br/><span style='font-size: 14px;'>(+ {round(((sum(data[data$year_invoice == 2021, ]$`Units Sold`)/sum(data[data$year_invoice == 2020, ]$`Units Sold`) * 100) - 100), 2)} %)</span> <span style='font-size: 14px'>Since last year</span>")
valueBox(glue("{sum(data[data$year_invoice == 2021, ]$`Units Sold`)} Item"),
caption = HTML(html),
icon="fa-area-chart")
```
Column {data-width=350}
-----------------------------------------------------------------------
###
```{r}
total_sales_2021 <- sum(data[data$year_invoice == 2021,]$`Total Sales`)
market_share <- data %>%
filter(year_invoice == 2021) %>%
group_by(Retailer) %>%
summarise(percent = round(sum(`Total Sales`)/total_sales_2021 * 100)) %>%
mutate(label = glue("{Retailer} ({percent}%)"))
pie(market_share$percent ,
labels = market_share$label,
col = c("#78C2AD", "#519A86","#297462", "#00503F", "#002E20", "#171e1c"), main = "Market Share Retailer By Total Sales in 2021")
```
###
```{r}
data_2021 <- data %>% filter(year_invoice == 2021) %>%
mutate(label = glue("Sales : {comma(`Total Sales`, big.mark='.', suffix = ' USD')}
Unit Sold : {comma(`Units Sold`, big.mark='.', suffix = ' Item')}
Sales Method : {`Sales Method`}
Region : {Region}
State : {State}
City : {City}
Retailer : {Retailer}
"))
scatter_plot <- ggplot(data_2021, aes(x=`Total Sales`, y=`Units Sold`)) +
geom_point(mapping = aes(color = Product, text = label)) +
geom_smooth(method=lm, color="red", fill="#69b3a2", se=TRUE) +
scale_x_continuous(labels = label_number(big.mark = ".", suffix = " USD")) +
scale_y_continuous(labels = label_number(big.mark = ".", suffix = " Item"))
ggplotly(scatter_plot, tooltip = "text")
```
Column {data-width=350 .tabset .tabset-fade}
-----------------------------------------------------------------------
### Total Sales By Product & Sales Method in 2021
```{r}
data_sales_product <- data %>% group_by(Product, `Sales Method`) %>%
filter(year_invoice == 2021) %>%
summarise(total_product_sales = sum(`Total Sales`)) %>%
arrange(-total_product_sales) %>%
mutate(urutan = rank(x = total_product_sales, ties.method = "first"))
data_sales_product <- data_sales_product %>%
mutate(label = glue(
"Category Product: {Product}
Total Sales Per Product: {comma(total_product_sales)} USD
Sales Method: {`Sales Method`}"
))
product_ranking_sales_plot <- ggplot(data = data_sales_product, aes(x = total_product_sales,
y = reorder(Product, total_product_sales), # reorder(A, berdasarkan B)
text = label)) + # menambahkan tooltip dari glue
geom_col(aes(fill = `Sales Method`, group=urutan, ), position = 'dodge') +
scale_fill_manual(values =c("#78C2AD", "#519A86","#297462")) +
scale_x_continuous(labels = label_number(big.mark = ".", suffix = " USD")) +
labs(title = NULL,
x = "Total Sales",
y = NULL) +
theme_minimal()
ggplotly(product_ranking_sales_plot, tooltip = "text") %>%
style(hoverlabel = list(bgColor = "white", align="left")) %>%
layout(margin = 'm', title= list(text = paste0("Top Total Sales", "<br>", "<sup>", "By Product & Sales method in 2021 ", "</sup>"), y=0.9))
```
### Total Unit Sold By Product & Sales Method in 2021
```{r}
data_sold_product <- data %>% group_by(Product, `Sales Method`) %>%
filter(year_invoice == 2021) %>%
summarise(total_product_sold = sum(`Units Sold`)) %>%
arrange(-total_product_sold) %>%
mutate(urutan = rank(x = total_product_sold, ties.method = "first"))
data_sold_product <- data_sold_product %>%
mutate(label = glue(
"Category Product: {Product}
Total Sales Per Product: {comma(total_product_sold)} Unit
Sales Method: {`Sales Method`}
"
))
product_ranking_sold_plot <- ggplot(data = data_sold_product, aes(x = total_product_sold,
y = reorder(Product, total_product_sold), # reorder(A, berdasarkan B)
text = label)) + # menambahkan tooltip dari glue
geom_col(aes(fill = `Sales Method`, group = urutan), position = "dodge") +
scale_fill_manual(values =c("#78C2AD", "#519A86","#297462")) +
scale_x_continuous(labels = label_number(big.mark = ".", suffix = " Unit")) +
labs(title = NULL,
x = "Total Unit",
y = NULL) +
theme_minimal()
ggplotly(product_ranking_sold_plot, tooltip = "text") %>%
style(hoverlabel = list(bgColor = "white", align="left")) %>%
layout(margin = 'm', title= list(text = paste0("Top Unit Sold", "<br>", "<sup>", "By Product & Sales method in 2021 ", "</sup>"), y=0.9))
```
Row {data-width=650}
-----------------------------------------------------------------------
###
```{r}
data_line_year <- data %>%
filter(year_invoice == 2021) %>%
group_by(month_invoice, Product) %>%
summarise(total_sales_line = sum(`Total Sales`), total_sold_unit = sum(`Units Sold`)) %>%
ungroup() %>%
arrange(-total_sales_line) %>%
mutate(label3 = glue("Month Invoice: {month_invoice}
Total Unit Sold: {comma(total_sold_unit)}
Total Sales : {comma(total_sales_line)}
"))
# Pembuatan plot statis 3
plot3 <- ggplot(data = data_line_year,
mapping = aes(x = month_invoice,
y = total_sales_line, color = Product)) +
geom_line() +
geom_point(aes(text = label3)) +
scale_y_continuous(labels = label_number(big.mark = ".", suffix = " USD"), breaks = seq(1000000, 20000000, 1000000)) +
scale_x_continuous(breaks = seq(1, 12, 1), labels = c("January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"Desember"
))+
theme_minimal() +labs(title = NULL,
x = "Month",
y = NULL)
# Pembuatan plot interaktif 2
ggplotly(plot3, tooltip = "text") %>%
style(hoverlabel = list(bgColor = "white", align="left")) %>%
layout(margin = 'm', title= list(text = paste0("Sales Activity in 2021", "<br>", "<sup>", "based on total sales / month", "</sup>"), y=0.9))
```
Row {data-width=650}
-----------------------------------------------------------------------
### Total Sales Map by Region in 2021
```{r}
data_map1 <- data_map %>% group_by(State) %>% summarise(total_sales_state = sum(`Total Sales`))
data_baru <- merge(y = data_map1, x= states, by.y = 'State', by.x = 'name')
m <- leaflet(data_baru) %>%
setView(-96, 37.8, 4) %>%
addProviderTiles("MapBox", options = providerTileOptions(
id = "mapbox.light",
accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN')))
bins <- c(5000000,10000000,15000000,20000000,25000000,Inf)
pal <- colorBin(c("#78C2AD", "#519A86","#297462", "#00503F", "#002E20"), domain = data_baru$total_sales_state, bins = bins)
labels <- sprintf(
"<strong>%s</strong><br/>%s</sup>",
data_baru$name, glue("{comma(data_baru$total_sales_state)} USD")
) %>% lapply(htmltools::HTML)
m %>% addPolygons(
fillColor = ~pal(data_baru$total_sales_state),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.5,
highlightOptions = highlightOptions(
weight = 2,
color = "#666",
dashArray = "",
fillOpacity = 0.1,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>% addLegend(pal = pal, values = ~total_sales_state, opacity = 0.7, title = NULL,
position = "bottomright")
```