knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
This documentation will guide you on how to use the THFstyle
package to create charts with The Health Foundation (THF) branding. THFstyle
package standardises the colours, shapes, text and sizes of charts by just adding it together with simple functions from the ggplot2 package. We provide below two examples of types of charts produced by the Improvement Analytics Unit (IAU) as part of their evaluations using THF_theme.
library(THFstyle) library(broom) library(dplyr) library(ggplot2) library(tidyr) library(boot)
The below piece of code creates a forest plot for the coeffiecients of a regression model. Here we assume that we have two types of outcomes (counts and proportions) and we show the confidence intervals for the relative log-scaled risk.
We will start by creating a dataframe with coefficient estimates from regressions. We do this by running regressions for binary and count data and combining the estimates that we are interested in.
model_carb <- glm(carb ~ mpg + cyl + hp + vs , data=mtcars, family = poisson(link = "log")) model_carb_tidy <- broom::tidy(model_carb, conf.int = TRUE) %>% filter(term=="vs") %>% mutate(outcome="carb") %>% mutate(measure="count") %>% as.data.frame() model_am <- glm(am ~ vs , data=mtcars, family=binomial(link = "logit")) model_am_tidy <- broom::tidy(model_am, conf.int = TRUE) %>% filter(term=="vs") %>% mutate(outcome="am") %>% mutate(measure="proportion") %>% as.data.frame() coef_data <- rbind(model_carb_tidy, model_am_tidy) %>% rename(ci_low=conf.low, ci_high=conf.high)
We can now create a forest plot using the data we just created. We use theme_THF
to make the background and axes look right and scale_colour_THF
to get the right colours.
ggplot2::ggplot(data = coef_data, aes(y = outcome, x = estimate, xmin = ci_low, xmax = ci_high, color = measure)) + ggplot2::geom_vline(xintercept = 1, linetype = "dashed", size = 0.6, colour = "grey75") + ggplot2::geom_errorbarh(height = 0.2) + ggplot2::geom_point(shape=21, fill="white", size = 3) + ylab('') + xlab("Relative risk, log-scaled") + theme_THF() + scale_colour_THF()
The below piece of code creates a barchart for counts of some binary variables. Here we can assume that binary variables represent comorbity flags and we show the proportions for two groups of populations (intervention and control).
nuclear_dat <- boot::nuclear %>% select(intervention=pt, pr, ct, bw) # create proportion of subjects with comorbs in each intervention group ---- prop_dat <- nuclear_dat %>% pivot_longer(-intervention, names_to = 'variable') %>% group_by(intervention, variable, value) %>% summarise(count = n()) %>% mutate(prop = count/sum(count)) %>% filter(value == 1) %>% ungroup() # take intervention proportions only to create an ordering variable ---- order_dat <- prop_dat %>% filter(intervention == 1) %>% mutate(order = prop) %>% dplyr::select(variable, order) # merge ordering variable onto proportion data ---- bar_data <- left_join(prop_dat, order_dat, by = "variable")
ggplot2::ggplot(data = bar_data , aes(x = reorder(variable, -order), y = prop, width = 0.5, fill = factor(intervention))) + scale_fill_THF(name = "", breaks=c(1,0), labels = c("intervention", "control")) + geom_bar(position = position_dodge(width=0.6), stat = "identity") + scale_y_continuous(expand = c(0,0),"", breaks = seq(0, 1 , by = 0.1), limits = c(0, 1)) + scale_x_discrete("") + theme_THF()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.