Producing charts from Fingertips

Introduction

This vignette demonstrates how to create some of the charts available in this package. All of the names of the functions match the name for the chart on Fingertips.

A note on ordering within the charts

The charts produced, generally, order the data alphabetically. In the static map section and the compare areas, there are worked examples of reordering the factor levels so they are presented in the required order in the legends.

Libraries

library(fingertipsR)
library(fingertipscharts)
library(dplyr)
library(tidyr)

Overview

This function plots the Overview view (also known as the Tartan Rug). First, let’s get some data:

region <- "North East region"
top_names <- c("England", region)
dfdom <- fingertips_data(DomainID = 8000037) %>%
        group_by(IndicatorID) %>%
        filter(TimeperiodSortable == max(TimeperiodSortable) &
                       Sex == "Persons" &
                       (AreaName %in% top_names |
                                ParentName == region)) %>%
        ungroup() %>%
        mutate(Value = round(Value, 1))

This is how the overview() function works:

p <- overview(data = dfdom, 
              area = AreaName, 
              indicator = IndicatorName, 
              value = Value,
              fill = ComparedtoEnglandvalueorpercentiles,
              timeperiod = Timeperiod,
              top_areas = top_names, wrap_length = 40,
              value_label_size = 0.7)
p

Compare indicators

Here is a function that helps produce a scatter plot as displayed on Fingertips. First the data needs to be changed to the correct format. The two variables that will be plotted need to be in separate fields. The spread() function from the tidyr package demonstrates how to do this below:

df <- fingertips_data(c(90362, 90366)) %>%
        group_by(IndicatorID) %>%
        filter(TimeperiodSortable == max(TimeperiodSortable),
               Sex == "Male",
               Age == "All ages") %>%
        ungroup() %>%
        select(IndicatorID, AreaName, Value) %>%
        mutate(IndicatorID = paste0("x", IndicatorID)) %>%
        spread(IndicatorID, Value)

This is how the compare_indicators() function works:

p <- compare_indicators(data = df,
                        x = x90362,
                        y = x90366,
                        xlab = "Healthy life expectancy at birth",
                        ylab = "Life expectancy at birth",
                        highlight = c("England", "Dorset"),
                        area = AreaName,
                        add_R2 = TRUE)
p

Maps

Here the package provides both interactive and static maps.

The shape files need to be sources from the ONS Geography Portal. To do this, click on the API button here, using Counties and Unitary Authorities as an example, and then use the GeoJSON address as the string input to the ons_api parameter.

df <- fingertips_data(90366) %>%
        filter(Sex == "Male" &
                       AreaType == "County & UA" &
                       TimeperiodSortable == max(TimeperiodSortable))

ons_api <- "https://opendata.arcgis.com/datasets/687f346f5023410ba86615655ff33ca9_4.geojson"

Static

Note, before plotting the chart, the levels of the field that is determining the fill of the polygons need to be reordered, as if they aren’t, they will be ordered alphabetically in the legend:

The default map type is static:

Interactive

It is also possible to have interactive maps. Note, for this to work the value parameter must be included and subtitle doesn’t work.

Compare areas

This code gets the latest year of data for IndicatorID 90316 filtered for local authorities whose parent is South East region and also the areas England and South East region:

region <- "South East region"
top_names <- c("England", region)
df <- fingertips_data(90316) %>%
        group_by(IndicatorID) %>%
        filter(is.na(CategoryType) &
                       TimeperiodSortable == max(TimeperiodSortable) &
                       (AreaName %in% top_names |
                                ParentName == region)) %>%
        ungroup()

Note, before plotting the chart, the levels of the field that is determining the fill of the polygons need to be reordered, as if they aren’t, they will be ordered alphabetically in the legend:

ordered_levels <- c("Better",
                    "Similar", 
                    "Worse",
                    "Not compared")
df <- df %>%
        mutate(ComparedtoEnglandvalueorpercentiles = 
                       factor(ComparedtoEnglandvalueorpercentiles,
                              levels = ordered_levels)) %>%
        filter(Sex == "Persons")

The compare_areas function makes plotting the compare areas chart straight forward.

p <- compare_areas(df, AreaName, Value,
                      fill = ComparedtoEnglandvalueorpercentiles,
                      lowerci = LowerCI95.0limit,
                      upperci = UpperCI95.0limit,
                      order = "desc",
                      top_areas = top_names,
                      title = unique(df$IndicatorName))
p

Area profiles

The area profiles image is created using the area_profiles() function. The argument rank = TRUE needs to be included as this returns a polarity field, which is needed for the spine chart:

dfspine <- fingertips_data(DomainID = 1938133222, rank = TRUE) %>%
        filter(Timeperiod == "2016")
## Warning: The following named parsers don't match the column names:
## Indicator full name, Policy
p <- area_profiles(dfspine,
                   value = Value,
                   count = Count,
                   area_code = AreaCode,
                   local_area_code = "E06000020",
                   indicator = IndicatorName,
                   timeperiod = Timeperiod,
                   polarity = Polarity,
                   significance = ComparedtoEnglandvalueorpercentiles,
                   area_type = AreaType,
                   cols = "fingertips",
                   median_line_area_code = "E92000001",
                   comparator_area_code = "E12000005",
                   datatable = TRUE,
                   header_positions = c(-1.43, -0.63, -0.43, -0.31, -0.19, -0.05, 1.08),
                   relative_domain_text_size = 0.75,
                   relative_text_size = 1.2,
                   bar_width = 0.68)
p

Population

This function provides the ability to draw the traditional population pyramid. First, the population data is extracted using fingertipsR package. Also, note that the ordering of the age bands are defined prior to the data being provided to the population() function:

agelevels <- c("0-4", "5-9","10-14","15-19",
               "20-24","25-29","30-34",
               "35-39","40-44","45-49",
               "50-54","55-59","60-64",
               "65-69","70-74","75-79",
               "80-84","85-89","90+")
pops <- fingertips_data(92708) %>%
        filter(TimeperiodSortable == max(TimeperiodSortable) &
                       Sex %in% c("Male", "Female") &
                       Age != "All ages") %>%
        mutate(Age = gsub(" yrs","", Age),
               Age = factor(Age,
                            levels = agelevels)) %>%
        droplevels()

This is how the function works:

p <- population(pops,
                value = Value,
                sex = Sex,
                age = Age,
                area = AreaName,
                area_name = "Nottingham",
                comparator_1 = "England",
                comparator_2 = "East Midlands region",
                title = "Age Profile",
                subtitle = paste(unique(pops$IndicatorName),
                                 unique(pops$Timeperiod)),
                xlab = "% of total population")
p

Box plots

Box plots can be found on some profiles. First the data is extracted using the fingertipsR package:

df <- fingertips_data(90366) %>%
        filter(Sex == "Male",
               AreaType == "County & UA",
               Age == "All ages")

This is how the function works. Note, the levels of the factor for the timeperiod parameter may need to be reordered, as this determines the order along the x-axis:

p <- box_plots(df,
               timeperiod = Timeperiod,
               value = Value,
               title = "Life expectancy at birth",
               subtitle = "Males in Upper Tier Local Authorities within England",
               ylab = "Age (years)")
p