data <- read.csv("data.split.csv")
install.packages("plotly")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
install.packages("dplyr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
install.packages("tidyr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
library(plotly)
## Loading required package: ggplot2
## 
## 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(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
# Convert the date columns
data$conventional.date <- as.Date(data$conventional.date, format="%Y/%m/%d")
data$organic.date <- as.Date(data$organic.date, format="%Y/%m/%d")
sales_data <- data %>%
  gather(key = "product_type", value = "total_sales", conventional.total_sales, organic.total_sales) %>%
  group_by(product_type) %>%
  summarise(total_sales = sum(total_sales))
plot_ly(sales_data, x = ~product_type, y = ~total_sales, type = 'bar', name = "Total Sales") %>%
  layout(title = "Total Sales by Product Type",
         yaxis = list(title = "Total Sales"),
         xaxis = list(title = "Product Type"))
price_data <- data %>%
  gather(key = "product_type", value = "price", conventional.average_price, organic.average_price) %>%
  mutate(date = as.Date(ifelse(product_type == "conventional", as.character(conventional.date), as.character(organic.date)))) %>%
  group_by(date, product_type) %>%
  summarise(average_price = mean(price))
## `summarise()` has grouped output by 'date'. You can override using the
## `.groups` argument.
plot_ly(price_data, x = ~date, y = ~average_price, color = ~product_type, type = 'scatter', mode = 'lines') %>%
  layout(title = "Price Trends Over Time",
         yaxis = list(title = "Average Price"),
         xaxis = list(title = "Date"))
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
subplot(
  plot_ly(sales_data, x = ~product_type, y = ~total_sales, type = 'bar', name = "Total Sales") %>%
    layout(title = "Total Sales by Product Type"),
  
  plot_ly(price_data, x = ~date, y = ~average_price, color = ~product_type, type = 'scatter', mode = 'lines') %>%
    layout(title = "Price Trends Over Time"),
  
  nrows = 1
)
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels