Skip to content

Commit

Permalink
switching code from active to example (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanodell committed Oct 31, 2021
1 parent d605be6 commit f35a0e5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 51 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
^cran-comments\.md$
CONTRIBUTING.md
^CRAN-RELEASE$
vignettes/Introduction-to-work-and-health-nomis-indicators.Rmd
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

* Better error message when API returns empty data in some circumstances.

* Suppressed printing of column types when reading CSV files (#25)
* Suppressed printing of column types when reading CSV files (#25, thanks jackobailey)


# nomisr 0.4.4
Expand Down
8 changes: 4 additions & 4 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

## Release summary

This is an resubmission of the `nomisr` package, version number 0.4.4. It
removes the use of the deprecated `tibble::as.tibble` function.
It also includes a second vignette showing a detailed package use case.
This is an resubmission of the `nomisr` package, version number 0.4.5. It
features improved error messages and makes output less verbose when reading
CSV files.


## Test environments
* local macOS install, R 4.0.3
* local macOS install, R 4.1.1
* win-builder (devel and release)
* Windows Server 2019, release (on GitHub Actions)
* macOS Catalina 10.15, release (on GitHub Actions)
Expand Down
48 changes: 18 additions & 30 deletions vignettes/Introduction-to-work-and-health-nomis-indicators.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ The nomisr package is an easy way to obtain data from Nomis. In this introductio
The first step is to understanding what datasets are available in `nomisr` and this can be done using the `nomis_get_info()` function. If no particular id is inputted inside the brackets, the command will return all available datasets.

```{r packages, echo=FALSE}
library(dplyr)
library(nomisr)
```

```{r all_datasets, echo=TRUE}
nomis_data <- nomis_data_info() %>%
select(id, description.value, name.value)
head(nomis_data)
```

This gives you a list of all available datasets in nomis. The list of ids/datasets used in this script are:
Expand All @@ -49,14 +45,11 @@ This gives you a list of all available datasets in nomis. The list of ids/datase
If you wanted to look at more information on a specific dataset, you can add this inside the bracket of the `nomis_get_info()` function or use the `nomis_get_metadata()` function. Both of these will provide you with the different options (concepts) available for your chosen dataset.

```{r APS_Dataset, echo=TRUE}
NM_17_5 <- nomis_data_info("NM_17_5")
tibble::glimpse(NM_17_5)
NM_17_5 <- nomis_get_metadata("NM_17_5")
head(NM_17_5)
```

# Querying data variables
Expand All @@ -68,10 +61,8 @@ As mentioned previously, the `nomis_get_info()` and `nomis_get_metadata()` funct
To explore the concepts we need to specify this in the code. For example to explore the dates available in the APS dataset, we need to specify the concept as "time".

```{r Exploring_Dates, echo=TRUE}
NM_17_5_DATES <- nomis_get_metadata("NM_17_5", concept = "time")
tail(NM_17_5_DATES)
```

Instead of referring to the ids for the specific dates you can also use the following options:
Expand All @@ -85,34 +76,27 @@ Instead of referring to the ids for the specific dates you can also use the foll
To explore the variables available in the APS dataset, we need to specify the concept as "variable".

```{r Exploring_Variables, echo=TRUE}
NM_17_5_VARIABLE <- nomis_get_metadata("NM_17_5", concept = "variable")
head(NM_17_5_VARIABLE)
```

To have a look at what geographies are available in the APS dataset, we need to specify the concept as "geography".

```{r Exploring_Geography, echo=TRUE}
NM_17_5_GEOG <- nomis_get_metadata("NM_17_5", concept = "geography")
head(NM_17_5_GEOG)
```

This gives the top level geographies available but if we wanted to identify what type of geographies are available, we would need to use 'type' in the code.

```{r Exploring_Geography_Type, echo=TRUE}
NM_17_5_GEOG <- nomis_get_metadata("NM_17_5", concept = "geography", "type")
head(NM_17_5_GEOG)
```

If you wanted all local authorities, combined authorities and regions in England, it is easier to create a vector which you can then feed into the `nomis_get_data()` function.

```{r Creating_Geography_Vector, echo=TRUE}
EN_LAs <- nomis_get_metadata(id = "NM_17_5", concept = "geography",
type = "TYPE464")[1:326, ] %>%
mutate(type = 464)
Expand All @@ -134,7 +118,6 @@ print(EN_geos)
# Save it as a vector so can plug it into nomis_get_data function
EN_geographies <- c(1946157057:1946157382, 1853882369:1853882372,
1853882374:1853882379, 2013265921:2013265929, 2092957699)
```

# Downloading data
Expand All @@ -148,13 +131,11 @@ Now you have enough information to use the `nomis_get_data()` function.
+ Step 5: select the measures of interest - in this example all measures available are pulled

```{r working_age_pop, echo=TRUE}
working_age_pop <- nomis_get_data(id = "NM_17_5", date = "latest",
geography = EN_geographies,
variable = 18,
measures = c(20599, 21001, 21002, 21003))
print(working_age_pop)
```

## Stacked bar chart showing labour market summary for Bolton
Expand Down Expand Up @@ -191,7 +172,6 @@ Stack 4: economically active, long term sick, temp sick, student, looking after
The data extracted is for all of the geographies outlined in the 'EN_geographies' vector, but the chart is filtered for Bolton only.

```{r Stacked_Bar, echo=TRUE}
library(ggplot2)
# FIRST STACK: Working age population
Expand Down Expand Up @@ -361,7 +341,6 @@ p <- ggplot(data, aes(fill=Key, y=OBS_VALUE, x=grouping)) +
theme(legend.position = "right")
p
```

## Pie charts showing economic activity in Bolton
Expand Down Expand Up @@ -504,7 +483,9 @@ pie2 = pie2 + coord_polar("y", start=0) + geom_text(aes(label = paste0(OBS_VALUE
#using phe colours for the slices
pie2 = pie2 + scale_fill_manual(values=c("#862633","#00AB8E","#01216A"))
pie2 = pie2 + labs(x = NULL, y = NULL, fill = NULL, title = (paste0("Participation in the labour market, ", area_of_interest_name, ", January 2019 to December 2019")), subtitle = "Working age population with conditions or illnesses lasting more than 12 months")
pie2 = pie2 + labs(x = NULL, y = NULL, fill = NULL,
title = (paste0("Participation in the labour market, ", area_of_interest_name, ", January 2019 to December 2019")),
subtitle = "Working age population with conditions or illnesses lasting more than 12 months")
# Tidy up the theme
Expand Down Expand Up @@ -547,7 +528,8 @@ ESA_data_condition[is.na(ESA_data_condition)] <- 0
ESA_data_condition <- ESA_data_condition %>%
group_by(GEOGRAPHY_CODE, ICDGP_CONDITION) %>%
mutate(total = sum(OBS_VALUE)) %>%
select(DATE_NAME, GEOGRAPHY: GEOGRAPHY_CODE, ICDGP_CONDITION: ICDGP_CONDITION_NAME, SEX_NAME, total) %>%
select(DATE_NAME, GEOGRAPHY: GEOGRAPHY_CODE,
ICDGP_CONDITION: ICDGP_CONDITION_NAME, SEX_NAME, total) %>%
distinct(total, .keep_all = TRUE)
# Total for each area
Expand All @@ -558,7 +540,8 @@ ESA_total <- ESA_data_condition %>%
distinct(tot, .keep_all = TRUE)
# Link the two tables together to calculate the percentage contributed
ESA_total_final <- left_join(ESA_data_condition, ESA_total, by = c("GEOGRAPHY_CODE", "GEOGRAPHY_NAME")) %>%
ESA_total_final <- left_join(ESA_data_condition, ESA_total,
by = c("GEOGRAPHY_CODE", "GEOGRAPHY_NAME")) %>%
select(DATE_NAME: ICDGP_CONDITION_NAME, SEX_NAME, total, tot) %>%
mutate(PERC = total / tot *100,
PERC = round(PERC, 1)) %>%
Expand All @@ -572,17 +555,21 @@ ESA_condition_rank <- ESA_total_final %>%
arrange(GEOGRAPHY_CODE, desc(PERC)) %>%
group_by(GEOGRAPHY_CODE) %>%
mutate(rank = 1:n()) %>%
select(GEOGRAPHY_CODE, GEOGRAPHY_NAME, ICDGP_CONDITION: ICDGP_CONDITION_NAME, rank)
select(GEOGRAPHY_CODE, GEOGRAPHY_NAME,
ICDGP_CONDITION: ICDGP_CONDITION_NAME, rank)
print(ESA_condition_rank)
# Step 2: Extract conditions to map on to treemap and create appropriate age-groups
# get ESA data by condition for latest data
# Don't need the total (ID=0), factors (ID=21), Codes for special purposes (ID=22), Claimants without any diagnosis (ID=23)
treemap_data <- nomis_get_data(id = "NM_134_1", date = "latest", geography = EN_geographies, icdgp_condition = c(1:19),
sex = 7, esa_phase = 0, payment_type = 0, ethnic_group = 0, age = c(1:8),
duration = 0, measures = 20100) %>%
select(DATE_NAME, GEOGRAPHY: GEOGRAPHY_CODE, ICDGP_CONDITION: ICDGP_CONDITION_NAME, SEX_NAME, AGE:AGE_NAME, OBS_VALUE)
treemap_data <- nomis_get_data(
id = "NM_134_1", date = "latest", geography = EN_geographies,
icdgp_condition = c(1:19), sex = 7, esa_phase = 0, payment_type = 0,
ethnic_group = 0, age = c(1:8), duration = 0, measures = 20100) %>%
select(
DATE_NAME, GEOGRAPHY: GEOGRAPHY_CODE, ICDGP_CONDITION: ICDGP_CONDITION_NAME,
SEX_NAME, AGE:AGE_NAME, OBS_VALUE)
# replace na with 0
Expand All @@ -607,7 +594,8 @@ treemap_data_final <- agegrp1 %>%
arrange(GEOGRAPHY, ICDGP_CONDITION_NAME, NEW_AGE) %>%
group_by(GEOGRAPHY, ICDGP_CONDITION_NAME, NEW_AGE) %>%
mutate(OBS = sum(OBS_VALUE)) %>%
select(DATE_NAME, GEOGRAPHY: GEOGRAPHY_CODE, ICDGP_CONDITION: ICDGP_CONDITION_NAME, SEX_NAME, NEW_AGE, OBS) %>%
select(DATE_NAME, GEOGRAPHY: GEOGRAPHY_CODE,
ICDGP_CONDITION: ICDGP_CONDITION_NAME, SEX_NAME, NEW_AGE, OBS) %>%
distinct(GEOGRAPHY, ICDGP_CONDITION_NAME, NEW_AGE, .keep_all = TRUE)
print(treemap_data_final)
Expand Down
22 changes: 6 additions & 16 deletions vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,23 @@ a

If provided with a concept name it returns the available values for that concept. However, in some cases, espescially with the geography concept, there are multiple options available, which Nomis labels types. In that case `nomis_get_metadata()` returns the values of the lowest indexed type available.

```{r concepts, echo=TRUE}
```
b <- nomis_get_metadata(id = "NM_893_1", concept = "GEOGRAPHY")
b
```

We can now pass a generic "type" string to the `type` parameter in `nomis_get_metadata()`, which returns all available geography types for dataset "NM_893_1".


```{r concept-types, echo=TRUE}
```
c <- nomis_get_metadata(id = "NM_893_1", concept = "geography", type = "type")
c
```


Passing a specific type to the `type` parameter, in this case "TYPE460" for all post-2010 parliamentary constituencies, returns a tibble with geographic codes for those specific constituencies, which can be used to filter queries.

```{r concept-type460, echo=TRUE}
```
d <- nomis_get_metadata(id = "NM_893_1", concept = "geography", type = "TYPE460")
d
```


Expand All @@ -159,20 +154,15 @@ The vast majority (98% as of February 2018) of Nomis datasets include a geograph

Using the information above, we can now query the latest data on bedroom occupancy per household type in different NHS clinical commissioning groups.

```{r download-data, echo=TRUE}
```
z <- nomis_get_data(id = "NM_893_1", time = "latest", geography = "TYPE266")
z
```


We can also query bedroom occupancy per household type in the Manchester, Gorton and Manchester, Withington parliamentary constituencies.

```{r download-specific-area, echo=TRUE}
```
x <- nomis_get_data(id = "NM_893_1", time = "latest",
geography = c("1929380119", "1929380120"))
x
```


Expand All @@ -182,7 +172,7 @@ x
```{r jsa-claimaints}
library(ggplot2)
jsa <- nomis_get_data(id = "NM_1_1", time = "2015-01-2020-01",
jsa <- nomis_get_data(id = "NM_1_1", time = "2018-01-2021-10",
geography = "TYPE480", measures=20201,
sex=c(5,6), item = 1, tidy = TRUE)
Expand Down

0 comments on commit f35a0e5

Please sign in to comment.