Skip to content

Commit

Permalink
New parameter peak added to read_access() function. Closes #17.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapereirabr committed Mar 1, 2021
1 parent 9aa496d commit b23d8f8
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 11 deletions.
6 changes: 5 additions & 1 deletion r-package/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

# aopdata v0.1.0 (dev)


**Major changes**
* New parameter `peak` added to `read_access()` function. Closes #17.

**Minor changes**
* Downloads two or more cities at the same time. Closes #3
* Downloads two or more cities at the same time. Closes #3.



Expand Down
28 changes: 20 additions & 8 deletions r-package/R/read_access.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
#' `city="all"`, results for all cities are loaded.
#' @param mode Character. A transport mode. Modes available include
#' 'public_transport', 'bicycle', or 'walk' (the default).
#' @param peak Logical. If `TRUE` (the default), returns accessibility estimates
#' during peak time, between 6am and 8am. If `FALSE`, returns
#' accessibility during off-peak, between 2pm and 4am. This argument
#' only takes effect when `mode = public_transport`.
#' @param year Numeric. A year number in YYYY format. Default set to 2019, the
#' only year currently available.
#' @param geometry Logical. if FALSE (the default), returns a regular data.table
#' of aop data. if TRUE, returns a an `sf data.frame` with simple
#' @param geometry Logical. If `FALSE` (the default), returns a regular data.table
#' of aop data. If `TRUE`, returns a an `sf data.frame` with simple
#' feature geometry of spatial hexagonal grid H3. See details in
#' \link{read_grid}.
#' @param showProgress Logical. Defaults to `TRUE` display progress bar
Expand All @@ -31,7 +35,7 @@
#' all <- read_access(city = 'all', mode = 'public_transport', year = 2019)

#'}
read_access <- function(city, mode = 'walk', year = 2019, geometry = FALSE, showProgress = TRUE){
read_access <- function(city, mode = 'walk', peak = TRUE, year = 2019, geometry = FALSE, showProgress = TRUE){

# Get metadata with data url addresses
temp_meta <- select_metadata(t='access',
Expand All @@ -45,15 +49,23 @@ read_access <- function(city, mode = 'walk', year = 2019, geometry = FALSE, show
# download files
aop_df <- download_data(file_url, progress_bar = showProgress)

# without spatial data, return df
# peak Vs off-peak
if(peak==TRUE){
aop_df <- subset(aop_df, peak == 1)
} else {
aop_df <- subset(aop_df, peak == 0)
}

# with Vs without spatial data
if(geometry == FALSE){
# return df
return(aop_df)
}

# with spatial data, return sf
if(geometry == TRUE){
} else {

# return sf
aop_sf <- read_grid(city=city, showProgress=showProgress)
aop <- aop_spatial_join(aop_df, aop_sf)
return(aop)
}
}
}
10 changes: 8 additions & 2 deletions r-package/man/read_access.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions r-package/prep_data/vignette_map.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

library(aopdata)
library(sf)
library(ggplot2)
library(data.table)

# download aop data
df <- aop::read_access(city='bho',
mode='public_transport',
year=2019,
geometry = T)

access_peak <- subset(df, peak==1)

# plot map accessibility
ggplot() +
geom_sf(data=df, aes(fill=CMATT60), color=NA, alpha=.9) +
scale_fill_viridis_c(option = "inferno", labels=scales::percent) +
labs(title='Proportion of jobs accessible', fill="Accessibility",
subtitle='by public transport in less than 60 min.') +
theme_void()


# plot map Schools
ggplot() +
geom_sf(data=df, aes(fill=S001), color=NA, alpha=.7) +
scale_fill_viridis_c(option = "inferno") +
labs(title='Spatial distribution of public schools', fill="N. of schools") +
theme_void()

ggplot() +
geom_sf(data=df, aes(fill=df$S001), color=NA, alpha=.7) +
scale_fill_viridis_c(option = "inferno") +
labs(title='Spatial distribution of public schools', fill="N. of schools") +
theme_void()



# Download
df2 <- aop::read_access(city='bho',
mode='public_transport',
year=2019)


ggplot() +
geom_boxplot(data=subset(df2, !is.na(R003)),
aes(x = factor(R003), y=CMAET30, color=factor(R003))) +
scale_color_brewer(palette = 'RdBu') +
labs(title='Distribution of the proportion of jobs accessible', color="Income\ndecile",
subtitle='by public transport in less than 30 min. by income decile',
x='Income decile', y="Accessibility") +
scale_x_discrete(labels=c("D1 Poorest", paste0('D', 2:9), "D10 Wealthiest")) +
scale_y_continuous(labels = scales::percent) +
theme_minimal()

weight=P001
summary(df2$P001)







library(aopdata)
library(data.table)

df <- aop::read_access(city='all', mode='walk', year=2019)

df[, round(weighted.mean(x = CMATT30, w=P001),3), by=name_muni]


0 comments on commit b23d8f8

Please sign in to comment.