library(flexdashboard)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(readr)
library(readr)
superstore <- read_csv("superstore.csv")
## Rows: 9994 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (15): Order ID, Order Date, Ship Date, Ship Mode, Customer ID, Customer ...
## dbl  (6): Row ID, Postal Code, Sales, Quantity, Discount, Profit
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
superstore <- superstore %>%
  mutate(Order.Date = mdy(`Order Date`),
         Year = year(Order.Date),
         Category = as.factor(Category),
         Segment = as.factor(Segment))
sales_by_segment <- superstore %>%
  group_by(Year, Segment) %>%
  summarise(Sales = sum(Sales, na.rm = TRUE))
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
plot_ly(sales_by_segment, x = ~Year, y = ~Sales, color = ~Segment, type = 'scatter', mode = 'lines+markers') %>%
  layout(title = "Yearly Sales by Segment",
         xaxis = list(title = "Year"),
         yaxis = list(title = "Sales"))
sales_by_category <- superstore %>%
  group_by(Year, Category) %>%
  summarise(Sales = sum(Sales, na.rm = TRUE))
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
ggplot(sales_by_category, aes(x = factor(Year), y = Sales, fill = Category)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Yearly Sales by Product Category", x = "Year", y = "Sales") +
  theme_minimal()