---
title: "My spending story"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
source_code: embed
---
```{r setup, include=FALSE}
```
Data collection
===================================
Row {.sidebar}
-----------------------------------------------------------------------
First of all, I collect my monthly bank statements for the past two years from 05/01/2016 to 06/01/2018. Then I also import the data from my stock account for the same period and merge those data into my bank statement spreadsheet.
The purpose of this visualization is to analyze my spending habit, and hope that can help me improve the budget planning in the future. In addition, I want to see if there is any correlation between my spending habit versus stock performance.
Row
-----------------------------------------------------------------------
### import my spending data
```{r}
spend1<- read.csv("~/spending.csv", header = T)
head(spend1)
```
Row
--------------------------------------------------------------------
### summary of my spending data
```{r}
summary(spend1)
```
Monthly spend
===================================
Row {.sidebar}
-----------------------------------------------------------------------
**Do I spend my money well?**
I want a general idea of how does my spending and stock performance look like in the past 2 years, so I use the line chart to do this analysis because it is easier to see the trend.
It is surprised me that for most months, my spending is around $2000 more or less. I spent more on Jun.17, Oct.17 and Feb.18 because of the tuition.
In general, stock performance looks better, a positive trend with a loss in April, 2018.
Row
-----------------------------------------------------------------------
```{r}
spend1$Date <- as.Date(spend1$Date, '%m/%d/%Y')
plot(Spending ~ Date, spend1, xaxt = "n", type = "l", xlab= "Date", ylab = "Monthly spending")
axis(1, spend1$Date, format(spend1$Date, "%b %d"), cex.axis = .7)
plot(Balance ~ Date, spend1, xaxt = "n", type = "l",xlab= "Date", ylab = "Stock account balance")
axis(1, spend1$Date, format(spend1$Date, "%b %d"), cex.axis = .7)
```
Category analysis
===================================
Row {.sidebar}
-----------------------------------------------------------------------
**Any trend in each spending category that I can improve?**
In this analysis I use scatter plot to visualize the spending by each category.
Spending on house looks consistent, gas spending increases every month. Other spendings seem random every month. I do realize that by the end of each year, I spent more money than before on online shopping and supermarket, that is probably because the ThanksGiving season, and I ate more!
Row
-----------------------------------------------------------------------
```{r}
plot(spend1$Date, spend1$Gas, main = "Trend of money spent on gas", xlab = "Year", ylab = "spending")
plot(spend1$Date, spend1$House, main = "Trend of money spent on house", xlab = "Year", ylab = "spending")
plot(spend1$Date, spend1$Online, main = "Trend of money spent on online shopping", xlab = "Year", ylab = "spending")
plot(spend1$Date, spend1$Supermarket, main = "Trend of money spent on supermarket", xlab = "Year", ylab = "spending")
plot(spend1$Date, spend1$Restaurant, main = "Trend of money spent on restaurant", xlab = "Year", ylab = "spending")
plot(spend1$Date, spend1$Entertainment, main = "Trend of money spent on entertainment", xlab = "Year", ylab = "spending")
```
Category percentage
===================================
Row {.sidebar}
-----------------------------------------------------------------------
**What is the percentage of each spending category?**
Even if I know I can do better on controlling the end of year shopping, it is worthwhile looking at percentage by each category.
I spent most of the money on "other", which is largely tuition. House expense accounts for 23% and is stable. I believe what I can actually control is the supermarket spend, it is double the money I spent on restaurant.
Row
-----------------------------------------------------------------------
```{r}
S_GAS = mean(spend1$Gas)
S_HOUSE = mean(spend1$House)
S_ONLINE = mean(spend1$Online)
S_SUPERMARKET = mean(spend1$Supermarket)
S_RESTAURANT = mean(spend1$Restaurant)
S_ENTERTAINMENT = mean(spend1$Entertainment)
S_OTHER = mean(spend1$Other)
piespend<-c(S_GAS, S_HOUSE,S_ONLINE,S_SUPERMARKET,S_RESTAURANT,S_ENTERTAINMENT,S_OTHER)
lbls<- c("Gas","House","Online","Supermarket","Restaurant","Entertainment","Other")
pct<-round(piespend/sum(piespend)*100)
lbls<-paste(lbls,pct)
lbls<-paste(lbls,"%",sep = " ")
pie(piespend,labels = lbls, col = rainbow(length(lbls)), main = "Spending by category")
```
Correlation
===================================
Row {.sidebar}
-----------------------------------------------------------------------
**Does each category affect each other?**
As spending is the main variable I am interested in, I start with looking at correlation between other variable with spending.
The correlation matrix on the right shows that others are most correlated to spending, followed by supermarket, gas and house. It is interesting to find that supermarket spending and restaurant are negative correlated.
Row
-----------------------------------------------------------------------
```{r}
allCor <- cor(spend1[,c(2:9)], use="pairwise.complete.obs")
library('corrplot')
corrplot(allCor, type="upper")
```
Stock & spend
===================================
Row {.sidebar}
-----------------------------------------------------------------------
**Relationship between the "extra earning" and spending?**
I thought there may be some correlation between my extra spending power and monthly spending. But it turns out that they are not highly correlated. I spend most of my money based on my needs, and it doesn't matter if I earn more in the stock market or not...
Row
-----------------------------------------------------------------------
```{r}
plot(spend1$Balance, spend1$Spending,xlab = "Stock account balance", ylab = "Monthly spending")
```