library(tidyverse)
library(data.table)
library(superheat)
library(viridis)
library(patchwork)
library(rnaturalearth)
library(rnaturalearthdata)
theme_set(theme_minimal())
CCI = read.csv("2021_state_CCI.csv",header = TRUE,row.names="date")
t_CCI = transpose(CCI)
colnames(t_CCI) <- rownames(CCI)
rownames(t_CCI) <- colnames(CCI)
rownames(t_CCI) = rownames(t_CCI) %>% 
  str_replace("\\."," ")%>% 
  str_replace("\\."," ")
day_avg = colSums(t_CCI)/nrow(t_CCI)
superheat(t_CCI, 
          left.label.text.size = 1.5,
          pretty.order.rows = TRUE,
          yt = day_avg,
          yt.plot.type = "line",
          yt.axis.name = "CCI average",
          row.dendrogram = TRUE,
          heat.pal = viridis_pal(direction = -1)(50),
          title = "heatmap of daily CCI of all states in USA")

set.seed(16)
p = list()

fit = t_CCI %>% 
  kmeans(centers = 4, nstart = 20)

centroids = data.frame(fit$centers) %>% 
  rownames_to_column("cluster") %>% 
  pivot_longer(-cluster, names_to = "date",values_to = "value")

p[["facet"]]=centroids %>% 
  mutate(date = str_remove(date, "X"),
         date = as.Date(date,"%Y.%m.%d")) %>% 
  ggplot() +
  geom_point(aes(date,value),size = 0.6) +
  facet_wrap(.~cluster,labeller = "label_both")+
  scale_x_date(date_breaks = "3 months",date_labels = "%m-%d")+
  ggtitle("Centroids of Close Contact Index 2021 Time Series Clusters")+
  theme(plot.title = element_text(hjust = 0.5))+
  labs(x="Date",y = "Close Contact Index Value")
us = ne_states(country = 'United States of America',returnclass = "sf") %>% 
  select(name,geometry)
mem = data.frame(fit$cluster)
mem = mem %>% 
  rownames_to_column("name")
p[["map"]]=us %>% 
  left_join(mem,by = "name") %>% 
  filter(!name %in% c("Alaska","Hawaii")) %>% 
  ggplot()+
  geom_sf(aes(fill = factor(fit.cluster)))+
  scale_fill_manual(values=c("#6baed6","#2171b5","#eff3ff","#bdd7e7"))+
  labs(fill="cluster")+
  ggtitle("Map of clusters of states")+
  theme(plot.title = element_text(hjust = 0.5,vjust=0.1))


p[["facet"]]/p[["map"]]+plot_layout(width=c(0.5,1))