Plotting healthy life expectancy and life expectancy by deprivation for English local authorities

This worked example attempts to document a common workflow a user might follow when using the fingertipsR package.

fingertipsR provides users the ability to import data from the Fingertips website. Fingertips is a major repository of public health indicators in England. The site is structured in the following way:

This example demonstrates how you can plot healthy life expectancy and life expectancy by deprivation for a given year of data that fingertips contains. So, where to start?

Where to start

There is one function in the fingertipsR package that extracts data from the Fingertips API: fingertips_data(). This function has the following inputs:

At least one of IndicatorID, DomainID or ProfileID must be complete. These fields relate to each other as described in the introduction. AreaCode needs completing if you are extracting data for a particular area or group of areas only. AreaTypeID determines the geography to extract the data for. In this case we want County and Unitary Authority level. ParentAreaTypeID requires an area type code that the AreaTypeID maps to at a higher level of geography. For example, when combining groups of County and Unitary Authorities it is possible to create Goverenment Office Regions. These mappings can be identified using the area_types() funciton. If ignored, a ParentAreaTypeID will be chosen automatically.

Therefore, the inputs to the fingertips_data function that we need to find out are the ID codes for:

We need to begin by calling the fingertipsR package:

library(fingertipsR)

IndicatorID

There are two indicators we are interested in for this exercise. Without consulting the Fingertips website, we know approximately what they are called:

We can use the indicators() function to return a list of all the indicators within Fingertips. We can then filter the name field for the term life expectancy (note, the IndicatorName field has been converted to lower case in the following code chunk to ensure matches will not be overlooked as a result of upper case letters).

inds <- indicators_unique()
life_expectancy <- inds[grepl("life expectancy", tolower(inds$IndicatorName)),]

knitr::kable(life_expectancy, row.names = FALSE) #note, this line will only work in a markdown file (*.Rmd). It presents the table for a report
IndicatorID IndicatorName
90362 Healthy life expectancy at birth: the average number of years a person would expect to live in good health based on contemporary mortality rates and prevalence of self-reported good health.
90362 0.1i - Healthy life expectancy at birth: the average number of years a person would expect to live in good health based on contemporary mortality rates and prevalence of self-reported good health.
90365 0.2iv - The gap in years between overall life expectancy at birth in each English local authority and life expectancy at birth for England as a whole.
90366 0.1ii - Life expectancy at birth: the average number of years a person would expect to live based on contemporary mortality rates.
90823 0.2ii - Number of upper tier local authorities for which the local slope index of inequality in life expectancy (as defined in indicator 0.2iii) has decreased
90825 0.2v - Slope index of inequality in healthy life expectancy at birth based on national deprivation deciles within England: the range in years of life expectancy across the social gradient, from most to least deprived.
91102 0.1ii - Life expectancy at 65: the average number of years a person would expect to live based on contemporary mortality rates.
92031 0.2vi - SII in healthy life expectancy based within local authorities, based on deprivation within Middle Super Output Areas
92900 0.2i - Slope index of inequality in life expectancy at birth based on national deprivation deciles within England: the range in years of life expectancy across the social gradient, from most to least deprived.
92901 0.2iii - Slope index of inequality in life expectancy at birth within English local authorities, based on local deprivation deciles: the range in years of life expectancy across the social gradient within each local authority, from most to least deprived.
92902 0.2vii - Slope index of inequality in life expectancy at birth within English region, based on regional deprivation deciles: the range in years of life expectancy across the social gradient within each local authority, from most to least deprived.
93189 0.2i - Slope index of inequality in life expectancy at 65 based on national deprivation deciles within England: the range in years of life expectancy across the social gradient, from most to least deprived.
93190 0.2iii - Slope index of inequality in life expectancy at age 65 within English local authorities, based on local deprivation deciles: the range in years of life expectancy across the social gradient within each local authority, from most to least deprived.
93191 0.2vii - Slope index of inequality in life expectancy at age 65 within English local regions, based on regional deprivation deciles: the range in years of life expectancy across the social gradient within each region, from most to least deprived.
650 Life expectancy - MSOA based
90366 Life expectancy at birth: the average number of years a person would expect to live based on contemporary mortality rates.
91102 Life expectancy at 65: the average number of years a person would expect to live based on contemporary mortality rates.
90362 Healthy life expectancy at birth: the average number of years a person would expect to live in good health based on contemporary mortality rates and prevalence of self-reported good health. (in PHOF 0.1i)
90366 Life expectancy at birth: the average number of years a person would expect to live based on contemporary mortality rates. (in PHOF 0.1ii)
92901 Slope index of inequality in life expectancy at birth within English local authorities, based on local deprivation deciles: the range in years of life expectancy across the social gradient within each local authority, from most to least deprived. (In PHOF 0.2iii)
92031 Slope index of inequality in healthy life expectancy within local authorities, based on deprivation within Middle Super Output Areas (MSOAs): the range in years of life expectancy across the social gradient within each local authority. (in PHOF 0.2vi)
93249 Disability-free life expectancy
93283 Life expectancy at birth, (upper age band 90+)
93285 Life expectancy at birth, (upper age band 85+)
93298 Healthy life expectancy, (upper age band 85+)

The two indicators we are interested in from this table are:

AreaTypeID

We can work out what the AreaTypeID codes we are interested in using the function area_types(). We’ve decided that we want to produce the graph at County and Unitary Authority level. From the section Where to start we need codes for AreaTypeID and ParentAreaTypeID.

areaTypes <- area_types()
DT::datatable(areaTypes, filter = "top", rownames = FALSE) #note, this line will only work in a markdown file (*.Rmd). It presents the table for a report

The table shows that the AreaID for County and Unitary Authority level is 102. The third column, ParentAreaTypeID, shows the IDs of the area types that these map to. In the case of County and Unitary Authorities, these are:

ParentAreaTypeID ParentAreaTypeName
6 Government Office Region
42 2013 PHE Regions (4)
10039 Depriv. decile (IMD 2015)
103 PHEC 2013 only plus PHEC unchanged
104 PHEC 2015 new plus PHEC 2013 unchanged
126 Combined authorities

ParentAreaTypeID is 6 by default for the fingertips_data() function for AreaTypeID of 102 (though it changes if different AreaTypeIDs are entered), so we can stick with that in this example. Use the area_types() function to understand more about how areas map to each other.

Extracting the data

Finally, we can use the fingertips_data() function with the inputs we have determined previously.

indicators <- c(90362, 90366)
data <- fingertips_data(IndicatorID = indicators,
                        AreaTypeID = 102)

pander::pandoc.table(tail(data), 
                     style="rmarkdown",
                     split.tables = 90, 
                     keep.line.breaks = TRUE) #note, this line will only work in a markdown file (*.Rmd). It presents the table for a report
## 
## 
## |  &nbsp;  | IndicatorID |              IndicatorName              | ParentCode |
## |:--------:|:-----------:|:---------------------------------------:|:----------:|
## | **6475** |    90362    | 0.1i - Healthy life expectancy at birth | E12000005  |
## | **6476** |    90362    | 0.1i - Healthy life expectancy at birth | E12000006  |
## | **6477** |    90362    | 0.1i - Healthy life expectancy at birth | E12000008  |
## | **6478** |    90362    | 0.1i - Healthy life expectancy at birth | E12000005  |
## | **6479** |    90362    | 0.1i - Healthy life expectancy at birth | E12000008  |
## | **6480** |    90362    | 0.1i - Healthy life expectancy at birth | E12000005  |
## 
## Table: Table continues below
## 
##  
## 
## |  &nbsp;  |       ParentName       | AreaCode  |    AreaName    |  AreaType   |
## |:--------:|:----------------------:|:---------:|:--------------:|:-----------:|
## | **6475** |  West Midlands region  | E10000028 | Staffordshire  | County & UA |
## | **6476** | East of England region | E10000029 |    Suffolk     | County & UA |
## | **6477** |   South East region    | E10000030 |     Surrey     | County & UA |
## | **6478** |  West Midlands region  | E10000031 |  Warwickshire  | County & UA |
## | **6479** |   South East region    | E10000032 |  West Sussex   | County & UA |
## | **6480** |  West Midlands region  | E10000034 | Worcestershire | County & UA |
## 
## Table: Table continues below
## 
##  
## 
## |  &nbsp;  |  Sex   |   Age    | CategoryType | Category | Timeperiod | Value |
## |:--------:|:------:|:--------:|:------------:|:--------:|:----------:|:-----:|
## | **6475** | Female | All ages |      NA      |    NA    | 2014 - 16  | 65.38 |
## | **6476** | Female | All ages |      NA      |    NA    | 2014 - 16  | 65.37 |
## | **6477** | Female | All ages |      NA      |    NA    | 2014 - 16  | 68.11 |
## | **6478** | Female | All ages |      NA      |    NA    | 2014 - 16  | 66.25 |
## | **6479** | Female | All ages |      NA      |    NA    | 2014 - 16  | 64.03 |
## | **6480** | Female | All ages |      NA      |    NA    | 2014 - 16  | 67.97 |
## 
## Table: Table continues below
## 
##  
## 
## |  &nbsp;  | LowerCI95.0limit | UpperCI95.0limit | LowerCI99.8limit |
## |:--------:|:----------------:|:----------------:|:----------------:|
## | **6475** |      63.87       |       66.9       |        NA        |
## | **6476** |      63.68       |      67.06       |        NA        |
## | **6477** |       66.7       |      69.53       |        NA        |
## | **6478** |      64.53       |      67.98       |        NA        |
## | **6479** |      62.32       |      65.74       |        NA        |
## | **6480** |       66.3       |      69.63       |        NA        |
## 
## Table: Table continues below
## 
##  
## 
## |  &nbsp;  | UpperCI99.8limit | Count | Denominator | Valuenote |
## |:--------:|:----------------:|:-----:|:-----------:|:---------:|
## | **6475** |        NA        |  NA   |     NA      |    NA     |
## | **6476** |        NA        |  NA   |     NA      |    NA     |
## | **6477** |        NA        |  NA   |     NA      |    NA     |
## | **6478** |        NA        |  NA   |     NA      |    NA     |
## | **6479** |        NA        |  NA   |     NA      |    NA     |
## | **6480** |        NA        |  NA   |     NA      |    NA     |
## 
## Table: Table continues below
## 
##  
## 
## |  &nbsp;  |     RecentTrend      | ComparedtoEnglandvalueorpercentiles |
## |:--------:|:--------------------:|:-----------------------------------:|
## | **6475** | Cannot be calculated |            Not compared             |
## | **6476** | Cannot be calculated |            Not compared             |
## | **6477** | Cannot be calculated |            Not compared             |
## | **6478** | Cannot be calculated |            Not compared             |
## | **6479** | Cannot be calculated |            Not compared             |
## | **6480** | Cannot be calculated |            Not compared             |
## 
## Table: Table continues below
## 
##  
## 
## |  &nbsp;  | Comparedtosubnationalparentvalueorpercentiles | TimeperiodSortable |
## |:--------:|:---------------------------------------------:|:------------------:|
## | **6475** |                 Not compared                  |      20140000      |
## | **6476** |                 Not compared                  |      20140000      |
## | **6477** |                 Not compared                  |      20140000      |
## | **6478** |                 Not compared                  |      20140000      |
## | **6479** |                 Not compared                  |      20140000      |
## | **6480** |                 Not compared                  |      20140000      |
## 
## Table: Table continues below
## 
##  
## 
## |  &nbsp;  | Newdata | Comparedtogoal |
## |:--------:|:-------:|:--------------:|
## | **6475** |   NA    |       NA       |
## | **6476** |   NA    |       NA       |
## | **6477** |   NA    |       NA       |
## | **6478** |   NA    |       NA       |
## | **6479** |   NA    |       NA       |
## | **6480** |   NA    |       NA       |

The data frame returned by fingertips_data() contains 26 variables. For this exercise, we are only interested in a few of them and for the time period 2012-14:

cols <- c("IndicatorID", "AreaCode", "Sex", "Timeperiod", "Value")
data <- data[data$AreaType == "County & UA" & data$Timeperiod == "2012 - 14", cols]

Deprivation

We want to plot life expectancy against deprivation information. As deprivation is a notable cause of health inequalities, the deprivation_deciles() function has been provided to allow easy access to this information. This is populated from the Department for Communities and Local Government Indices of Multiple Deprivation (IMD). Note, there is only information for General Practices, upper and lower tier local authorities (AreaTypeID = 7, 102 and 101 respectively). IMD has only been produced for the years 2010 and 2015 (for the latter areas) as well as 2011 and 2012 for General Practices.

dep <- deprivation_decile(AreaTypeID = 102, Year = 2015)
DT::datatable(dep, filter = "top", rownames = FALSE) #note, this line will only work in a markdown file (*.Rmd). It presents the table for a report

Now we need to merge this with the main dataset:

# merge deprivation onto data
data <- merge(data, dep, by.x = "AreaCode", by.y = "AreaCode", all.x = TRUE)

# remove NA values
data <- data[complete.cases(data),]
DT::datatable(data, filter = "top", rownames = FALSE) #note, this line will only work in a markdown file (*.Rmd). It presents the table for a report

Plotting outputs

Using ggplot2 it is possible to plot the outputs.

library(ggplot2)
p <- ggplot(data, aes(x = IMDscore, y = Value, col = factor(IndicatorID)))
p <- p + 
        geom_point() +
        geom_smooth(se = FALSE, method = "loess") +
        facet_wrap(~ Sex) +
        scale_colour_manual(name = "Indicator",
                            breaks = c("90366", "90362"),
                            labels = c("Life expectancy", "Healthy life expectancy"),
                            values = c("#128c4a", "#88c857")) +
        scale_x_reverse() + 
        labs(x = "IMD deprivation",
             y = "Age",
             title = "Life expectancy and healthy life expectancy at birth \nfor Upper Tier Local Authorities (2012 - 2014)") +
        theme_bw()
print(p)