Skip to content

Instantly share code, notes, and snippets.

@adamhsparks
adamhsparks / Sigatoka_dual_axis.R
Last active January 21, 2022 02:18
Another example of dual axis in ggplot2
# Entering data
week <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
YLI <- c(3, 4, 5, 2, 6, 5, 4, 3, 5, 6, 2, 4)
YLS <- c(10, 11, 11, 12, 10, 11, 11, 10, 12, 13, 10, 11)
rainfall <- c(1, 10, 8, 6, 65, 30, 2, 10, 8, 6, 65, 30)
# Creating Data Frame
perf <- data.frame(week, YLI, YLS, rainfall)
# Plotting Charts and adding a secondary axis
@adamhsparks
adamhsparks / audpc.jl
Last active January 19, 2022 10:57
A Julia function to calculate area under the disease progress curve (AUDPC)
function audpc(evaluation, dates)
n = length(evaluation) - 1
disvec = Base.zeros(n)
datevec = Base.zeros(n)
out = 0.0
for i in 1:n
disvec[i] = (evaluation[i] + evaluation[i + 1]) / 2
datevec[i] = dates[i + 1] - dates[i]
out = sum(disvec .* datevec)
@adamhsparks
adamhsparks / downwind.R
Created September 21, 2021 01:21
Spore trap upwind/downwind
dat <-
dat %>%
left_join(wind, by = c("field" = "FIELD")) %>%
mutate(
field = factor(field),
trap_coord_deg = case_when(
trap_coord == "N" ~ 0,
trap_coord == "NbE" ~ 11.25,
trap_coord == "NNE" ~ 22.5,
trap_coord == "NEbN" ~ 33.75,
##############################################################################
# title : EPIRICE-sim-bs-lb.R;
# purpose : Install the epicrop package and simulate rice brown spot and
# rice leaf blast epidemics;
# producer : prepared by A. Sparks;
# last update : in Perth, WA, Australia, Jun 2021;
# inputs : na;
# outputs : leaf blast and brown spot model outputs of unmanaged disase
# epidemics;
# remarks : Prepared for Jean Fabrice Adanve;
@adamhsparks
adamhsparks / mean_weather.R
Last active March 17, 2021 12:07
Download and calculate country and year mean values for weather variables using GSODR and dplyr
library("GSODR")
library("tidyr")
library("dplyr")
library("countrycode")
w <- get_GSOD(years = 1929:1931)
w <-
w %>%
group_by(CTRY, YEAR) %>%
@adamhsparks
adamhsparks / leaf_wet.jl
Created February 20, 2021 09:29
Julia code to calculate leaf wetness using latitude, day of year and relative humidity. Not functional, just a start at translation from R, see leaf_wet.R gist.
function leaf_wet(wth = wth, simple = TRUE)
rh = diurnal_rh(
doy = wth[, DOY],
rh = wth[, RHUM],
tmin = wth[, TMIN],
tmax = wth[, TMAX],
tmp = wth[, TEMP],
lat = wth[, LAT]
@adamhsparks
adamhsparks / leaf_wet.R
Last active April 22, 2025 00:41
Calculate leaf wetness in R using relative humidity, latitude and day length. Adapted from GPL3 code from R. Hijmans in the meteor and cropsim packages.
#' Get weather data from NASA POWER API
#'
#' @param lonlat A numeric vector of geographic coordinates for a cell or region
#' entered as x, y coordinates.
#' @param dates A character vector of start and end dates in that order.
#'
#' @return A \code{\link[data.table]{data.table}} of weather data, dates and
@adamhsparks
adamhsparks / Create PHL Map
Last active April 9, 2020 07:08
Quickly creates a generic map using ggplot2, sf and rnaturalearth
library("ggplot2")
library("ggthemes")
theme_set(theme_map())
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")
world <- ne_countries(scale = "medium", returnclass = "sf")
ctry <- world[world$name == "Philippines", ]
@adamhsparks
adamhsparks / track_package_downloads.R
Last active August 13, 2024 03:12
Tracks my R package downloads over time
library(dplyr)
library(ggplot2)
library(lubridate)
library(cranlogs)
library(janitor)
dl <-
cran_downloads(
package = c(
"bomrang",
@adamhsparks
adamhsparks / bomrang_climogram.R
Last active July 3, 2019 04:32
Generate a climogram using bomrang to download station data for 2018/19 summer growing season in Australia
library(bomrang)
library(tidyverse)
library(theme.usq)
# Find stations ----------------------------------------------------------------
# Find nearest stations for each area
# search https://www.latlong.net/ for the lat lon values of each location
# clermont