Last active
October 14, 2015 00:09
-
-
Save TonyLadson/dc8cadbb6165f068ba56 to your computer and use it in GitHub Desktop.
Plotting Spells see https://tonyladson.wordpress.com/2015/10/14/plotting-spells/
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(ggplot) | |
library(grid) | |
library(devtools) | |
library(dplyr) | |
library(lubridate) | |
source_gist('cc60bbb3cbadf0e72619') # ggplot theme | |
# Data is stored in a data frame with the date, flow and quality code | |
# You'll need to provide your own data for this to work. | |
joyce | |
# Source: local data frame [19,025 x 3] | |
# | |
# Datetime flow QC | |
# 1 1963-05-14 1.001 1 | |
# 2 1963-05-15 61.000 1 | |
# 3 1963-05-16 32.000 1 | |
# 4 1963-05-17 34.000 1 | |
# 5 1963-05-18 19.000 1 | |
# 6 1963-05-19 19.000 1 | |
# 7 1963-05-20 12.000 1 | |
# 8 1963-05-21 8.000 1 | |
# 9 1963-05-22 9.000 1 | |
# 10 1963-05-23 13.000 1 | |
# .. ... ... .. | |
str(joyce) | |
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 19025 obs. of 3 variables: | |
# $ Datetime: POSIXct, format: "1963-05-14" "1963-05-15" "1963-05-16" "1963-05-17" ... | |
# $ flow : num 1 61 32 34 19 ... | |
# $ QC : int 1 1 1 1 1 1 1 1 1 1 ... | |
# High flow spell plot | |
# Calculate day numbers for the start of each month | |
xdate <- seq(as.Date('2010-01-01'),as.Date('2010-12-31'), by='months') | |
month.start <- as.numeric(format(xdate,'%j')) | |
joyce %>% | |
mutate(high = ifelse(flow > 10, 1, 0)) %>% # We are interested in flows above 10 | |
mutate(high = ifelse(is.na(high), -1, high)) %>% # Specifically code missing values | |
mutate(my.year = year(Datetime)) %>% # year | |
mutate(my.day = yday(Datetime) ) %>% # day of the year | |
ggplot(aes(x = my.day, y = my.year)) + | |
geom_tile(aes(fill = factor(high, levels = c(1,0,-1) ))) + | |
scale_fill_manual(name = "High flows", values=c('dark blue', 'light blue','red'), labels = c(' > 10 ML/d', ' < 10 ML/d', ' missing')) + | |
scale_x_continuous(name="Month", breaks=month.start, labels=month.lab) + | |
scale_y_continuous(name="Year", breaks=1962:2015, labels=1962:2015) + | |
BwTheme + # Theme for ggplot see https://gist.github.com/TonyLadson/cc60bbb3cbadf0e72619 | |
theme(axis.text.y = element_text(colour="grey20",size=9)) # small text for year to include all years | |
# Low flow spell plot | |
# x-axes starts in September | |
xdate <- seq(as.Date('2010-01-01'),as.Date('2010-12-31'), by='months') | |
month.start <- as.numeric(format(xdate,'%j')) + 120 | |
month.start <- ifelse(month.start >365, month.start - 365, month.start) | |
joyce %>% | |
mutate(low = ifelse(flow < 0.1, 1, 0)) %>% # flows below 0.1 | |
mutate(low = ifelse(is.na(low), -1, low)) %>% # Specifically code missing values | |
mutate(my.year = year(Datetime)) %>% | |
mutate(my.day = yday(Datetime) + 120) %>% | |
mutate(my.year = ifelse(my.day > 365, my.year + 1, my.year)) %>% | |
mutate(my.day = ifelse(my.day > 365, my.day - 365, my.day)) %>% | |
ggplot(aes(x = my.day, y = my.year)) + | |
geom_tile(aes(fill = factor(low, levels = c(1, 0, -1)))) + | |
scale_fill_manual(name = "Low flows", values=c('light blue', 'dark blue', 'red'), labels = c(' < 0.1 ML/d', ' > 0.1 ML/d', ' Missing')) + | |
scale_x_continuous(name="Month", breaks=month.start, labels=month.lab) + | |
scale_y_continuous(name="Year", breaks=1962:2015, labels=1962:2015) + | |
BwTheme + # Theme for ggplot see https://gist.github.com/TonyLadson/cc60bbb3cbadf0e72619 | |
theme(axis.text.y = element_text(colour="grey20",size=9)) | |
# High flow spell plot | |
# Calculate day numbers for the start of each month | |
xdate <- seq(as.Date('2010-01-01'),as.Date('2010-12-31'), by='months') | |
month.start <- as.numeric(format(xdate,'%j')) | |
joyce %>% | |
mutate(high = ifelse(flow > 10, 1, 0)) %>% # We are interested in flows above 10 | |
mutate(high = ifelse(is.na(high), -1, high)) %>% # Specifically code missing values | |
mutate(my.year = year(Datetime)) %>% | |
mutate(my.day = yday(Datetime) ) %>% | |
ggplot(aes(x = my.day, y = my.year)) + | |
geom_tile(aes(fill = factor(high, levels = c(1,0,-1) ))) + | |
scale_fill_manual(name = "High flows", values=c('dark blue', 'light blue','red'), labels = c(' > 10 ML/d', ' < 10 ML/d', ' missing')) + | |
scale_x_continuous(name="Month", breaks=month.start, labels=month.lab) + | |
scale_y_continuous(name="Year", breaks=1962:2015, labels=1962:2015) + | |
BwTheme + # Theme for ggplot see https://gist.github.com/TonyLadson/cc60bbb3cbadf0e72619 | |
theme(axis.text.y = element_text(colour="grey20",size=9)) # small text for year to include all years |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment