fergustaylor / openprescribingR

A set of tools for loading openprescribing.net data into R.
1 stars 0 forks source link

Difficulty plotting old ccg outlines #2

Open fergustaylor opened 7 years ago

fergustaylor commented 7 years ago

It's possible to return CCG geometries with st_read, as a simple features collection version of CCG_boundaries_or_location(). Then you can join this to a spending function to give a dataset with the ccg geometries as a column which makes it easier to plot gifs, leaflets, geom_sf stuff over time. BUT this assumes the CCGs are consistent over time, which doesn't appear to be the case going by row_name alone, we know there are name changes, but not necessarily if the geometries change.

Example dataset

ccggeom <- st_read("https://openprescribing.net/api/1.0/org_location/?org_type=ccg") %>%
  dplyr::rename(row_name = name) %>%
  select(-ons_code, -org_type)

dataframe <- dplyr::full_join(

  (openprescribingR::list_size() %>%
  select(-row_id)), 

  (spending_by_CCG(chemical_section_or_presentation_code = "7.4.5")), 

  by = c("row_name", "date")) %>%
  dplyr::mutate(costperperson = actual_cost/total_list_size) %>%
  dplyr::full_join(ccggeom, by="row_name") %>%
  st_as_sf() %>%
  dplyr::mutate(label = stringr::str_c(row_name, " £", format(round(costperperson, 2), nsmall = 2)))

But looking at the unique rownames from a spending function. shows some names not included in CCG_boundaries_or_location().

dplyr::setdiff(unique(dataframe$row_name), unique(ccggeom$row_name))

[1] "NHS NEWCASTLE WEST CCG" "NHS NORTH MANCHESTER CCG"
[3] "NHS SOUTH MANCHESTER CCG" "NHS NEWCASTLE NORTH AND EAST CCG" [5] "NHS GATESHEAD CCG" "NHS CENTRAL MANCHESTER CCG"

fergustaylor commented 7 years ago

Oddly enough adding a date to the CCG_boundaries_or_location() will return a result. But since it has 207 entries I assume it's just the same result it returns without a date.

ccggeom2 <- st_read("https://openprescribing.net/api/1.0/org_location/?org_type=ccg&date=2012-07-01") %>%
  dplyr::rename(row_name = name) %>%
  select(-ons_code, -org_type)

unique(filter(dataframe, date=="2012-07-01")$row_name)
fergustaylor commented 7 years ago
oldccgdifferences <- function(argument) 
{
dplyr::setdiff(unique(filter(dataframe, date==argument)$row_name), unique(ccggeom$row_name))
}

oldccglist <- lapply(unique(dataframe$date), oldccgdifferences) %>%
  setNames(unique(dataframe$date))

Shows the additional CCGs from previous months.

newccgdifferences <- function(argument) 
{
dplyr::setdiff(unique(ccggeom$row_name), unique(filter(dataframe, date==argument)$row_name))
}

newccglist <- lapply(unique(dataframe$date), newccgdifferences) %>%
  setNames(unique(dataframe$date))

Shows that there are no CCGs listed in ccggeom that aren't listed in previous months. I.e no new CCGs have been formed ccggeom, only disbanded. N.B this only refers to them by name, it doesn't tell you anything about the geometries, which may have changed (i.e absorbed the geography of a now-disbanded ccg, but kept the same title as before).