NHS Names - Graph Example
Calum Polwart
2023-08-29
Source:vignettes/graph_example.Rmd
graph_example.Rmd
Some NHS Organisations have really long names! That means if you want to plot some information with those names on the X axis your graph can become unmanageable. The solution is to shorten or abbreviate those names in some way.
In addition, there are a few ‘formatting’ issues even with standard NHS organisation names if you wanted to include them in a markdown document. You can see more of this later in the vignette.
Lets assume we for some reason wanted to draw a graph of the number of characters in an NHS Organisations official name. We might start with obtaining a list of NHS Organisation names.
We will start by downloading a list of NHS organisations
download.file ("https://files.digital.nhs.uk/assets/ods/current/etr.zip", destfile = "etr.zip")
unzip("etr.zip")
trusts <- read.csv("etr.csv", header=F)
head(trusts)|> DT::datatable(options = list(scrollX = TRUE, paging=FALSE, fixedHeader=TRUE), style='bootstrap')
As you can see, we now have a list of trust names in V2, all fully capitalised. This is a very common format to receive NHS name data.
We can now use some quick and simple tidyverse to tidy up the data:
trusts |>
dplyr::select(Code = V1, Name = V2) |>
# we will add a colum for the number of characters in the trust name
dplyr::mutate(Chars = nchar(Name)) |>
# then sort the data to have the longest trust names first
dplyr::arrange(-Chars) -> trusts
head(trusts)|> DT::datatable(options = list(scrollX = TRUE, paging=FALSE, fixedHeader=TRUE ), style='bootstrap')
If we now want to graph that (lets use the first 15 rows for simplicity) we can do:
require(ggplot2)
#> Loading required package: ggplot2
head(trusts, 15) |>
ggplot(aes(x=Name, y=Chars)) +
geom_col() -> p1
print(p1)
Obviously the x axis with the trust names on it is unreadable. We can try rotating the labels:
p1 +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
The labels are now visible, but the graph doesn’t fit on the screen! Which is why the NHSnames package exists!
AbbreviateNHS
There are a few things we could do to help. Firstly, we could abbreviate every trust to its initials. That’s a common approach within the NHS, and in a local region where the initials may be recognisable might work
trusts$Abrev <- NHSnames::AbbreviateNHS(trusts$Name)
#> Warning in NHSnames::AbbreviateNHS(trusts$Name): Abbreviation of organisation
#> names has resulted in non-unique names! Consider using MiniNHS instead
head(trusts, 15) |>
ggplot(aes(x=Abrev, y=Chars)) +
geom_col() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
MiniNHS
You’ll see above we received a warning. This is telling us that the abbreviations are not unique. You’ll also see they aren’t very helpful to read. In fact, you might be better off simply using the NHS ODS code. However, NHS ODS codes are not particularly memorable. If we want our audience to be able to relate to the data we must be able to do better.
trusts$Mini <- NHSnames::MiniNHS(trusts$Name)
head(trusts, 15) |>
ggplot(aes(x=Mini, y=Chars)) +
geom_col() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
Here is the result of the MiniNHS function. This shortens parts of the name, but keeps the data readable. It is also returning it as title case, which takes up less space, and is more readable. If we use stringr’s rather nifty wrap function we can make that even better:
trusts$Mini <- NHSnames::MiniNHS(trusts$Name)
head(trusts, 15) |>
ggplot(aes(x=Mini, y=Chars)) +
geom_col() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 29))
Inline Names
TitleCaseNHS
Sometimes you will want to include the name of an organisation in text, perhaps using markdown. In that case you might want to use some inline r commands like this:
The trust with the longest name is ‘r trusts$Name[trusts$Chars == max(trusts$Chars)]’ and the shortest is ‘r trusts$Name[trusts$Chars == min(trusts$Chars)]’.
That will render as:
The trust with the longest name is THE ROBERT JONES AND AGNES HUNT ORTHOPAEDIC HOSPITAL NHS FOUNDATION TRUST and the shortest is SOLENT NHS TRUST.
Which isn’t what we want as its in capital letters. We can apply a title case function such as stringr::str_to_title() to that but that will then render the NHS as Nhs and NHS house style says that conjuctions (words like ‘and’) should be in all lower case. So: The Robert Jones And Agnes Hunt Orthopaedic Hospital Nhs Foundation Trust isn’t quite what we want.
Which is where NHSnames::TitleCaseNHS() comes to hand The Robert Jones and Agnes Hunt Orthopaedic Hospital NHS Foundation Trust renders from - ‘r NHSnames::TitleCaseNHS(trusts$Name[trusts$Chars == max(trusts$Chars)])’
ShortenNHS
Sometimes even that will be a bit of a mouthful to read, in which case, you might prefer NHSnames::ShortenNHS(). It will drop the NHS and Foundation Trust from the description allowing you to render:
The trust with the longest name is ‘r NHSnames::ShortenNHS(NHSnames::TitleCaseNHS(trusts$Name[trusts$Chars == max(trusts$Chars)]))’ and the shortest is ‘r NHSnames::ShortenNHS(NHSnames::TitleCaseNHS(trusts$Name[trusts$Chars == min(trusts$Chars)]))’.
into:
The trust with the longest name is The Robert Jones and Agnes Hunt Orthopaedic Hospital and the shortest is Solent.