Dear Jim,
Thanks again so much for your help with the table. I am trying to create a pdf document (needs to be pdf) to present the code chunks and working and print the final three figures in the pdf but the text seems to be outside the code margins of the boxes when printed in pdf. Also, I don't know how to change the font of the tables to the same as the text - maybe not possible?
Many thanks again,
CM1
title: 'Unintentional injuries in Scotland: admissions to hospital and deaths'
author: "CM1"
output:
pdf_document: default
html_document: default
word_document: default
toc: yes
toc_float: yes
theme: flatly
r format(Sys.time(), '%d %B, %Y')
Overview
Unintentional injury is a common cause of emergency admission to hospital for adults and children.
Data Processing
# Set coding display settings to show code in HTML document, note cog settings switched to prevent warnings and messages from being presented in HTML document
knitr::opts_chunk$set(
echo = TRUE,
message = FALSE,
warning = FALSE,
dpi = 300)
# Load in R packages
library(tidyverse)
library(ggthemes)
# Load the janitor package to allow extra themes and geoms for plotting
library(janitor)
# Set and examine a variety of global options which affect the way that R computes and displays results. Scipen refers to the use of integers where a penalty is applied when deciding to print numeric values in fixed or exponential
options(scipen = 9)
library(knitr)
library("kableExtra")
library("dplyr")
library("readr")
library("tidyr")
library("forcats")
library("formatR")
# Read in admissions data
orig_ui_admissions = read_csv("https://www.opendata.nhs.scot/dataset/b0135993-3d8a-4f3b-afcf-e01f4d52137c/resource/aee43295-2a13-48f6-bf05-92769ca7c6cf/download/ui_admissions_2020.csv")
# Review the nature and content of variable headings to assist with code writing for plotting
glimpse(orig_ui_admissions)
# Fix the size of the figure by adding comments into {r} command
# Pipe the original admissions data set
orig_ui_admissions %>%
# Filter the data to include the most recent year or data, the whole of Scotland health board data as comparison is then with a larger figure and focuses on the distribution of unintentional injuries by age and sex. Exclude data points that include "All" as will confuse the creation of a table
filter(FinancialYear == "2018/19", HBR == "S92000003" & AgeGroup != "All" & Sex != "All" & InjuryType != "All Diagnoses") %>%
# Order age group categories in ascending order to provide clearer presentation and interpretation of the data
mutate(AgeGroup = factor(AgeGroup, levels = c("0-4 years", "5-9 years", "10-14 years", "15-24 years", "25-44 years", "45-64 years", "65-74 years", "75plus years"))) %>%
# Group the data according to sex, age group, and type of injury
group_by(Sex, AgeGroup, InjuryType)%>%
# Add all the admissions for each group, creating a cumulative total
summarise(n = sum (NumberOfAdmissions)) %>%
# Create a new variable for total number of admissions for each type of injury for analysis of relative frequencies of each type of injury according to age and sex
mutate(pct = n / sum(n)) %>%
# Set axis characteristics, and fill the columns according to injury type
ggplot(aes(x = InjuryType, y = pct, fill = InjuryType)) +
# Specify style of plot
geom_col()+
# Create a grid of tables grouped according to age and sex for easy visual comparison
facet_wrap(~AgeGroup, nrow = 2) +
facet_grid(Sex~AgeGroup)+
# Ensure that scales are continuous and range from 0 - 95% so that labels appear tidy on the plot
scale_y_continuous(labels = scales::percent_format(), limits = c(0, 0.95)) +
# Specfic limits of graph from 0 - 100% so that images are uniform and allow comparison
expand_limits(y = c(0, 100)) +
# Remove padding around limits of plots before zero
coord_cartesian(expand = FALSE) +
# Specify theme for presentation of plot
theme_excel_new() +
# Specify position of the legend
theme(axis.text.x = element_blank(), legend.position = "bottom") +
# Create title and subtitle for the plot and create an object name
labs(title = "Admissions to hospital with unintentional injury", subtitle = "Grouped by age and sex", x = "", y = "Percentage of unintentional injury admissions") -> Fig_1
# Read in mortality data
orig_ui_deaths = read_csv("https://www.opendata.nhs.scot/dataset/b0135993-3d8a-4f3b-afcf-e01f4d52137c/resource/89807e07-fc5f-4b5e-a077-e4cf59491139/download/ui_deaths_2020.csv")
# Review the nature and content of variable headings
glimpse(orig_ui_deaths)
library("knitr")
library("kableExtra")
library("dplyr")
library("readr")
library("tidyr")
library("forcats")
orig_ui_deaths = read_csv("https://www.opendata.nhs.scot/dataset/b0135993-3d8a-4f3b-afcf-e01f4d52137c/resource/89807e07-fc5f-4b5e-a077-e4cf59491139/download/ui_deaths_2020.csv")
summary_tab <- orig_ui_deaths %>%
# Apply filters in same was as in plots so looking at one year, whole of Scotland, and exlucing "All" entries
filter(Year == "2018", HBR == "S92000003", AgeGroup != "All" & Sex != "All", InjuryType != "Accidental exposure" & InjuryType != "All") %>%
# Group the table according to injury type, age, and sex
group_by(InjuryType, AgeGroup, Sex) %>%
# Create a summary of total number of deaths for neater appearance to the table and demonstration of the figures
summarise(total_deaths = sum(NumberOfDeaths)) %>%
# Change the orientation of the table so that age groups become the variable headings
pivot_wider(names_from = AgeGroup, values_from = total_deaths) %>%
mutate(Sex = factor(Sex)) %>%
arrange(Sex) %>%
select(Sex, everything())
# Create table to present the data]
Fig_2 <- kable(summary_tab[, -1],
caption = "Deaths from unintentional injuries in Scotland",
format = "latex", booktabs = TRUE) %>%
kable_styling(font_size = 10) %>%
pack_rows(tab_kable, colnum = 1,
index = table(fct_inorder(summary_tab$Sex), useNA = "no"))
# Join original admissions and deaths tibbles using full_join so that all admission and death variables are included in the tibble
ui_total1 = full_join(orig_ui_admissions, orig_ui_deaths)
# Review the nature and content of variable headings to assist with code writing for plotting
glimpse(ui_total1)
# Fix the size of the figure by adding comments into {r} command
# Pipe the joined data set
ui_total1 %>%
# Filter the data to include the most recent year or data, the whole of Scotland health board data as comparison is then with a larger figure and focuses on the distribution of unintentional injuries by age and sex. Exclude data points that include "All" as will confuse the creation of a table
filter(FinancialYear == "2018/19" & HBR == "S92000003" & AgeGroup != "All" & Sex != "All" & InjuryType != "All Diagnoses" & NumberOfDeaths != "NA") %>%
# Order age group categories in ascending order to provide clearer presentation and interpretation of the data
mutate(AgeGroup = factor(AgeGroup, levels = c("0-4 years", "5-9 years", "10-14 years", "15-24 years", "25-44 years", "45-64 years", "65-74 years", "75plus years"))) %>%
# Add all the admissions for each group, creating a cumulative total
group_by(Sex, AgeGroup, InjuryType) %>%
# Add all the deaths for each group, creating a cumulative total
summarise(n = sum(NumberOfDeaths), m = sum(NumberOfAdmissions)) %>%
# Create a new variable for proportion of number of deaths for admission for each type of injury for each type of injury according to age and sex
mutate(pct = n / m) %>%
# Set axis characteristics, and fill the columns according to injury type
ggplot(aes(x = InjuryType, y = pct, fill = InjuryType)) +
# Specify style of plot
geom_col()+
# Create a grid of tables grouped according to age and sex for easy visual comparison
facet_wrap(~AgeGroup, nrow = 2) +
facet_grid(Sex~AgeGroup)+
# Ensure that scales are continuous and range from 0 - 30% due to smaller numbers
scale_y_continuous(labels = scales::percent_format(), limits = c(0, 0.3)) +
# Remove padding around limits of plots before zero
coord_cartesian(expand = FALSE) +
# Specify theme for presentation of plot
theme_excel_new() +
# Specify position of the legend
theme(axis.text.x = element_blank(), legend.position = "bottom") +
# Create title and subtitle for the plot and create an object name
labs(title = "Percentage of deaths among those admitted with unintentional injury", subtitle = "by age and sex", x = "", y = "number of patients") -> Fig_3
Results
Fig_1
Fig_2
Fig_3