This file is a template for R Markdown and includes some resources mentioned in week 2.
st_join
) and
contains (st_contain
). st_intersection
also
returns the spatial subset of geometries which are in both x and y.Leaflet map for Part 2
#note that this chunk has eval=FALSE meaning it will not be evaluated
#create your palette (e.g. how you want to fill in the domain/target variable for mapping)
pal1 <-
colorNumeric(
palette = "Oranges",
domain = tibble$targ_column_name,
na.color = "Grey"
)
#create your labels
#update the label_template as appropriate, Mbr> is a linebreak tag, <strong> is a bold tag
label_template <- str_glue("<strong>NAME %s</strong><br>TARG COLUMN (Pct): %s<br/>")
labels <- sprintf(
label_template,
tibble$NAME, #the first '%s'
percent(tibble$targ_column_name, accuracy = .1) #the second '%s'
) %>%
lapply(htmltools::HTML)
your_map <- leaflet() %>%
addTiles() %>%
addPolygons(
data = tibble, #update this with your tibble name
fillColor = ~ pal1(targ_column_name), #update this with the name of your target column
weight = 0.5,
opacity = 0.5,
color = "white",
dashArray = 3,
fillOpacity = 0.7
highlight = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE
),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto"
)
) %>%
addLegend(
pal = pal1, #the name of your palette
values = tibble$targ_column_name, #the name of your target variable
opacity = 0.7,
title = NULL,
position = "bottomright"
)
Calculating Distance for Part 3
12.4.7 calculates distance matrices, e.g. the distance between two
sets of points. In this case, the example distance of a ride is between
two points, ride start and ride end. Since sf
objects have
one geometry this doesn’t fit nicely into the patterns we saw in class.
Here’s a few steps breaking down the problem. Try to see if you can
complete the problem without looking at all the steps.
Modifying divvy
to have two points per row
This is a bit of a weird pattern. We want to apply functions on the
rows of the tibble not of the column. In other languages we might use
apply
but in R it is often easier to use
rowwise
which groups a tibble by its rows so every mutation
to the tibble is then applied to one row at a time.
if(str_detect(getwd(), 'static')){
file_loc <- '../data/'
} else {
file_loc <- 'static/data/'
}
divvy_example <- read_csv(str_glue(file_loc,
'week2_divvy_10rows.csv'))
divvy_rowwise <- divvy_example %>% select(contains('lat'), contains('lng')) %>%
rowwise()
divvy_rowwise %>% glimpse()
## Rows: 10
## Columns: 4
## Rowwise:
## $ start_lat <dbl> 41.86127, 41.78000, 41.89604, 41.89675, 41.89411, 41.95000, …
## $ end_lat <dbl> 41.88213, 41.80000, 41.90040, 41.91808, 41.89387, 41.95000, …
## $ start_lng <dbl> -87.65663, -87.60000, -87.66768, -87.63567, -87.62951, -87.7…
## $ end_lng <dbl> -87.62512, -87.59000, -87.69668, -87.64375, -87.62947, -87.7…
Then we need to create a simple feature geometry list column e.g. a
column which is start_point
and end_point
which stores the lat/lon information as an sf
object.
The functions you will need include st_point
and
st_sfc
which takes a simple feature geometry (e.g. the
output of st_point
) and also has argument crs
which should be 4326.
Finally, you can use st_distance
to calculate the
distance between start_point
and end_point