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.
This function plots the Overview view (also known as the Tartan Rug). All of the charts in this vignette, starting with this one, use a function called
create_test_data() from this packages to generate a dummy dataset. This data has the same structure as data exported from the
fingertips_data() function in the
This is how the
overview() function works:
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
pivot_wider() function from the
tidyr package demonstrates how to do this below:
This is how the
compare_indicators() function works:
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
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:
It is also possible to have interactive maps. Note, for this to work the value parameter must be included and subtitle doesn’t work.
This function allows users to plot the trends graph. The dummy dataset is slightly manipulated here to extend over a few years. A significance is then prescribed based on whether the confidence intervals overlap the country value.
df <- create_test_data() %>% arrange(IndicatorName) %>% mutate(Timeperiod = rep(c("2011", "2012", "2013", "2014", "2015", "2016"), each = 111)) country_val <- df %>% filter(AreaCode == "C001") %>% select(Timeperiod, Country_val = Value) df <- df %>% left_join(country_val, by = "Timeperiod") %>% mutate(Significance = case_when( LCI > Country_val ~ "Higher", UCI < Country_val ~ "Lower", TRUE ~ "Similar" ))
Here is an example of how to use the
This code filters the dummy dataset for a single region and its parent, and all the child areas within that region.
Note, before plotting the chart, the levels of the field that is determining the fill of the bars need to be reordered, as if they aren’t, they will be ordered alphabetically in the legend:
compare_areas function makes plotting the compare areas chart straight forward.
The area profiles image is created using the
p <- area_profiles(dfspine, value = Value, count = Count, area_code = AreaCode, local_area_code = "AC110", indicator = IndicatorName, timeperiod = Timeperiod, trend = Trend, polarity = Polarity, significance = Significance, area_type = AreaType, cols = "fingertips", median_line_area_code = "C001", comparator_area_code = "PAC11", datatable = TRUE, header_positions = c(-1.2, -0.9, -0.63, -0.48, -0.36, -0.21, -0.05, 1.08), relative_domain_text_size = 0.75, relative_text_size = 1.2, bar_width = 0.68, horizontal_arrow_multiplier = 0.7) p
This function provides the ability to draw the traditional population pyramid. Note that the ordering of the age bands are defined prior to the data being provided to the
set.seed(1234) 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+") local_males <- dnorm(seq(1, 19), mean = 5, sd = 5) * 1e5 local_females <- dnorm(seq(1, 19), mean = 4, sd = 5) * 1e5 region_males <- dnorm(seq(1, 19), mean = 5, sd = 7) * 1e5 region_females <- dnorm(seq(1, 19), mean = 4.5, sd = 6.5) * 1e5 country_males <- dnorm(seq(1, 19), mean = 4, sd = 3) * 1e5 country_females <- dnorm(seq(1, 19), mean = 5, sd = 3) * 1e5 pops <- data.frame(Value = c(local_males, local_females, region_males, region_females, country_males, country_females), AreaName = c(rep("Local", 38), rep("Region", 38), rep("Country", 38)), Sex = rep(c(rep("Male", 19), rep("Female", 19)), times = 3), stringsAsFactors = FALSE) %>% mutate(Age = factor(rep(agelevels, 6), levels = agelevels))
This is how the function works:
Box plots can also be drawn.
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: