Using R to put public health data at your fingertips

Julian Flowers: Public Health England

27/09/2018

Outline

Fingertips 1

Fingertips 2

Fingertips behind the scenes

Fingertips API

fingertipsR

#install.packages("fingertipsR")

library(fingertipsR)
fingertips_stats()
## This may take a few moments... On 01/10/2018 Fingertips consisted of 54 profiles, made up of 4115 indicators and 1769 distinct indicators.

Data extraction

example <- fingertips_data(ProfileID = 26, AreaTypeID = c(101, 102))
head(example)
##   IndicatorID            IndicatorName ParentCode ParentName  AreaCode
## 1       90366 Life expectancy at birth       <NA>       <NA> E92000001
## 2       90366 Life expectancy at birth       <NA>       <NA> E92000001
## 3       90366 Life expectancy at birth  E92000001    England E12000001
## 4       90366 Life expectancy at birth  E92000001    England E12000002
## 5       90366 Life expectancy at birth  E92000001    England E12000003
## 6       90366 Life expectancy at birth  E92000001    England E12000004
##                          AreaName AreaType    Sex      Age CategoryType
## 1                         England  Country   Male All ages         <NA>
## 2                         England  Country Female All ages         <NA>
## 3               North East region   Region   Male All ages         <NA>
## 4               North West region   Region   Male All ages         <NA>
## 5 Yorkshire and the Humber region   Region   Male All ages         <NA>
## 6            East Midlands region   Region   Male All ages         <NA>
##   Category Timeperiod Value LowerCI95.0limit UpperCI95.0limit
## 1     <NA>  2000 - 02    NA               NA               NA
## 2     <NA>  2000 - 02    NA               NA               NA
## 3     <NA>  2000 - 02    NA               NA               NA
## 4     <NA>  2000 - 02    NA               NA               NA
## 5     <NA>  2000 - 02    NA               NA               NA
## 6     <NA>  2000 - 02    NA               NA               NA
##   LowerCI99.8limit UpperCI99.8limit Count Denominator
## 1               NA               NA    NA          NA
## 2               NA               NA    NA          NA
## 3               NA               NA    NA          NA
## 4               NA               NA    NA          NA
## 5               NA               NA    NA          NA
## 6               NA               NA    NA          NA
##                                          Valuenote RecentTrend
## 1 Aggregated from all known lower geography values        <NA>
## 2 Aggregated from all known lower geography values        <NA>
## 3 Aggregated from all known lower geography values        <NA>
## 4 Aggregated from all known lower geography values        <NA>
## 5 Aggregated from all known lower geography values        <NA>
## 6 Aggregated from all known lower geography values        <NA>
##   ComparedtoEnglandvalueorpercentiles ComparedtoRegionvalueorpercentiles
## 1                        Not compared                       Not compared
## 2                        Not compared                       Not compared
## 3                        Not compared                       Not compared
## 4                        Not compared                       Not compared
## 5                        Not compared                       Not compared
## 6                        Not compared                       Not compared
##   TimeperiodSortable Newdata Comparedtogoal
## 1           20000000    <NA>           <NA>
## 2           20000000    <NA>           <NA>
## 3           20000000    <NA>           <NA>
## 4           20000000    <NA>           <NA>
## 5           20000000    <NA>           <NA>
## 6           20000000    <NA>           <NA>

Analytical functions

performance <- fingertips_redred(ProfileID = 26, Comparator = "England")

head(performance)
## # A tibble: 6 x 26
## # Groups:   IndicatorID, Sex, Age, CategoryType, Category [2]
##   IndicatorID IndicatorName ParentCode ParentName AreaCode AreaName
##         <int> <chr>         <chr>      <chr>      <chr>    <chr>   
## 1       93085 Smoking stat… E12000009  South Wes… E100000… Devon   
## 2       20201 Breastfeedin… E92000001  England    E120000… East Mi…
## 3       20201 Breastfeedin… E12000001  North Eas… E060000… Hartlep…
## 4       20201 Breastfeedin… E12000001  North Eas… E060000… Stockto…
## 5       20201 Breastfeedin… E12000004  East Midl… E060000… Derby   
## 6       20201 Breastfeedin… E12000006  East of E… E060000… Peterbo…
## # ... with 20 more variables: AreaType <chr>, Sex <chr>, Age <chr>,
## #   CategoryType <chr>, Category <chr>, Timeperiod <chr>, Value <dbl>,
## #   LowerCI95.0limit <dbl>, UpperCI95.0limit <dbl>,
## #   LowerCI99.8limit <dbl>, UpperCI99.8limit <dbl>, Count <dbl>,
## #   Denominator <dbl>, Valuenote <chr>, RecentTrend <chr>,
## #   ComparedtoEnglandvalueorpercentiles <chr>,
## #   ComparedtoRegionvalueorpercentiles <chr>, TimeperiodSortable <int>,
## #   Newdata <chr>, Comparedtogoal <chr>
performance %>%
  ggplot(aes(IndicatorName, forcats::fct_rev(AreaName))) +
  geom_tile(fill = "red") +
  scale_x_discrete(position  = "top") +
  theme(axis.text.x = element_text(angle = 90, hjust = 0), 
        axis.text = element_text(size = rel(.5))) +
  labs(x= "", y = "")

Other functions

dep <- deprivation_decile()
dep
##      AreaCode IMDscore decile
## 1   E06000001   33.178      2
## 2   E06000002   40.216      1
## 3   E06000003   28.567      3
## 4   E06000004   24.625      5
## 5   E06000005   23.639      5
## 6   E06000006   31.943      2
## 7   E06000007   19.312      7
## 8   E06000008   34.189      1
## 9   E06000009   41.997      1
## 10  E06000010   41.235      1
## 11  E06000011   15.792      8
## 12  E06000012   30.898      2
## 13  E06000013   21.363      6
## 14  E06000014   12.219      9
## 15  E06000015   27.790      3
## 16  E06000016   33.065      2
## 17  E06000017    9.621     10
## 18  E06000018   36.927      1
## 19  E06000019   19.737      7
## 20  E06000020   24.852      5
## 21  E06000021   34.360      1
## 22  E06000022   12.094     10
## 23  E06000023   27.161      4
## 24  E06000024   15.783      8
## 25  E06000025   11.358     10
## 26  E06000026   26.643      4
## 27  E06000027   28.788      3
## 28  E06000028   21.847      6
## 29  E06000029   15.219      8
## 30  E06000030   17.857      8
## 31  E06000031   27.659      4
## 32  E06000032   27.577      4
## 33  E06000033   24.516      5
## 34  E06000034   21.603      6
## 35  E06000035   22.332      6
## 36  E06000036   10.462     10
## 37  E06000037   10.242     10
## 38  E06000038   19.319      7
## 39  E06000039   22.873      6
## 40  E06000040    8.857     10
## 41  E06000041    5.652     10
## 42  E06000042   18.029      7
## 43  E06000043   23.441      5
## 44  E06000044   27.054      4
## 45  E06000045   26.878      4
## 46  E06000046   23.087      5
## 47  E06000047   25.741      4
## 48  E06000049   14.132      9
## 49  E06000050   18.086      7
## 50  E06000051   16.689      8
## 51  E06000052   23.833      5
## 52  E06000053   12.013     10
## 53  E06000054   13.466      9
## 54  E06000055   19.238      7
## 55  E06000056   12.201     10
## 56  E06000057   20.525      6
## 57  E08000001   28.420      3
## 58  E08000002   21.769      6
## 59  E08000003   40.512      1
## 60  E08000004   30.291      2
## 61  E08000005   33.684      1
## 62  E08000006   32.959      2
## 63  E08000007   19.108      7
## 64  E08000008   29.380      3
## 65  E08000009   15.388      8
## 66  E08000010   24.857      5
## 67  E08000011   41.387      1
## 68  E08000012   41.126      1
## 69  E08000013   29.809      2
## 70  E08000014   25.732      4
## 71  E08000015   26.892      4
## 72  E08000016   29.568      3
## 73  E08000017   29.051      3
## 74  E08000018   28.279      3
## 75  E08000019   27.568      4
## 76  E08000021   28.264      3
## 77  E08000022   21.279      6
## 78  E08000023   30.608      2
## 79  E08000024   29.725      3
## 80  E08000025   37.768      1
## 81  E08000026   28.107      3
## 82  E08000027   22.958      6
## 83  E08000028   34.614      1
## 84  E08000029   17.238      8
## 85  E08000030   30.370      2
## 86  E08000031   33.183      2
## 87  E08000032   33.168      2
## 88  E08000033   24.607      5
## 89  E08000034   23.964      5
## 90  E08000035   26.623      4
## 91  E08000036   26.892      4
## 92  E08000037   25.932      4
## 93  E09000001   13.602      9
## 94  E09000002   34.635      1
## 95  E09000003   17.813      8
## 96  E09000004   16.170      8
## 97  E09000005   26.655      4
## 98  E09000006   15.164      9
## 99  E09000007   24.959      5
## 100 E09000008   23.643      5
## 101 E09000009   23.585      5
## 102 E09000010   26.994      4
## 103 E09000011   25.544      5
## 104 E09000012   35.280      1
## 105 E09000013   24.362      5
## 106 E09000014   31.043      2
## 107 E09000015   14.302      9
## 108 E09000016   17.876      8
## 109 E09000017   18.108      7
## 110 E09000018   22.469      6
## 111 E09000019   32.534      2
## 112 E09000020   23.376      5
## 113 E09000021   11.125     10
## 114 E09000022   28.913      3
## 115 E09000023   28.591      3
## 116 E09000024   14.930      9
## 117 E09000025   32.939      2
## 118 E09000026   20.242      6
## 119 E09000027   10.037     10
## 120 E09000028   29.489      3
## 121 E09000029   14.579      9
## 122 E09000030   35.657      1
## 123 E09000031   30.190      2
## 124 E09000032   18.295      7
## 125 E09000033   27.686      3
## 126 E10000002    9.757     10
## 127 E10000003   13.393      9
## 128 E10000006   21.331      6
## 129 E10000007   18.515      7
## 130 E10000008   17.086      8
## 131 E10000009   14.336      9
## 132 E10000011   18.825      7
## 133 E10000012   17.163      8
## 134 E10000013   15.014      9
## 135 E10000014   11.917     10
## 136 E10000015   12.192     10
## 137 E10000016   18.805      7
## 138 E10000017   22.495      6
## 139 E10000018   12.457      9
## 140 E10000019   20.609      6
## 141 E10000020   21.158      6
## 142 E10000021   18.936      7
## 143 E10000023   14.646      9
## 144 E10000024   18.850      7
## 145 E10000025   11.513     10
## 146 E10000027   17.783      8
## 147 E10000028   16.380      8
## 148 E10000029   18.314      7
## 149 E10000030    9.386     10
## 150 E10000031   15.006      9
## 151 E10000032   14.027      9
## 152 E10000034   17.704      8

fingertipscharts

Having extracted data it can be visualised using the fingertipscharts package which produces visualisations available through the Fingertips website.

These include:

Example 2: Drawing maps

library(fingertipsR)
library(dplyr)
library(fingertipscharts)
df <- fingertips_data(40401, AreaTypeID = 101) %>%
        filter(Sex == "Male" &
                       AreaType == "District & UA" &
                       TimeperiodSortable == max(TimeperiodSortable))
 
## ultra-generalised lower tier LA boundaries
 
ons_api <- "https://opendata.arcgis.com/datasets/ae90afc385c04d869bc8cf8890bd1bcd_3.geojson"
 
ordered_levels <- c("Better",
                    "Similar",
                    "Worse",
                    "Not compared")
df <- df %>%
        mutate(ComparedtoEnglandvalueorpercentiles =
                       factor(ComparedtoEnglandvalueorpercentiles,
                              levels = ordered_levels))
 
 
p <- fingertipscharts::map(data = df,
                           ons_api = ons_api,
                           area_code = AreaCode,
                           fill = ComparedtoEnglandvalueorpercentiles,
                           title = "Premature deaths from heart disease",
                           subtitle = "Males in Lower Tier Local Authorities England",
                           copyright_size = 3)
p

Future directions

Further resources