Created
May 20, 2019 11:47
-
-
Save cimentadaj/563c8f86810a2bc71862ae9595b25ec1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Slides: https://github.com/ITSLeeds/TDS/blob/master/slides/madrid-slides.Rmd | |
# Exercises http://git.io/tds2dayex | |
library(sf) | |
library(dplyr) | |
library(stplanr) # geographic transport data package | |
library(tmap) | |
library(osmdata) | |
library(tidyverse) | |
library(stats19) | |
library(pct) | |
u = "https://www.openstreetmap.org/trace/2992569/data" | |
download.file(u, "track.gpx") | |
tracks <- sf::read_sf("track.gpx", layer = 'track_points') | |
mapview::mapview(tracks) | |
library(stplanr) | |
f <- flow[1:3, 1:5] | |
f_tidy <- flow %>% select(1:5) %>% slice(1:3) %>% as_tibble() | |
f_tidy | |
z <- zones_sf | |
l <- od2line(f, zones_sf) | |
plot(l) | |
line2route(l[2, ]) | |
od <- pct::get_od() | |
iow <- pct::get_pct_centroids("isle-of-wight") | |
plot(iow) | |
mapview::mapview(iow) | |
eyes = c(2.3,4,3.7,4) | |
eyes = matrix(eyes, ncol = 2, byrow = T) | |
mouth = c(2,2,2.5,1.3,3,1,3.5,1.3,4,2) | |
mouth = matrix(mouth, ncol = 2, byrow = T) | |
plot(eyes, type = "p", main = "Smile you're using R", | |
cex = 2, xlim = c(0,5), ylim = c(0,5)) | |
lines(mouth, type = "l", col = "red") | |
library(sf) | |
iow <- pct::get_pct_zones('isle-of-wight') | |
sel = iow$all > 3000 # create a subsetting object | |
iow_large = iow[sel, ] # subset areas with a popualtion over 100,000 | |
iow_2 = iow[iow$geo_name == "Isle of Wight 002",] # subset based on 'equality' query | |
five_in_name = iow[grepl(pattern = "5", x = iow$geo_name), ] # subset based on text string match | |
iow_first_and_third_column = iow[c(1, 3)] | |
iow_just_all = iow["all"] | |
file.edit("attribute-operations.R") | |
# Solutions | |
iow_small <- | |
iow %>% | |
filter(all < 3000) | |
iow_small <- | |
iow_small %>% | |
mutate(sel_high_car = car_driver > median(car_driver)) | |
iow_small %>% | |
count(grepl("1", geo_name)) | |
iow_small %>% | |
select(car_driver) %>% | |
plot() | |
iow_small %>% | |
mutate(pop_density = as.numeric(st_area(select(iow_small, all)))) %>% | |
select(pop_density) %>% | |
plot() | |
iow_small %>% | |
mutate(perc_bike = (bicycle / all)) %>% | |
plot() | |
iow_small %>% | |
filter(foot > 500) %>% | |
mutate(prop_car = car_driver / all) %>% | |
select(prop_car) %>% | |
plot() | |
st_crs(iow) | |
iow_projected = st_transform(iow, 27700) # transfrom to projected | |
st_crs(iow_projected) | |
iow_lanlot <- st_transform(iow, 4326) | |
st_crs(iow_lanlot) | |
iow_cents <- pct::get_pct_centroids(region = "isle-of-wight", geography = "lsoa") | |
iow_cents2 <- iow_cents[iow_2, ] | |
plot(iow$geometry) | |
plot(iow_cents, col = "black", add = TRUE) | |
plot(iow_cents2, col = "red", add = TRUE) | |
st_difference(iow_cents, iow_large) %>% plot() | |
iow_cents[iow_2, , op = st_disjoint] %>% plot() | |
library(spData) | |
plot() | |
plot(nz$geom) | |
plot(nz_height['elevation'], add = TRUE) | |
nz_islands <- | |
nz %>% | |
group_by(Island) %>% | |
summarize(Population = sum(Population)) | |
plot(nz_islands) | |
canterbury <- | |
nz %>% | |
filter(Name == "Canterbury") | |
plot(nz$geom) | |
plot(nz_height[canterbury, 'elevation'], add = TRUE) | |
nz_height[canterbury, 'elevation'] %>% nrow() | |
sf::st_join(nz, nz_height) %>% | |
count(Name) %>% | |
arrange(desc(n)) | |
nz %>% | |
filter(Name == "West Coast") %>% | |
{plot(nz_height[., 'elevation'], add = TRUE)} | |
library(tmap) | |
iow %>% | |
select(all, bicycle) %>% | |
plot() | |
iow %>% | |
select(all, bicycle) %>% | |
tm_shape() + | |
tm_polygons(c('all', 'bicycle')) | |
europe <- | |
world %>% | |
filter(continent == "Europe", !is.na(iso_a2)) %>% | |
left_join(worldbank_df, by = "iso_a2") %>% | |
select(name, subregion, gdpPercap, HDI, pop_growth) %>% | |
st_transform("+proj=aea +lat_1=20 +lat_2=-23 +lat_0=0 +lon_0=25") | |
europe %>% | |
tm_shape() + | |
tm_polygons() | |
europe %>% | |
select(gdpPercap) %>% | |
plot() | |
europe %>% | |
mutate(gdp_groups = case_when(gdpPercap > 30000 ~ "High", | |
between(gdpPercap, 30000, 20000) ~ "Mid", | |
~ "Low")) | |
tm_shape() + | |
tm_polygons("gdpPercap") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment