Welfare data from data.gov.au - department of social security statistics 2013-2016 - total number across all Welfare categories

### LOAD 2013 Tax Data

url2 <- "https://data.gov.au/dataset/e29ef9ca-0d1a-47ec-9e9b-14a79a941511/resource/fa6fda82-a70d-4f4c-bb00-203ffc4d20af/download/taxstats2013individual06selected-items-by-taxable-status-postcode.xlsx"
p2f <- tempfile()

GET(url2, write_disk(tf2 <- tempfile(fileext = ".xlsx")))
## Response [https://data.gov.au/dataset/e29ef9ca-0d1a-47ec-9e9b-14a79a941511/resource/fa6fda82-a70d-4f4c-bb00-203ffc4d20af/download/taxstats2013individual06selected-items-by-taxable-status-postcode.xlsx]
##   Date: 2018-09-30 12:11
##   Status: 200
##   Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
##   Size: 7.46 MB
## <ON DISK>  /var/folders/pl/4vb412rs2pd1bn6kf7h3dc840000gn/T//Rtmpjgn6Vx/file46b753961ce.xlsx
##Load Payment Data 2014
##Load Payment Data 2015
##Load Payment Data 2016

Grab only the columns that we want to choose for our predictive modelling research.

str(table1)
## 'data.frame':    10282 obs. of  31 variables:
##  $ State               : chr  "ACT" "ACT" "ACT" "ACT" ...
##  $ Postcode            : chr  "2600" "2601" "2602" "2603" ...
##  $ No.Population       : num  5755 2650 19110 6180 7165 ...
##  $ No.HealthInsurance  : num  4455 1360 12325 4725 5070 ...
##  $ No.Tax              : num  4960 1800 16355 5370 6285 ...
##  $ Tax                 : num  1.55e+08 3.79e+07 2.84e+08 1.77e+08 1.43e+08 ...
##  $ No.Salary           : num  4025 2185 15525 4600 5970 ...
##  $ Salary              : num  3.31e+08 1.25e+08 1.05e+09 3.74e+08 4.91e+08 ...
##  $ No.Allowances       : num  175 195 1075 225 285 ...
##  $ Allowances          : num  867475 1192431 5975182 1225973 1368105 ...
##  $ No.Pensions         : num  195 45 940 170 210 475 345 590 30 1100 ...
##  $ Pensions            : num  1452867 371161 7201386 1557289 1805084 ...
##  $ No.Interest         : num  4105 1775 13075 4380 4820 ...
##  $ Interest            : num  14852802 3622769 27229224 15509702 10721436 ...
##  $ NoRent              : num  1560 545 3430 1530 2000 1610 1020 1900 115 2720 ...
##  $ Rent                : num  41760840 14057264 71800358 45263237 44753380 ...
##  $ No.Superannuation   : num  225 25 235 225 100 140 100 195 10 250 ...
##  $ Superannuation      : num  4601346 516019 4049535 4903593 1606696 ...
##  $ Year                : chr  "2013" "2013" "2013" "2013" ...
##  $ TotalWelfare        : num  2320342 1563592 13176568 2783262 3173189 ...
##  $ TotalPeopleWelfare  : num  370 240 2015 395 495 ...
##  $ PropPeopleWelfare   : num  0.0643 0.0906 0.1054 0.0639 0.0691 ...
##  $ PropPeopleIncome    : num  0.699 0.825 0.812 0.744 0.833 ...
##  $ PropPeopleNoIncome  : num  0.301 0.175 0.188 0.256 0.167 ...
##  $ PropPeopleHInsurance: num  0.774 0.513 0.645 0.765 0.708 ...
##  $ AverageIncome       : num  82137 57390 67311 81200 82323 ...
##  $ AverageWelfare      : num  6271 6515 6539 7046 6410 ...
##  $ AverageInterest     : num  3618 2041 2083 3541 2224 ...
##  $ AverageTax          : num  31337 21054 17386 32977 22684 ...
##  $ PropTaxToWelfare    : num  0.0149 0.0413 0.0463 0.0157 0.0223 ...
##  $ GapTaxWelfare       : num  1.53e+08 3.63e+07 2.71e+08 1.74e+08 1.39e+08 ...
tb1 <- table1 %>%
  select(State, Postcode, Salary, Tax, No.Tax, Allowances, Pensions, Year, 
         AverageWelfare, AverageIncome, AverageTax, TotalWelfare, PropTaxToWelfare, GapTaxWelfare) 

Multiple Plots on a Grid

Lets view 10 highest Welfare Postcodes, and compare amount of Tax collected, by State.

The following code also uses function() to graph a bar chart, which will then use plot_grid() to show each Tax/Welfare by State. Similar to facet_wrap() however allows to show comparison between 2 factors instead of 1 x value.

# Plotting function
#---------------------------------------------------
plotTopN <- function(df, sFilter, xVal, yVal1, yVal2, title1, xLab, yLab) {

  xAxis <- enquo(xVal)
  #yAxis1 <- enquo(yVal1)
  #yAxis2 <- enquo(yVal2)
  
  #p <- ggplot(df, aes_string(xVal, yVal, group = grVal, colour = grVal))
  fi <- filter(df, State==sFilter) 
  ar <- arrange(fi, -Tax) 
  tn <- top_n(ar, 20, TotalWelfare)
  ga <- gather(tn, key, value, Tax, TotalWelfare)
  p <- ggplot(ga, aes(x=Postcode, y=value, fill=key, colour=key))
  
  p + geom_bar(stat = "identity", position = "dodge") + 
    theme(axis.text.x = element_text(angle=65, vjust=0.6), legend.position = "bottom") +
    scale_y_continuous(labels = comma) + 
    labs(title=title1, 
         subtitle="",
         caption="Source: data.gov.au",
         x=xLab,
         y=yLab)
}

p1 <- plotTopN (tb1, "NSW", "Postcode", "Tax", "TotalWelfare", "Tax collected v's Welfare paid", "Postcode (NSW)", "$ million")
p2 <- plotTopN (tb1, "QLD", "Postcode", "Tax", "TotalWelfare", "Tax collected v's Welfare paid", "Postcode (QLD)", "$ million")
p3 <- plotTopN (tb1, "VIC", "Postcode", "Tax", "TotalWelfare", "Tax collected v's Welfare paid", "Postcode (VIC)", "$ million")
p4 <- plotTopN (tb1, "ACT", "Postcode", "Tax", "TotalWelfare", "Tax collected v's Welfare paid", "Postcode (ACT)", "$ million")
p5 <- plotTopN (tb1, "WA", "Postcode", "Tax", "TotalWelfare", "Tax collected v's Welfare paid", "Postcode (WA)", "$ million")
p6 <- plotTopN (tb1, "WA", "Postcode", "Tax", "TotalWelfare", "Tax collected v's Welfare paid", "Postcode (WA)", "$ million")
p7 <- plotTopN (tb1, "TAS", "Postcode", "Tax", "TotalWelfare", "Tax collected v's Welfare paid", "Postcode (NT)", "$ million")
p8 <- plotTopN (tb1, "NT", "Postcode", "Tax", "TotalWelfare", "Tax collected v's Welfare paid", "Postcode", "$ million")

theme_set(theme_cowplot(font_size=8)) # reduce default font size
plot_grid(p1, p2, p3, p4, p5, p6, p7, p8 ) #, labels = c('A', 'B')

More Plots

Lets view Trends for Income, Tax and Welfare payments.

# Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing # of the R code that generated the plot.

tbSum <- tb1 %>% group_by(Year, State) %>% 
  summarise(sum(Salary), sum(Tax), sum(TotalWelfare)) 

# Plotting Function
#-------------------------------------------------------------------
plotTrend <- function(df, xVal, yVal, grVal, yr, title1, xLab, yLab) {
  
  xAxis <- enquo(xVal)
  yAxis <- enquo(yVal)
  grAxis <- enquo(grVal)
  
  p <- ggplot(df, aes_string(xVal, yVal, group = grVal, colour = grVal))
  
  p + geom_line(alpha = 1) +
    geom_text(data = subset(df, Year == yr), aes_string(label = grVal, colour = grVal, x = xVal, y = yVal), hjust = -.1) +
    scale_colour_discrete(guide = 'none')  +    
    theme(plot.margin = unit(c(1,3,1,1), "lines")) +
    labs(title =title1, x = xLab, y = yLab) +
    theme(legend.position = "bottom") +
    scale_y_continuous(labels = comma)
}

p1 <- plotTrend (tbSum, "Year", "`sum(Salary)`", "State", "2016", "Yearly trend - Salary", "Year", "$ Salary")
p2 <- plotTrend (tbSum, "Year", "`sum(Tax)`", "State", "2016", "Yearly trend - Tax", "Year", "$ Tax")
p3 <- plotTrend (tbSum, "Year", "`sum(TotalWelfare)`", "State", "2016", "Yearly trend - Welfare", "Year", "$ Welfare")

theme_set(theme_cowplot(font_size=8)) # reduce default font size
plot_grid(p1, p2, p3) #, labels = c('A', 'B')

# https://cran.r-project.org/web/packages/cowplot/vignettes/plot_grid.html
# samples
# plot_grid(plot.mpg, plot.diamonds, labels = "AUTO", align = 'h')
# plot_grid(plot.mpg, plot.diamonds, labels = "AUTO", ncol = 1, align = 'v')

options(warn = oldw)

The end!