US Personal Expenditure

Following is based on the USPersonalExpenditure dataset in the datasets package:

library(plotly)
library(datasets)
library(reshape2)

data(USPersonalExpenditure)
USPersonalExpenditure
##                       1940   1945  1950 1955  1960
## Food and Tobacco    22.200 44.500 59.60 73.2 86.80
## Household Operation 10.500 15.500 29.00 36.5 46.20
## Medical and Health   3.530  5.760  9.71 14.0 21.10
## Personal Care        1.040  1.980  2.45  3.4  5.40
## Private Education    0.341  0.974  1.80  2.6  3.64
# converting original matrix dataset into dataframe for plotly
prsnlexp = as.data.frame(USPersonalExpenditure)
prsnlexp$type = rownames(prsnlexp)
prsnlexp
##                       1940   1945  1950 1955  1960                type
## Food and Tobacco    22.200 44.500 59.60 73.2 86.80    Food and Tobacco
## Household Operation 10.500 15.500 29.00 36.5 46.20 Household Operation
## Medical and Health   3.530  5.760  9.71 14.0 21.10  Medical and Health
## Personal Care        1.040  1.980  2.45  3.4  5.40       Personal Care
## Private Education    0.341  0.974  1.80  2.6  3.64   Private Education
# converting dataframe into a form required by plotly
prsnlexpmelt = melt(prsnlexp, value.name = "expenditure")
## Using type as id variables
names(prsnlexpmelt)[2] = "year"
head(prsnlexpmelt, 10)
##                   type year expenditure
## 1     Food and Tobacco 1940      22.200
## 2  Household Operation 1940      10.500
## 3   Medical and Health 1940       3.530
## 4        Personal Care 1940       1.040
## 5    Private Education 1940       0.341
## 6     Food and Tobacco 1945      44.500
## 7  Household Operation 1945      15.500
## 8   Medical and Health 1945       5.760
## 9        Personal Care 1945       1.980
## 10   Private Education 1945       0.974
# creating a dataset with total expenditure as sum of sub-categories
prsnlexptotal = data.frame(total = colSums(prsnlexp[,-6]))
prsnlexptotal$year = rownames(prsnlexptotal)
prsnlexptotal
##        total year
## 1940  37.611 1940
## 1945  68.714 1945
## 1950 102.560 1950
## 1955 129.700 1955
## 1960 163.140 1960
# setting custom color palette 
pal = c("#F0E68C", "#FFA07A", "#BDB76B", "#FFDAB9", "#FFFF00")

# line graph to show total expenditure
linetotal = plot_ly(data = prsnlexptotal, x = ~year, y = ~total, type = "scatter", mode = "line", showlegend = FALSE, color = "#F0E68C", colors = pal)

# adding labels to graph
linetotal = add_text(linetotal, data = prsnlexptotal, x = ~year, y = ~total, text = ~round(total), textposition = "top left", textfont = list(color = "#000000"))

linetotal = layout(linetotal, yaxis = list(title = "$ billion"), xaxis = list(title = ""))

# stack bar chart to show categories of expenditure
stacktype = plot_ly(data = prsnlexpmelt, x = ~year, y = ~expenditure, name = ~type, type = "bar", color = ~type, colors = pal)

# adding labels, legend, and title
stacktype = layout(stacktype, barmode = "stack", yaxis = list(title = "$ billion"), xaxis = list(title = ""), legend = list(orientation = 'h'), title = "US Personal Expenditure: Total and by Category")

# combining the above two plots into one
allplot = subplot(linetotal, stacktype, nrows = 2, shareX = TRUE, shareY = TRUE)
allplot

Thank you!