Last active
August 29, 2015 14:15
-
-
Save TonyLadson/c7b37ff92ae12816fe50 to your computer and use it in GitHub Desktop.
Code to summarise and visualise quality codes in hydrologic data
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
# see https://tonyladson.wordpress.com/2015/02/17/quality-codes-in-hydrologic-data/ | |
library(treemap) | |
library(RColorBrewer) | |
library(ggplot2) | |
library(grid) | |
library(lubridate) | |
# Generate some data | |
# dates | |
set.seed(2015) | |
xdate <- seq(as.Date('2000-01-01'),as.Date('2002-12-31'), by = '1 day' ) | |
# generate some flow data, usually this would come from a stream gauging record | |
flow <- rlnorm(n= length(xdate), meanlog = 1, sdlog=0.5) | |
qcodes <- c(1, 2, 15, 50, 151, 240, 255) | |
qcode.seq <- sample(qcodes, length(xdate), replace = TRUE, prob = 1/qcodes) | |
my.data <- data.frame(xdate = xdate, flow = flow, qcode = qcode.seq) | |
# my.data can be replaced with a data set | |
# Tabulate quality codes | |
with(my.data, table(qcode)) | |
# 1 2 15 50 151 255 | |
# 704 340 37 9 4 2 | |
# Percentage of data with each code | |
with(my.data, round(100*prop.table(table(qcode)), 2 )) | |
# 1 2 15 50 151 255 | |
# 64.23 31.02 3.38 0.82 0.36 0.18 | |
# Tree map of quality codes | |
qcode.table.df <- with(my.data, as.data.frame(table(qcode))) | |
treemap(qcode.table.df, index="qcode", vSize="Freq", title="", algorithm="pivotSize", palette=rev(brewer.pal(8, "RdYlGn"))) | |
# Tile plot showing the occurance of quality codes | |
# set up x-axes labels | |
month.start <- yday( seq(as.Date('2000-01-01'),as.Date('2000-12-31'), by = 'months' )) | |
pl<- ggplot(my.data,aes(yday(xdate), year(xdate))) | |
pl + geom_tile(aes(fill = factor(qcode))) + | |
scale_fill_manual(values = rev(brewer.pal(8, "RdYlGn")), name='Quality Code' ) + | |
theme_bw() + | |
theme(axis.title.x = element_text(colour="grey20", size=20, vjust=-2)) + | |
theme(axis.text.x = element_text(colour="grey20",size=12)) + | |
theme(axis.title.y = element_text(colour="grey20",size=20, vjust=0)) + | |
theme(axis.text.y = element_text(colour="grey20",size=12)) + | |
theme(legend.title = element_text(colour="grey20",size=12)) + | |
theme(plot.margin = unit(c(2.5, 2.5, 2.5, 2.5), "cm")) + | |
ylab("Year") + | |
scale_x_continuous(name="Month", breaks=month.start, labels=month.abb) | |
# Time series showing the occurance of quality codes | |
pl <- ggplot(my.data, aes(x=xdate, y=flow, color=factor(qcode) )) | |
pl + geom_point() + | |
scale_color_manual(values = rev(brewer.pal(8, "RdYlGn")), name = "Quality code") + | |
theme_bw() + | |
theme(axis.title.x = element_text(colour="grey20", size=20, vjust=-2)) + | |
theme(axis.text.x = element_text(colour="grey20",size=10)) + | |
theme(axis.title.y = element_text(colour="grey20",size=20, vjust=0)) + | |
theme(axis.text.y = element_text(colour="grey20",size=15)) + | |
theme(legend.title = element_text(colour="grey20",size=12)) + | |
theme(legend.text = element_text(colour="grey20",size=12)) + | |
theme(plot.margin = unit(c(2.5, 2.5, 2.5, 2.5), "cm")) + | |
labs(x = "Date") + | |
labs(y = "Flow") | |
# Box plot by quality code | |
pl <- ggplot(my.data, aes(x=factor(qcode), y=flow)) | |
pl + geom_boxplot(aes(fill=factor(qcode)), outlier.size=0) + | |
geom_jitter(alpha=0.3) + | |
scale_fill_manual(values=rev(brewer.pal(8, "RdYlGn")), name="Quality code") + | |
labs(x = "Quality code") + | |
labs(y = "Flow") + | |
theme_bw() + | |
theme(axis.title.x = element_text(colour="grey20", size=20, vjust=-2)) + | |
theme(axis.text.x = element_text(colour="grey20",size=12)) + | |
theme(axis.title.y = element_text(colour="grey20",size=20, vjust=0)) + | |
theme(axis.text.y = element_text(colour="grey20",size=12)) + | |
theme(legend.title = element_text(colour="grey20",size=12)) + | |
theme(plot.margin = unit(c(2.5, 2.5, 2.5, 2.5), "cm")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For details see blog at https://tonyladson.wordpress.com/2015/02/17/quality-codes-in-hydrologic-data/