Public policy and vaccination

library(httr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(RColorBrewer)
library(tidyr)
library(dplyr)
library(rvest)
## Loading required package: xml2
library("ggplot2");
disease_names=c("Diptheria",
                "Hib","Measles","Mumps",'Pneumococcal',
                "Whooping cough",
                "Rotavirus","Rubella","Tetanus",
                "Chicken pox",
                "Polio",
               "Hepatitis")
disease_num=c('009',
  "012", "021","043","065",
  "024",
              "077","029",
              "033","073",
              "026",
  "039")
names(disease_num)=disease_names
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
df<-melt(disease_num)
df$disease<-rownames(df)
url <- "http://www9.health.gov.au/cda/source/rpt_3_sel.cfm"
cocorahs <- html_session(url)
form.unfilled <- cocorahs %>% html_node("form") %>% html_form()

getstuff <- function(code){
 

  print(code)
# Specify URL
 
# Grab Initial Form
#  Form is filled in stages. Here, only do country and date
  form.filled <- form.unfilled %>%
    set_values(
                   CAUSE=code,REPORT_OPTION=1)

# submit the form and save as a new session
  session <- submit_form(cocorahs, form.filled) 

# look for a table in the nodes
  table <- session %>% html_nodes("table")

  print("in here")
# The table you want
  df<-table[[1]] %>% html_table()
  colnames(df)
  colnames(df)<-c("Year","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","AnnualTotal")

  dd=(spread(df[,c("Year",'AnnualTotal')],key = as.numeric(Year), value = AnnualTotal))
  print(head(dd))
  return(dd)

}



all_df=lapply(df$value,getstuff)%>% bind_rows()
## [1] 009
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
## 1    1    4    0    0    0    0    0    0    0    0    1    0    0    0
##   2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
## 1    0    0    0    0    0    0    4    0    3    2    2    8    8    7
## [1] 012
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
## 1  589  490  339  153   71   47   45   35   41   27   20   29   21   18
##   2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
## 1   17   21   17   25   19   23   13   16   20   21   16   17   16   11
## [1] 021
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
## 1 1479 1438 4680 4794 1182  478  831  285  240  108  131   31   92   44
##   2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
## 1   10  125   12   65  104   70  194  199  158  339   74   99   81   79
## [1] 043
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
## 1   57   64   78   90  157  132  188  174  183  209  113   70   77  102
##   2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
## 1  240  274  582  286  166   97  153  201  216  186  645  804  811  544
## [1] 065
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1993 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
## 1    1    1 1754 2415 2221 2356 1676 1445 1470 1628 1554 1639 1883 1824
##   2013 2014 2015 2016 2017 2018
## 1 1552 1564 1498 1664 2049 1473
## [1] 024
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1991 1992 1993 1994 1995 1996  1997 1998 1999 2000 2001 2002 2003 2004
## 1  342  803 4516 5469 4199 4561 10799 5675 4359 5991 9513 5569 5098 8742
##    2005 2006 2007  2008  2009  2010  2011  2012  2013  2014  2015  2016
## 1 11151 9759 4857 14294 30186 34836 38755 24093 12368 11863 22540 20099
##    2017 2018
## 1 12194 7124
## [1] 077
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   2010 2011 2012 2013 2014 2015 2016 2017 2018
## 1 3919 3331 3798 3208 3022 4134 2731 7131 2048
## [1] 029
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
## 1  104  610 4034 3485 5541 2951 1399  730  371  321  263  252   54   30
##   2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
## 1   29   59   35   35   27   44   58   35   25   16   17   15   10    7
## [1] 033
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
## 1   11   13   10   13    7    3    7    8    2    8    2    4    4    6
##   2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
## 1    2    3    3    4    3    2    3    7    4    3    2    7    4    2
## [1] 073
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1993 1994 1995 1996 1997 2003 2006 2007 2008 2009 2010 2011 2012 2013
## 1    5   27    1   11    5    1 1622 1664 1807 1796 1793 2100 1989 2134
##   2014 2015 2016 2017 2018
## 1 2111 2488 3025 3170 2708
## [1] 026
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
## 1    0    0    0    0    0    0    0    0    0    0    0    0    0    0
##   2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
## 1    0    0    1    0    0    0    0    0    0    0    0    0    0    0
## [1] 039
## Levels: 009 012 021 024 026 029 033 039 043 065 073 077
## Submitting with 'CAUSE'
## [1] "in here"
##   1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
## 1    2  242  264  260  214  263  242  285  377  389  403  338  277  245
##   2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
## 1  289  281  253  238  209  180  175  164  162  136  154  132   76
mat<-as.matrix(all_df)
matnorm<-mat/rowSums(mat,na.rm=T)
datnorm<-as.data.frame(matnorm)

head(all_df)
##   1991 1992 1993 1994 1995 1996  1997 1998 1999 2000 2001 2002 2003 2004
## 1    1    4    0    0    0    0     0    0    0    0    1    0    0    0
## 2  589  490  339  153   71   47    45   35   41   27   20   29   21   18
## 3 1479 1438 4680 4794 1182  478   831  285  240  108  131   31   92   44
## 4   57   64   78   90  157  132   188  174  183  209  113   70   77  102
## 5   NA   NA    1   NA   NA   NA    NA   NA   NA    1 1754 2415 2221 2356
## 6  342  803 4516 5469 4199 4561 10799 5675 4359 5991 9513 5569 5098 8742
##    2005 2006 2007  2008  2009  2010  2011  2012  2013  2014  2015  2016
## 1     0    0    0     0     0     0     4     0     3     2     2     8
## 2    17   21   17    25    19    23    13    16    20    21    16    17
## 3    10  125   12    65   104    70   194   199   158   339    74    99
## 4   240  274  582   286   166    97   153   201   216   186   645   804
## 5  1676 1445 1470  1628  1554  1639  1883  1824  1552  1564  1498  1664
## 6 11151 9759 4857 14294 30186 34836 38755 24093 12368 11863 22540 20099
##    2017 2018
## 1     8    7
## 2    16   11
## 3    81   79
## 4   811  544
## 5  2049 1473
## 6 12194 7124
all_reqd=cbind(df,datnorm)
mat=data.matrix(all_reqd[,-c(1,2)])
colnames(all_reqd)
##  [1] "value"   "disease" "1991"    "1992"    "1993"    "1994"    "1995"   
##  [8] "1996"    "1997"    "1998"    "1999"    "2000"    "2001"    "2002"   
## [15] "2003"    "2004"    "2005"    "2006"    "2007"    "2008"    "2009"   
## [22] "2010"    "2011"    "2012"    "2013"    "2014"    "2015"    "2016"   
## [29] "2017"    "2018"
f<-melt(all_reqd[,-c(1)])
## Using disease as id variables
head(f)
##          disease variable       value
## 1      Diptheria     1991 0.025000000
## 2            Hib     1991 0.270555811
## 3        Measles     1991 0.084892664
## 4          Mumps     1991 0.008262067
## 5   Pneumococcal     1991          NA
## 6 Whooping cough     1991 0.001037134
cols <- c(colorRampPalette(c("#e7f0fa", "#c9e2f6", "#95cbee", "#0099dc", "#4ab04a", "#ffd73e"))(20),
          colorRampPalette(c("#eec73a", "#e29421", "#e29421", "#f05336","#ce472e"), bias=2)(80))
                             
a<-ggplot(f, aes((variable), disease, fill=value)) +
  geom_tile(colour="white", size=0.25) + 
  #remove extra space
  scale_y_discrete(expand=c(0,0))+
  labs(y="Disease",x="Year",title="Notifications of vaccinated diseases in Australia")+
  #remove extra space
  #custom breaks on x-axis
  scale_x_discrete(expand=c(0,0))+
  #maintains aspect ratio.
  coord_fixed()+
  #set a base size for all fonts
  theme_grey(base_size=8)+
  #theme options
  theme(
    #bold font for both axis text
    axis.text=element_text(face="bold"),
    #set thickness of axis ticks
    axis.ticks=element_line(size=0.4),
    #remove plot background
    plot.background=element_blank(),
    #remove plot border
    panel.border=element_blank())+
  scale_fill_gradientn(colours=cols, limits=c(0, 0.3),
                       breaks=seq(0.003, 0.276, by=0.01), 
                       #values=c(0,0.01, 0.02, 0.03,0.04,0.05,0.06,0.07,0.08, 0.09,0.1,0.12,0.15,0.175,0.2,0.25,0.3), 
                       na.value=rgb(192,192,192 ,max=255))+
   theme(axis.text.x = element_text(angle = 90, hjust = 1))
a

ggsave("vaccination_notification.png",a)
## Saving 12 x 15 in image