Last active
June 16, 2021 20:11
-
-
Save TonyLadson/d747b73f837c1e2e4d9a to your computer and use it in GitHub Desktop.
Plotting percentiles see: https://tonyladson.wordpress.com/2016/02/22/plotting-percentiles/
This file contains 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
library(quantreg) | |
library(dplyr) | |
library(lubridate) | |
library(stringr) | |
library(readr) | |
library(ggplot2) | |
library(devtools) | |
library(splines) | |
# Source my preferred ggplot Theme see | |
# see https://gist.github.com/TonyLadson/cc60bbb3cbadf0e72619 | |
devtools::source_gist('cc60bbb3cbadf0e72619') | |
# Load sample data | |
id <- "1uIPrjRq-qghTbBIzBXcvDfxbzJ250i6M" | |
wq <- read_csv(sprintf("https://docs.google.com/uc?id=%s&export=download", id)) | |
# parse and sort | |
wq <- wq %>% | |
mutate(xdatetime = stringr::str_c(Date, Time, sep = ' ')) %>% | |
mutate(xdatetime = lubridate::ymd_hms(xdatetime)) %>% # set up dates | |
arrange(xdatetime) # sort by date | |
# Check for missing values | |
colSums(is.na(wq)) | |
# remove them | |
wq <- wq[complete.cases(wq), ] | |
# Add quantile fits | |
# create 75th and 25th quantile models | |
fit.75 <- quantreg::rq(Total.N ~ splines::bs(xdatetime, df=15), tau=0.75, data=wq) | |
fit.50 <- quantreg::rq(Total.N ~ splines::bs(xdatetime, df=15), tau=0.50, data=wq) | |
fit.25 <- quantreg::rq(Total.N ~ splines::bs(xdatetime, df=15), tau=0.25, data=wq) | |
# Add quantiles to data frame | |
wq <- wq %>% | |
mutate(pc.75 = predict(fit.75)) %>% | |
mutate(pc.50 = predict(fit.50)) %>% | |
mutate(pc.25 = predict(fit.25)) | |
# plot | |
wq %>% | |
ggplot(aes(x = xdatetime)) + | |
geom_point(aes(y = Total.N)) + | |
geom_line(aes(y = pc.75, colour = '75th percentile')) + | |
geom_line(aes(y = pc.50, colour = 'Median')) + | |
geom_line(aes(y = pc.25, colour = '25th percentile')) + | |
scale_color_manual('', values = c('75th percentile' = 'red', '25th percentile' = 'green', 'Median' = 'blue'), | |
breaks = c('75th percentile', 'Median', '25th percentile')) + | |
BwTheme + | |
xlab('Date') + | |
ylab('Total Nitrogen mg/L') |
Hi Tony, the csv file is no longer available.
Peter
Should now work ok. Dropbox changed their rules around the public folder. I've move the data to google drive
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Tony, the csv file is no longer available.
Peter