
# read in the necessary packages
library("ggplot2")
library("treemapify")
library("dplyr")
library("viridis")


# now read in the data set
m = read.csv("MovingCosts.csv")

# group the same items together (e.g. multiple instances of "tolls") and summarize by sum of costs
i = m %>% group_by(Item, Cause, WhoGotPaid) %>% summarize(Amount = sum(Amount))

i$Item = as.factor(i$Item)

# generate colors for the first treemap
c3 = c(
  "#A70367","#C24E95","#D381B3",
  "#F55480","#F886A4",
  "#FB6B42","#FC987A","#FDB5A0",
  "#F6D86B","#F8E498","#FAEDB5",
  "#A1DBB2","#BDE6C9","#D1EDD9",
  "#349194","#71B2B4","#99C8CA",
  "#2a59b5","#5572AB","#859AC3") 

# now create a color scale linking the colors to the values of the "Items" variable
names(c3) = levels(i$Item)
colScale3 <- scale_fill_manual(name = "Item",values = c3)

# The first graph with costs by item
ggplot(i, aes(area = Amount, fill = Item, label = Item)) +
  geom_treemap() +
  geom_treemap_text() +
  colScale3 +
  ggtitle("Moving Costs") +
  theme(plot.title = element_text(hjust = 0.5, size = 18)) 

# Generate a color scheme for cost by cause
h11 = c("#F9993C","#F77D2E","#FBCCAD",
        "#DD5C50","#b95461","#335C7C",
        "#7A86A6","#b3c2e8","#E1E7F6")


# graph cause of costs - want label to show item and cause together with new line in between
ggplot(i, aes(area = Amount, fill = Cause, label = paste(Item,Cause, sep = ":\n"))) +
  geom_treemap() +
  geom_treemap_text() +
  scale_fill_manual(values = h11) +
  ggtitle("Why did I have to pay?") +
  theme(plot.title = element_text(hjust = 0.5, size = 18)) 

# group by cause to have 1 tile per cause, sum cost
j = i %>% group_by(Cause) %>% summarize(Amount = sum(Amount))

# Now graph by cause with one tile per cause showing the total amount for that cause
ggplot(j, aes(area = Amount, fill = Cause, label = Cause)) +
  geom_treemap() +
  geom_treemap_text() +
  scale_fill_manual(values = h11) +
  ggtitle("Why did I have to pay?") +
  theme(plot.title = element_text(hjust = 0.5, size = 18)) 

# Now graph who got paid
i = m %>% group_by(Item, Cause, WhoGotPaid, Returned) %>% summarize(Amount = sum(Amount))

# want the label to show which were deposits
i$WhoGotPaid[i$Returned == "Maybe"] = paste(i$WhoGotPaid[i$Returned == "Maybe"]," (Returned?)",sep = "")

# final color scheme for the last few graphs
h12 = c("#F1CEB4","#d6d0cb",
        "#FDBA58","#d1c5b4",
        "#FFD59D",
        "#FC7E5F","#BC5555","#E3BCBB",
        "#B1CAD6","#63b8d4","#15637A")

# Graph who got paid
ggplot(i, aes(area = Amount, fill = WhoGotPaid,
              label = paste(Item,WhoGotPaid, sep = ":\n"))) +
  geom_treemap() +
  geom_treemap_text() +
  scale_fill_manual(values = h12,
                    name = "Who got paid?") +
  ggtitle("Who got my money?") +
  theme(plot.title = element_text(hjust = 0.5, size = 18))

# now group by who got paid to show one tile per who got paid
k = i %>% group_by(WhoGotPaid, Returned) %>% summarize(Amount = sum(Amount))

# Now the final chart
ggplot(k, aes(area = Amount, fill = WhoGotPaid, label = WhoGotPaid)) +
  geom_treemap() +
  geom_treemap_text() +
  scale_fill_manual(values = h12,
                    name = "Who got paid?") +
  ggtitle("Who got my money?") +
  theme(plot.title = element_text(hjust = 0.5, size = 18)) 
