Skip to content

Commit 820e7c8

Browse files
committed
vignettes with eval = !is.null(df)
1 parent 903b412 commit 820e7c8

File tree

4 files changed

+19
-144
lines changed

4 files changed

+19
-144
lines changed

r-package/vignettes/access_inequality.Rmd

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ df <- aopdata::read_access(
4646

4747
## Inequality in access to job opportunities by income decile
4848

49-
```{r, message = FALSE, eval = FALSE}
49+
```{r, message = FALSE, eval = !is.null(df)}
5050
ggplot() +
5151
geom_boxplot(data=subset(df, !is.na(R003)),
5252
aes(x = factor(R003), y=CMATT60/1000, color=factor(R003))) +
@@ -57,23 +57,11 @@ ggplot() +
5757
scale_x_discrete(labels=c("D1 Poorest", paste0('D', 2:9), "D10 Wealthiest")) +
5858
theme_minimal()
5959
```
60-
```{r, message = FALSE, echo = FALSE}
61-
if (!is.null(df)) {
62-
ggplot() +
63-
geom_boxplot(data=subset(df, !is.na(R003)),
64-
aes(x = factor(R003), y=CMATT60/1000, color=factor(R003))) +
65-
scale_color_brewer(palette = 'RdBu') +
66-
labs(title='Distribution of the number of jobs accessible', color="Income\ndecile",
67-
subtitle='by public transport in less than 60 min. by income decile',
68-
x='Income decile', y="N. of jobs accessible\n(thousands)") +
69-
scale_x_discrete(labels=c("D1 Poorest", paste0('D', 2:9), "D10 Wealthiest")) +
70-
theme_minimal()
71-
}
72-
```
60+
7361

7462
## Palma ratio
7563

76-
```{r, message = FALSE, eval = FALSE}
64+
```{r, message = FALSE, eval = !is.null(df)}
7765
# average access of the wealthiest 10%
7866
avg_access_10p_wealthiest <- df[ R003==10, weighted.mean(x=CMATT60, w=P001, na.rm=T)]
7967
@@ -84,19 +72,7 @@ avg_access_40p_poorest <- df[ R003<=4, weighted.mean(x=CMATT60, w=P001, na.rm=T)
8472
palma_ratio <- avg_access_10p_wealthiest / avg_access_40p_poorest
8573
palma_ratio
8674
```
87-
```{r, message = FALSE, echo = FALSE}
88-
if (!is.null(df)) {
89-
# average access of the wealthiest 10%
90-
avg_access_10p_wealthiest <- df[ R003==10, weighted.mean(x=CMATT60, w=P001, na.rm=T)]
91-
92-
# average access of the poorest 40%
93-
avg_access_40p_poorest <- df[ R003<=4, weighted.mean(x=CMATT60, w=P001, na.rm=T)]
94-
95-
# Palma ratio
96-
palma_ratio <- avg_access_10p_wealthiest / avg_access_40p_poorest
97-
palma_ratio
98-
}
99-
```
75+
10076
This means that the 10% wealthiest population could access by public transport
10177
on average 2.6 times more job opportunites than the 40%
10278
poorest people in less than 60 min.
@@ -105,7 +81,7 @@ poorest people in less than 60 min.
10581

10682
## Inequality in travel time to closes hospital
10783

108-
```{r, message = FALSE, eval = FALSE}
84+
```{r, message = FALSE, eval = !is.null(df)}
10985
# replace Inf travel time with 120
11086
df[, TMISA := fifelse(TMISA==Inf, 120, TMISA)]
11187
@@ -124,23 +100,3 @@ ggplot() +
124100
theme_minimal()
125101
```
126102

127-
```{r, message = FALSE, echo = FALSE}
128-
if (!is.null(df)) {
129-
# replace Inf travel time with 120
130-
df[, TMISA := fifelse(TMISA==Inf, 120, TMISA)]
131-
132-
# calculate average travel time by race
133-
df[, .(average = weighted.mean(x=TMISA, w=P001, na.rm=T),
134-
white = weighted.mean(x=TMISA, w=P002, na.rm=T),
135-
black = weighted.mean(x=TMISA, w=P003, na.rm=T))]
136-
137-
# calculate average travel time by income
138-
temp <- df[, .(average = weighted.mean(x=TMISA, w=P001, na.rm=T)), by=R003]
139-
temp <- na.omit(temp)
140-
141-
ggplot() +
142-
geom_point(data=temp, aes(y=average, x=factor(R003))) +
143-
labs(x='Income decile', y='Avg. travel time to\nclosest hospital') +
144-
theme_minimal()
145-
}
146-
```

r-package/vignettes/access_maps.Rmd

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ knitr::opts_chunk$set(
2020
```
2121

2222

23-
2423
**Load libraries**
2524

2625
```{r, message = FALSE, eval = TRUE, warning=FALSE}
@@ -46,42 +45,24 @@ df <- aopdata::read_access(
4645
```
4746

4847
## Map access to job opportunities
49-
```{r, message = FALSE, eval = FALSE}
48+
```{r, message = FALSE, eval = !is.null(df)}
5049
ggplot() +
5150
geom_sf(data=df, aes(fill=CMATT60), color=NA, alpha=.9) +
5251
scale_fill_viridis_c(option = "inferno", labels = scales::comma) +
5352
labs(title='Number of jobs accessible', fill="Accessibility",
5453
subtitle='by public transport in less than 60 min.') +
5554
theme_void()
5655
```
57-
```{r, message = FALSE, echo = FALSE}
58-
if (!is.null(df)) {
59-
ggplot() +
60-
geom_sf(data=df, aes(fill=CMATT60), color=NA, alpha=.9) +
61-
scale_fill_viridis_c(option = "inferno", labels = scales::comma) +
62-
labs(title='Number of jobs accessible', fill="Accessibility",
63-
subtitle='by public transport in less than 60 min.') +
64-
theme_void()
65-
}
66-
```
56+
6757

6858
## Map access to schools
6959

70-
```{r, message = FALSE, eval = FALSE}
60+
```{r, message = FALSE, eval = !is.null(df)}
7161
ggplot() +
7262
geom_sf(data=df, aes(fill=CMAET30), color=NA, alpha=.9) +
7363
scale_fill_viridis_c(option = "cividis", labels=scales::comma) +
7464
labs(title='Number of schools accessible', fill="Accessibility",
7565
subtitle='by public transport in less than 30 min.', fill="N. of schools") +
7666
theme_void()
7767
```
78-
```{r, message = FALSE, echo = FALSE}
79-
if (!is.null(df)) {
80-
ggplot() +
81-
geom_sf(data=df, aes(fill=CMAET30), color=NA, alpha=.9) +
82-
scale_fill_viridis_c(option = "cividis", labels=scales::comma) +
83-
labs(title='Number of schools accessible', fill="Accessibility",
84-
subtitle='by public transport in less than 30 min.', fill="N. of schools") +
85-
theme_void()
86-
}
87-
```
68+

r-package/vignettes/landuse_maps.Rmd

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ df <- aopdata::read_landuse(
4545
## Spatial distribution of jobs
4646

4747

48-
```{r, message = FALSE, eval = TRUE}
48+
```{r, message = FALSE, eval = !is.null(df)}
4949
ggplot() +
5050
geom_sf(data=df, aes(fill=T001), color=NA, alpha=.9) +
5151
scale_fill_distiller(palette = "YlOrRd", direction = 1) +
@@ -54,76 +54,41 @@ ggplot() +
5454
5555
```
5656

57-
```{r, message = FALSE, echo = TRUE}
58-
if (!is.null(df)) {
59-
ggplot() +
60-
geom_sf(data=df, aes(fill=T001), color=NA, alpha=.9) +
61-
scale_fill_distiller(palette = "YlOrRd", direction = 1) +
62-
labs(title='Spatial distribution of jobs', fill="N. of jobs") +
63-
theme_void()
64-
}
65-
```
6657

6758

6859
## Spatial distribution of schools
6960

7061
In this case below, elementary schools with the column`E003`.
7162

72-
```{r, message = FALSE, eval = FALSE}
63+
```{r, message = FALSE, eval = !is.null(df)}
7364
ggplot() +
7465
geom_sf(data=df, aes(fill=factor(E003)), color=NA, alpha=.9) +
7566
scale_fill_brewer(palette = "PuBuGn", direction = 1) +
7667
labs(title='Spatial distribution of elementary schools', fill="N. of schools") +
7768
theme_void()
7869
7970
```
80-
```{r, message = FALSE, echo = FALSE}
81-
if (!is.null(df)) {
82-
ggplot() +
83-
geom_sf(data=df, aes(fill=factor(E003)), color=NA, alpha=.9) +
84-
scale_fill_brewer(palette = "PuBuGn", direction = 1) +
85-
labs(title='Spatial distribution of elementary schools', fill="N. of schools") +
86-
theme_void()
87-
}
88-
```
8971

9072
## Spatial distribution of healthcare
9173

9274
In this example, we mape high-complexity health care facilities (column `S004`).
9375

94-
```{r, message = FALSE, eval = FALSE}
76+
```{r, message = FALSE, eval = !is.null(df)}
9577
ggplot() +
9678
geom_sf(data=df, aes(fill=factor(S004)), color=NA, alpha=.9) +
9779
scale_fill_brewer(palette = "YlGnBu", direction = 1)+
9880
labs(title='Spatial distribution of hospitals', fill="N. of hospitals") +
9981
theme_void()
10082
```
10183

102-
```{r, message = FALSE, echo = FALSE}
103-
if (!is.null(df)) {
104-
ggplot() +
105-
geom_sf(data=df, aes(fill=factor(S004)), color=NA, alpha=.9) +
106-
scale_fill_brewer(palette = "YlGnBu", direction = 1)+
107-
labs(title='Spatial distribution of hospitals', fill="N. of hospitals") +
108-
theme_void()
109-
}
110-
```
11184

11285
## Map Centers for social assistance (CRAS)
11386

114-
```{r, message = FALSE, eval = FALSE}
87+
```{r, message = FALSE, eval = !is.null(df)}
11588
ggplot() +
11689
geom_sf(data=df, aes(fill=factor(C001)), color=NA, alpha=.9) +
11790
scale_fill_brewer(palette = "RdPu", direction = 1)+
11891
labs(title='Spatial distribution of CRAS facilities', fill="N. of CRAS") +
11992
theme_void()
12093
```
121-
```{r, message = FALSE, echo = FALSE}
122-
if (!is.null(df)) {
123-
ggplot() +
124-
geom_sf(data=df, aes(fill=factor(C001)), color=NA, alpha=.9) +
125-
scale_fill_brewer(palette = "RdPu", direction = 1)+
126-
labs(title='Spatial distribution of CRAS facilities', fill="N. of CRAS") +
127-
theme_void()
128-
}
129-
```
94+

r-package/vignettes/population_maps.Rmd

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,21 @@ df <- aopdata::read_population(
4646

4747
## Map total population
4848

49-
```{r, message = FALSE, eval=FALSE}
49+
```{r, message = FALSE, eval=!is.null(df)}
5050
ggplot() +
5151
geom_sf(data=subset(df, P001>0), aes(fill=P001), color=NA, alpha=.8) +
5252
scale_fill_distiller(palette = "YlOrRd", direction = 1)+
5353
labs(title='Population distribution', fill="Total population") +
5454
theme_void()
5555
5656
```
57-
```{r, message = FALSE, echo=FALSE}
58-
if (!is.null(df)) {
59-
ggplot() +
60-
geom_sf(data=subset(df, P001>0), aes(fill=P001), color=NA, alpha=.8) +
61-
scale_fill_distiller(palette = "YlOrRd", direction = 1)+
62-
labs(title='Population distribution', fill="Total population") +
63-
theme_void()
64-
}
65-
```
6657

6758

6859
## Map population by income levels
6960

7061
Here, we map the spatial distribution population by income decile (column `R003`).
7162

72-
```{r, eval = FALSE}
63+
```{r, eval = !is.null(df)}
7364
ggplot() +
7465
geom_sf(data=subset(df, !is.na(R002)), aes(fill=factor(R003)), color=NA, alpha=.8) +
7566
scale_fill_brewer(palette = "RdBu") +
@@ -78,22 +69,13 @@ ggplot() +
7869
7970
```
8071

81-
```{r, message=F, echo=FALSE}
82-
if (!is.null(df)) {
83-
ggplot() +
84-
geom_sf(data=subset(df, !is.na(R002)), aes(fill=factor(R003)), color=NA, alpha=.8) +
85-
scale_fill_brewer(palette = "RdBu") +
86-
labs(title='Average household income per capita', fill="Income decile") +
87-
theme_void()
88-
}
89-
```
9072

9173

9274
## Map population by race
9375

9476
Here, we map the spatial distribution of the black population.
9577

96-
```{r, message = FALSE, eval=FALSE}
78+
```{r, message = FALSE, eval=!is.null(df)}
9779
df$prop_black <- df$P003 / df$P001
9880
9981
ggplot() +
@@ -103,14 +85,5 @@ ggplot() +
10385
theme_void()
10486
10587
```
106-
```{r, message = FALSE, echo=FALSE}
107-
if (!is.null(df)) {
108-
df$prop_black <- df$P003 / df$P001
109-
110-
ggplot() +
111-
geom_sf(data=subset(df, P001 >0), aes(fill=prop_black), color=NA, alpha=.8) +
112-
scale_fill_distiller(palette = "RdPu", direction = 1, labels = percent)+
113-
labs(title='Proportion of black population', fill="Black population") +
114-
theme_void()
115-
}
116-
```
88+
89+

0 commit comments

Comments
 (0)