library(data.table)
library(ggplot2)
library(dplyr)
library(gridExtra)
library(arrow)
library(ggiraph) # Add ggiraph for interactive plots
open_dataset("~/foodprices/retailprices/parquetfilesnew/")->retail
as.data.table(select(retail,c(states,date,commodity,price,commodity_group)))->retail
open_dataset("~/foodprices/wholesaleprices/parquetfilesnew/")->wholesale
as.data.table(select(wholesale,c(states,date,commodity,price,commodity_group)))->wholesale
wholesale$Type<-"Wholesale"
wholesale$price/100->wholesale$price
retail$Type<-"Retail"
rbind(retail,wholesale)->combinedprice
# Clean data
ifelse(is.na(combinedprice$price),0,combinedprice$price)->combinedprice$price
gsub(" @","",combinedprice$commodity,fixed=TRUE)->combinedprice$commodity
gsub("- ","",combinedprice$commodity,fixed=TRUE)->combinedprice$commodity
gsub("Packed","",combinedprice$commodity,fixed=TRUE)->combinedprice$commodity
gsub("()","",combinedprice$commodity,fixed=TRUE)->combinedprice$commodity
gsub("*","",combinedprice$commodity,fixed=TRUE)->combinedprice$commodity
gsub("/ ","-",combinedprice$commodity,fixed=TRUE)->combinedprice$commodity
gsub("/","-",combinedprice$commodity,fixed=TRUE)->combinedprice$commodity
copy(combinedprice)->t
combinedprice[states!="Maximum Price"]->combinedprice
combinedprice[states!="Minimum Price"]->combinedprice
combinedprice[states!="Modal Price"]->combinedprice
combinedprice[states!="Average Price"]->combinedprice
combinedprice[price>0]->combinedprice
# Modified theme for interactive plots
my_theme <- theme_bw(base_size = 14) + # Slightly smaller base size for interactive
theme(
text = element_text(family = "sans-serif", color = "#333333"),
plot.title = element_text(size = 18, face = "bold", hjust = 0.5,
margin = margin(10, 0, 10, 0)),
axis.title = element_text(size = 16),
axis.text = element_text(size = 12),
legend.position = "bottom",
legend.title = element_blank(),
panel.grid.major = element_line(color = "#EEEEEE"),
panel.border = element_blank(),
axis.line = element_line(color = "#333333"),
axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=1), # Reduced angle
strip.background = element_rect(fill = "#F2F2F2", color = NA)
)
### Function to create interactive plots
create_interactive_plot <- function(data, title = "", nrow = NULL) {
if (nrow(data) == 0) return(NULL)
p <- ggplot(data, aes(x = date, y = price, colour = commodity,
tooltip = paste0(commodity, "\n",
"Date: ", format(date, "%b %Y"), "\n",
"Price: ₹", round(price, 2), "\n",
"Type: ", Type),
data_id = commodity)) +
geom_smooth_interactive(method = "gam", se = FALSE, size = 1.2,
aes(hover_css = "stroke-width:3;fill-opacity:0.8;")) +
my_theme +
facet_wrap(~commodity, nrow = nrow) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
labs(title = title, x = "Date", y = "Price (₹)") +
scale_color_viridis_d(begin = 0.2, end = 0.8) +
theme(legend.position = "none") # Remove legend since we have tooltips
# Make interactive
girafe(
ggobj = p,
options = list(
opts_tooltip(
css = "background-color:white;color:black;padding:5px;border-radius:3px;
border:1px solid #ddd;font-family:sans-serif;font-size:12px;",
use_fill = TRUE
),
opts_hover_inv(css = "opacity:0.3;"),
opts_hover(css = "stroke-width:2.5;"),
opts_selection(
type = "single",
css = "stroke:red;stroke-width:3;",
only_shiny = FALSE
),
opts_zoom(min = 1, max = 5),
opts_toolbar(position = "topright"),
opts_sizing(rescale = TRUE, width = 1)
),
width_svg = ifelse(is.null(nrow), 10, 8),
height_svg = ifelse(is.null(nrow), 6, ifelse(nrow == 1, 4, 6))
)
}