library(tmaptools)
library(dplyr)
library(sf)
library(tmap)
library(lubridate)
library(deckgl)
library(htmlwidgets)
library(tidyr)
library(ggplot2)
library(dplyr)
library(tidyr)
library(magrittr)
library(lubridate)
library(stringr)
library(wesanderson)
library(ggpubr)
library(grid)
library(readxl)
library(readODS)
# library(png)
setwd("~/Documents/code/spatial-epi/staging-area")
# covid_bg <- readPNG("light-blue-coronavirus.png", native = TRUE)
c19 <- read_ods("cases.ods")
c19.all <- read.csv("https://raw.githubusercontent.com/UoA-eResearch/nz-covid19-data-auto/master/data.csv")
names(c19.all) <- c("Date", "Status", "Sex", "Age", "DHB", "Overseas", "Historical")
c19.all$Date <- as.Date(parse_date_time(c19.all$Date, "ymd"))
# c19.all %<>% select(-Case_number)
c19.community <- c19.all %>%
# select(-NA) %>%
filter(Overseas == "No") %>%
group_by(Date) %>%
summarise(Infected.in.NZ = n())
c19.overseas <- c19.all %>%
# select(-NA) %>%
filter(Overseas == "Yes") %>%
group_by(Date) %>%
summarise(Infected.overseas = n())
c19$Date <- as.Date(parse_date_time(c19$Date, "ymd"))
max.date <- max(c19$Date)
min.date <- min(c19$Date)
c19 <- c19 %>%
#  filter(Date >= min.date & Date <= max.date) %>%
left_join(c19.community) %>%
left_join(c19.overseas)
alert.levels <- c19 %>%
dplyr::select(Date, Level) %>%
pivot_longer(-Date) %>%
rename(variable = name) %>%
arrange(variable, Date) %>%
filter(Date > min.date, Date <= max.date)
# This code determines the total cases from the provided spreadsheet input
# by cumulative calculation, but leads to inconsistency with the reported daily numbers... ¯\_(ツ)_/¯
c19 %<>%
mutate(Total.overseas = cumsum(replace_na(Infected.overseas, 0)),
Total.in.NZ = cumsum(replace_na(Infected.in.NZ, 0)),
Total.cases =  Total.overseas + Total.in.NZ)
# ,
#          Active.cases = Total.cases - replace_na(Recovered, 0) - replace_na(Dead, 0))
cases <- c19 %>%
dplyr::select(Date, Total.cases, Active.cases, Total.in.NZ, Total.overseas) %>%
# select(Date, Active.cases) %>%
pivot_longer(-Date) %>%
rename(variable = name) %>%
arrange(variable, Date) %>%
filter(Date > min.date, Date <= max.date)
changes <- c19 %>%
mutate(Recovered = -New.recovered,
Dead = -New.dead) %>%
dplyr::select(Date, Infected.in.NZ, Infected.overseas, Recovered, Dead) %>%
pivot_longer(-Date) %>%
rename(variable = name) %>%
arrange(variable) %>%
filter(Date > min.date, Date <= max.date)
var.names <- c("Total.cases", "Total.in.NZ", "Total.overseas",
"Active.cases", "Infected.in.NZ", "Infected.overseas",
"Recovered", "Dead")
my.colours <- c("red", alpha(c("indianred4", "indianred2"), c(0.25, 0.15)),
"darkorange", "indianred4", "indianred2",
"seagreen", "black")
#as an experiment...
# palname <- sample(names(wes_palettes), 1)
# wp <- wes_palette(palname, n = 5, type = "continuous")
# my.colours <- c(wp[1], alpha(wp[3:4], c(0.25, 0.15)), wp[2:5], "black")
g1 <- ggplot(alert.levels) +
# annotation_custom(rasterGrob(covid_bg, width = unit(1, "npc"), height = unit(1, "npc")),
#                   -Inf, Inf, -Inf, Inf) +
geom_rect(aes(xmin = lag(Date), xmax = Date,
ymin = min(changes$value, na.rm = TRUE), ymax = max(cases$value, na.rm = TRUE),
alpha = value)) +
scale_alpha(range = c(0.1, 0.6)) +
geom_area(data = filter(cases, variable %in% c("Total.in.NZ", "Total.overseas")),
aes(x = Date, y = value, fill = variable, group = variable)) +
annotate("text", x = rep(max.date, 2), y =  c(max(c19$Total.overseas + 0.5 * c19$Total.in.NZ),
0.5 * max(c19$Total.overseas)),
colour = '#555555',
# colour = '#ffcc66',
label = c('italic(\"Infected in New Zealand\")', 'italic(\"Infected overseas\")'),
parse = TRUE, hjust = 0.5, vjust = -0.1, angle = -90, cex = 3
) +
geom_line(data = filter(cases, !(variable %in% c("Total.in.NZ", "Total.overseas"))),
aes(x = Date, y = value, colour = variable, group = variable)) +
scale_colour_manual(breaks = var.names[c(1, 4)], values = my.colours[c(1, 4)]) +
geom_col(data = changes, aes(x = Date, y = value, fill = variable), width = 1) +
scale_fill_manual(breaks = var.names[2:8], values = my.colours[2:8]) +
scale_x_date(date_breaks = 'months', date_labels = '%b') +
labs(y = 'Number of people') +
theme_minimal() +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank()) +
guides(alpha = FALSE,
colour = FALSE,
fill = FALSE)
all.data <- c19 %>%
dplyr::select(Date, Active.cases, Dead, Infected.overseas, Infected.in.NZ, Recovered, Total.cases) %>%
pivot_longer(-Date) %>%
rename(variable = name) %>%
arrange(variable, Date) %>%
filter(Date > min.date, Date <= max.date)
all.data$value <- replace(all.data$value, all.data$value == 0, NA)
g2 <- ggplot(alert.levels) +
geom_rect(aes(xmin = lag(Date), xmax = Date,
ymin = 1, ymax = max(all.data$value, na.rm = TRUE),
alpha = value)) +
scale_alpha(range = c(0.1, 0.6)) +
# this horrible hack: https://stackoverflow.com/questions/44168996/how-to-create-a-continuous-legend-color-bar-style-for-scale-alpha
# is used to get a continuous legend bar for the alert levels because ggplot does
# not allow for a continuous alpha legend even though scale_alpha is continuous...
geom_point(aes(x = min.date, y = 1, fill = value), alpha = 0) +
scale_fill_gradient(high = "#00000099", low = "#00000019") +
geom_line(data = all.data, aes(x = Date, y = value, colour = variable, group = variable)) +
scale_colour_manual(breaks = var.names, values = my.colours) +
scale_y_log10() +
scale_x_date(date_breaks = 'months', date_labels = '%b') +
labs(x = "", y = 'Number of people') +
theme_minimal() +
guides(alpha = FALSE) +
labs(colour = "Counts",
fill = "Alert levels")
# ,
#       caption = paste("wes palette:", palname))
# g2
ggarrange(g1, g2, ncol = 1, nrow = 2, legend = 'right', common.legend = TRUE)
plotly::ggplotly(g1)
setwd("~/Documents/code/spatial-epi/delta")
cases.new <- read.csv("https://github.com/minhealthnz/nz-covid-data/blob/main/locations-of-interest/august-2021/locations-of-interest.csv?raw=true") %>%
drop_na() %>%
st_as_sf(coords = c("LNG", "LAT"), crs = 4326) %>%
mutate(Start = as_datetime(Start, format = "%e/%m/%Y,%t%l:%M%t%p"),
End = as_datetime(End, format = "%e/%m/%Y,%t%l:%M%t%p")) %>%
mutate(lat = st_coordinates(geometry)[, 2],
lon = st_coordinates(geometry)[, 1]) %>%
select(id, Event, Location, City, Start, End, Advice, Added, lat, lon, geometry)
cases.old <- st_read("loi.geojson")
cases <- bind_rows(cases.old, cases.new) %>%
distinct(id, .keep_all = TRUE)
st_write(cases, "loi.geojson", delete_dsn = TRUE)
tmap_mode("plot")
setwd("~/Documents/code/spatial-epi/delta")
cases.new <- read.csv("https://github.com/minhealthnz/nz-covid-data/blob/main/locations-of-interest/august-2021/locations-of-interest.csv?raw=true") %>%
drop_na() %>%
st_as_sf(coords = c("LNG", "LAT"), crs = 4326) %>%
mutate(Start = as_datetime(Start, format = "%e/%m/%Y,%t%l:%M%t%p"),
End = as_datetime(End, format = "%e/%m/%Y,%t%l:%M%t%p")) %>%
mutate(lat = st_coordinates(geometry)[, 2],
lon = st_coordinates(geometry)[, 1]) %>%
select(id, Event, Location, City, Start, End, Advice, Added, lat, lon, geometry)
cases.old <- st_read("loi.geojson")
cases <- bind_rows(cases.old, cases.new) %>%
distinct(id, .keep_all = TRUE)
st_write(cases, "loi.geojson", delete_dsn = TRUE)
deck <- deckgl(latitude = -41, longitude = 174, zoom = 3, width = '100vw', height = '100vh') %>%
add_source("cases", cases) %>%
add_geojson_layer(
source = "cases",
getRadius = 1500,
stroked = FALSE,
pointRadiusUnits = 'meters',
pointRadiusMinPixels = 3,
pointRadiusMaxPixels = 25,
getFillColor = c(204, 102, 0, 153),
getTooltip = ~Event #JS("object => object.properties.name")
) %>%
add_basemap(use_carto_style("positron"))
if (interactive()) deck
deck <- deckgl(latitude = -41, longitude = 174, zoom = 3, width = '100vw', height = '100vh') %>%
add_source("cases", cases) %>%
add_geojson_layer(
source = "cases",
getRadius = 1500,
stroked = FALSE,
pointRadiusUnits = 'meters',
pointRadiusMinPixels = 3,
pointRadiusMaxPixels = 20,
getFillColor = c(204, 102, 0, 153),
getTooltip = ~Event #JS("object => object.properties.name")
) %>%
add_basemap(use_carto_style("positron"))
if (interactive()) deck
library(ggplot2)
library(dplyr)
library(tidyr)
library(magrittr)
library(lubridate)
library(stringr)
library(wesanderson)
library(ggpubr)
library(grid)
library(readxl)
library(readODS)
# library(png)
setwd("~/Documents/code/spatial-epi/staging-area")
# covid_bg <- readPNG("light-blue-coronavirus.png", native = TRUE)
c19 <- read_ods("cases.ods")
c19.all <- read.csv("https://raw.githubusercontent.com/UoA-eResearch/nz-covid19-data-auto/master/data.csv")
names(c19.all) <- c("Date", "Status", "Sex", "Age", "DHB", "Overseas", "Historical")
c19.all$Date <- as.Date(parse_date_time(c19.all$Date, "ymd"))
# c19.all %<>% select(-Case_number)
c19.community <- c19.all %>%
# select(-NA) %>%
filter(Overseas == "No") %>%
group_by(Date) %>%
summarise(Infected.in.NZ = n())
c19.overseas <- c19.all %>%
# select(-NA) %>%
filter(Overseas == "Yes") %>%
group_by(Date) %>%
summarise(Infected.overseas = n())
c19$Date <- as.Date(parse_date_time(c19$Date, "ymd"))
max.date <- max(c19$Date)
min.date <- min(c19$Date)
c19 <- c19 %>%
#  filter(Date >= min.date & Date <= max.date) %>%
left_join(c19.community) %>%
left_join(c19.overseas)
alert.levels <- c19 %>%
dplyr::select(Date, Level) %>%
pivot_longer(-Date) %>%
rename(variable = name) %>%
arrange(variable, Date) %>%
filter(Date > min.date, Date <= max.date)
# This code determines the total cases from the provided spreadsheet input
# by cumulative calculation, but leads to inconsistency with the reported daily numbers... ¯\_(ツ)_/¯
c19 %<>%
mutate(Total.overseas = cumsum(replace_na(Infected.overseas, 0)),
Total.in.NZ = cumsum(replace_na(Infected.in.NZ, 0)),
Total.cases =  Total.overseas + Total.in.NZ)
# ,
#          Active.cases = Total.cases - replace_na(Recovered, 0) - replace_na(Dead, 0))
cases <- c19 %>%
dplyr::select(Date, Total.cases, Active.cases, Total.in.NZ, Total.overseas) %>%
# select(Date, Active.cases) %>%
pivot_longer(-Date) %>%
rename(variable = name) %>%
arrange(variable, Date) %>%
filter(Date > min.date, Date <= max.date)
changes <- c19 %>%
mutate(Recovered = -New.recovered,
Dead = -New.dead) %>%
dplyr::select(Date, Infected.in.NZ, Infected.overseas, Recovered, Dead) %>%
pivot_longer(-Date) %>%
rename(variable = name) %>%
arrange(variable) %>%
filter(Date > min.date, Date <= max.date)
var.names <- c("Total.cases", "Total.in.NZ", "Total.overseas",
"Active.cases", "Infected.in.NZ", "Infected.overseas",
"Recovered", "Dead")
my.colours <- c("red", alpha(c("indianred4", "indianred2"), c(0.25, 0.15)),
"darkorange", "indianred4", "indianred2",
"seagreen", "black")
#as an experiment...
# palname <- sample(names(wes_palettes), 1)
# wp <- wes_palette(palname, n = 5, type = "continuous")
# my.colours <- c(wp[1], alpha(wp[3:4], c(0.25, 0.15)), wp[2:5], "black")
g1 <- ggplot(alert.levels) +
# annotation_custom(rasterGrob(covid_bg, width = unit(1, "npc"), height = unit(1, "npc")),
#                   -Inf, Inf, -Inf, Inf) +
geom_rect(aes(xmin = lag(Date), xmax = Date,
ymin = min(changes$value, na.rm = TRUE), ymax = max(cases$value, na.rm = TRUE),
alpha = value)) +
scale_alpha(range = c(0.1, 0.6)) +
geom_area(data = filter(cases, variable %in% c("Total.in.NZ", "Total.overseas")),
aes(x = Date, y = value, fill = variable, group = variable)) +
annotate("text", x = rep(max.date, 2), y =  c(max(c19$Total.overseas + 0.5 * c19$Total.in.NZ),
0.5 * max(c19$Total.overseas)),
colour = '#555555',
# colour = '#ffcc66',
label = c('italic(\"Infected in New Zealand\")', 'italic(\"Infected overseas\")'),
parse = TRUE, hjust = 0.5, vjust = -0.1, angle = -90, cex = 3
) +
geom_line(data = filter(cases, !(variable %in% c("Total.in.NZ", "Total.overseas"))),
aes(x = Date, y = value, colour = variable, group = variable)) +
scale_colour_manual(breaks = var.names[c(1, 4)], values = my.colours[c(1, 4)]) +
geom_col(data = changes, aes(x = Date, y = value, fill = variable), width = 1) +
scale_fill_manual(breaks = var.names[2:8], values = my.colours[2:8]) +
scale_x_date(date_breaks = 'months', date_labels = '%b') +
labs(y = 'Number of people') +
theme_minimal() +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank()) +
guides(alpha = FALSE,
colour = FALSE,
fill = FALSE)
all.data <- c19 %>%
dplyr::select(Date, Active.cases, Dead, Infected.overseas, Infected.in.NZ, Recovered, Total.cases) %>%
pivot_longer(-Date) %>%
rename(variable = name) %>%
arrange(variable, Date) %>%
filter(Date > min.date, Date <= max.date)
all.data$value <- replace(all.data$value, all.data$value == 0, NA)
g2 <- ggplot(alert.levels) +
geom_rect(aes(xmin = lag(Date), xmax = Date,
ymin = 1, ymax = max(all.data$value, na.rm = TRUE),
alpha = value)) +
scale_alpha(range = c(0.1, 0.6)) +
# this horrible hack: https://stackoverflow.com/questions/44168996/how-to-create-a-continuous-legend-color-bar-style-for-scale-alpha
# is used to get a continuous legend bar for the alert levels because ggplot does
# not allow for a continuous alpha legend even though scale_alpha is continuous...
geom_point(aes(x = min.date, y = 1, fill = value), alpha = 0) +
scale_fill_gradient(high = "#00000099", low = "#00000019") +
geom_line(data = all.data, aes(x = Date, y = value, colour = variable, group = variable)) +
scale_colour_manual(breaks = var.names, values = my.colours) +
scale_y_log10() +
scale_x_date(date_breaks = 'months', date_labels = '%b') +
labs(x = "", y = 'Number of people') +
theme_minimal() +
guides(alpha = FALSE) +
labs(colour = "Counts",
fill = "Alert levels")
# ,
#       caption = paste("wes palette:", palname))
# g2
ggarrange(g1, g2, ncol = 1, nrow = 2, legend = 'right', common.legend = TRUE)
plotly::ggplotly(g1)
setwd("~/Documents/teaching/GISC-420/labs/geopandas-as-gis/data")
pts <- st_read("crashes.geojson")
dir()
pts <- st_read("crashes-2017.gpkg")
polys <- st_read("welly-census-2013.gpkg")
counts <- polys %>% st_contains(pts) %>% lengths()
polys$n <- counts
tm_shape(polys) + tm_polygons(col = "n")
tm_shape(polys) + tm_polygons(col = "n", style = "quantile")
as_tibble(polys)
polys <- st_read("welly-census-2013.gpkg") %>% group_by(AU2013) %>% summarise()
counts <- polys %>% st_contains(pts) %>% lengths()
polys$n <- counts
tm_shape(polys) + tm_polygons(col = "n", style = "quantile")
tm_shape(polys) + tm_polygons(col = "n", style = "quantile", legend.format = list(digits = 0))
tm_shape(polys) + tm_polygons(col = "n", style = "quantile", as.count = TRUE)
tm_shape(polys) + tm_polygons(col = "n", style = "pretty", as.count = TRUE)
tm_shape(polys) + tm_polygons(col = "n", style = "prettylog10", as.count = TRUE)
tm_shape(polys) + tm_polygons(col = "n", style = "log10_pretty", as.count = TRUE)
tm_shape(polys) + tm_polygons(col = "n", style = "pretty", as.count = TRUE)
