################################### # OTAGO SUMMER SCHOOL TURTORIAL 1 # ################################### #install and load needed packages install.packages("sf") install.packages("dplyr") install.packages("ggplot2") install.packages("tidyverse") install.packages("tmap", repos = c("https://r-tmap.r-universe.dev", "https://cloud.r-project.org")) library(sf) library(dplyr) library(ggplot2) library(tmap) library(mapview) library(tidyverse) #### Tasks and goals #### # # # load in spatial data # # explore attributes # # print map # # # ######################### ## It is nice practice to set up a working directory where your data is stored/will output ## note: use "/" instead of "\", or "\\" wd <- "CHANGE TO YOUR DIRECTORY [FILE WITH TUTORIAL DATA]" # working directory setwd(wd) # set the working directory getwd() # to get your working directory details ## Now bring in your data auckland_cau <- st_read("CHANGE HERE TO NAME OF SHP") #since your working directory was set you just need your shp file View(auckland_cau) # will open dataset ## identify what cau is the largest summary(auckland_cau) #first review the summary statistics for the whole dataset largest_cau <- auckland_cau[which.max(auckland_cau$AreaKM), ] print(largest_cau) ## Now make a printable map map_auckland <- st_transform(auckland_cau, 2193) map_auckland <- tm_shape(map_auckland) + tm_polygons() class(map_auckland) map_auckland_cau <- map_auckland + tm_compass(type = "arrow", position = c("bottom", "right")) + # North arrow tm_scalebar(breaks = c(0, 2.5, 5), position = c("bottom", "left"))+ # scale bar tm_polygons (col = "AreaKM", palette = "Greens", style = "quantile") + #makes chloropleth map where darker green indicates larger CAU [can try out different colors or styles] tm_title("Auckland Census Area Units") print(map_auckland_cau) tmap_save(tm = map_auckland_cau, filename = "AucklandCAU.tiff") ##export map as tiff ################################### # OTAGO SUMMER SCHOOL TURTORIAL 2 # ################################### ######## Tasks and goals ######## # # # geocode stroke address file # # # ################################## # Install needed library for geocoding if("ggmap" %in% installed.packages()==FALSE){install.packages(pkgs=c("ggmap"), repos = "http://cran.us.r-project.org")} library(ggmap) # see help file on ggmap mykey <- "GOOGLE API GOES HERE" ##add your api, free up to 10,000 register_google(key = mykey) # Load stroke data strokes <- read.csv("CHANGE NAME HERE TO CSV FILE") #if set working directory, just put the name of the file # Create list of text addresses stroke_address_list <- with(strokes, paste(Street, Dependent_Locality, Locality, Country, sep = ", ")) # Check the address_list to make sure they look correct print(stroke_address_list[1:5]) print(length(stroke_address_list)) # Does this match the number of rows in strokes? n <- length(stroke_address_list) print(stroke_address_list[(n-5):n]) # Geocode! This can take time to run on larger lists. It also costs money (if doing more than 10,000 addresses)! # Be positive that stroke_address_list is correct and what you want to geocode before starting long_lat <- geocode(stroke_address_list, output='more') print(long_lat[0:5,]) # How does it look? Did addresses geocode to rooftop? # Now plot them to see the distribution across the city strokes_geocoded <- st_as_sf(long_lat, coords = c("lon", "lat"), crs = 2193) ################################### # OTAGO SUMMER SCHOOL BONUS #1 # ################################### ######## Tasks and goals ######## # # # download spatial data # # append tabular data # # # ################################## ## Now bring in the data you just downloaded from NZ Stats webpage [https://datafinder.stats.govt.nz/] nz_stat_areas <- st_read("CHANGE HERE TO NAME OF SHP") #since your working directory was set you just need your shp file View(nz_stat_areas) # will open dataset names(nz_stat_areas) str(nz_stat_areas) #check data type # Load text data that was in folder asm_rate_bySA2 <- read.csv("CHANGE NAME HERE TO CSV FILE") #if set working directory, just put the name of the file names(asm_rate_bySA2) str(asm_rate_bySA2) #check data type asm_rate_bySA2$SA22018_V1 <- as.character(asm_rate_bySA2$SA22018_V1) #need field data type to match to be able to join ##join the 2 files together asm_and_sa2 <- nz_stat_areas %>% left_join (asm_rate_bySA2, by = "SA22018_V1") ## Now make a map asm_and_sa2_map <- st_transform(asm_and_sa2, 2193) asm_and_sa2_map <- tm_shape(asm_and_sa2_map) + tm_polygons() class(asm_and_sa2_map) asm_and_sa2_map <- asm_and_sa2_map + tm_compass(type = "arrow", position = c("bottom", "right")) + # North arrow tm_scalebar(breaks = c(0, 250, 500), position = c("bottom", "right"))+ # scale bar tm_polygons (fill = "ASM_Rate", col = "grey", fill.scale = tm_scale_intervals(style = "quantile", values = "brewer.blues")) + #makes chloropleth map tm_title("New Zealand ASM Rate") print(asm_and_sa2_map) tmap_save(tm = asm_and_sa2_map, filename = "asm_and_sa2_map.tiff") ##export map as tiff