Skip to content

Commit

Permalink
possibly passing all checks w/o notes
Browse files Browse the repository at this point in the history
  • Loading branch information
fawda123 committed Mar 31, 2015
1 parent 7e216c9 commit d457662
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 42 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ before_install:

install:
- ./travis-tool.sh install_deps
- ./travis_tool.sh install_r Rcpp

script: ./travis-tool.sh run_tests

Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: SWMPr
Type: Package
Title: SWMPr package for estuarine monitoring data
Version: 1.9.1
Date: 2015-3-30
Version: 1.9.2
Date: 2015-3-31
Author: Marcus Beck
Maintainer: Marcus Beck <[email protected]>
Description: This packages provides functions for retrieving, organizing, and
Expand Down
70 changes: 40 additions & 30 deletions R/swmpr_analyze.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,27 +190,28 @@ aggregate_metab.swmpr <- function(swmpr_in, by = 'weeks', na.action = na.pass, a
}

# long-form
to_agg <- tidyr::gather(to_agg, Estimate, Value, -date)
to_agg <- reshape2::melt(to_agg, measure.vars = c('Pg', 'Rt', 'NEM'))
names(to_agg) <- c('date', 'Estimate', 'Value')
to_agg$Estimate <- as.character(to_agg$Estimate)

# aggregate
sum_fun <- function(x, alpha_in = alpha, out){
sum_fun <- function(x, alpha_in = alpha){
x <- na.omit(x)
means <- mean(x)
margs <- suppressWarnings(
qt(1 - alpha_in/2, length(x) - 1) * sd(x)/sqrt(length(x))
)
upper <- means + margs
lower <- means - margs
return(get(out))
}
aggs <- dplyr::group_by(to_agg, date, Estimate)
aggs <- dplyr::summarize(aggs,
means = sum_fun(Value, out = 'means'),
lower = sum_fun(Value, out = 'lower'),
upper = sum_fun(Value, out = 'upper')
)

return(c(means, upper, lower))
}
aggs <- stats::aggregate(Value ~ date + Estimate, to_agg,
FUN = function(x) sum_fun(x, alpha_in = alpha))
aggs_vals <- data.frame(aggs[, 'Value'])
names(aggs_vals) <- c('means', 'lower', 'upper')
aggs <- data.frame(aggs[, c('date', 'Estimate')], aggs_vals)

# return output
return(aggs)

Expand Down Expand Up @@ -580,7 +581,9 @@ decomp_cj.swmpr <- function(swmpr_in, param, vals_out = FALSE, ...){
# otherwise, ggplot
to_plo <- out
to_plo <- reshape2::melt(to_plo, id.var = 'Time')
plo <- ggplot(to_plo, aes(x = Time, y = value, group = variable)) +
plo <- ggplot(to_plo,
aes_string(x = 'Time', y = 'value', group = 'variable')
) +
geom_line() +
facet_wrap(~variable, ncol = 1, scales = 'free_y') +
theme_bw()
Expand Down Expand Up @@ -863,7 +866,7 @@ plot_summary.swmpr <- function(swmpr_in, param, years = NULL, ...){
to_plo <- dat_plo
to_plo$month <- factor(to_plo$month, levels = rev(mo_levs), labels = rev(mo_labs))
p3 <- ggplot(to_plo, aes_string(x = param)) +
geom_histogram(aes(y = ..density..), colour = 'lightblue', binwidth = diff(range(to_plo[, param], na.rm = T))/30) +
geom_histogram(aes_string(y = '..density..'), colour = 'lightblue', binwidth = diff(range(to_plo[, param], na.rm = T))/30) +
facet_grid(month ~ .) +
xlab(ylab) +
theme_bw(base_family = 'Times') +
Expand All @@ -882,10 +885,10 @@ plot_summary.swmpr <- function(swmpr_in, param, years = NULL, ...){
to_plo$month <- factor(to_plo$month, labels = mo_labs, levels = mo_levs)
names(to_plo)[names(to_plo) %in% param] <- 'V1'
midpt <- mean(to_plo$V1, na.rm = T)
p4 <- ggplot(subset(to_plo, !is.na(V1)),
aes(x = year, y = month, fill = V1)) +
p4 <- ggplot(subset(to_plo, !is.na(to_plo$V1)),
aes_string(x = 'year', y = 'month', fill = 'V1')) +
geom_tile() +
geom_tile(data = subset(to_plo, is.na(V1)),
geom_tile(data = subset(to_plo, is.na(to_plo$V1)),
aes(x = year, y = month), fill = NA
) +
scale_fill_gradient2(name = ylab,
Expand All @@ -903,10 +906,10 @@ plot_summary.swmpr <- function(swmpr_in, param, years = NULL, ...){
names(to_plo)[names(to_plo) %in% param] <- 'trend'
to_plo$anom <- with(to_plo, V1 - trend)
rngs <- max(abs(range(to_plo$anom, na.rm = T)))
p5 <- ggplot(subset(to_plo, !is.na(anom)),
aes(x = year, y = month, fill = anom)) +
p5 <- ggplot(subset(to_plo, !is.na(to_plo$anom)),
aes_string(x = 'year', y = 'month', fill = 'anom')) +
geom_tile() +
geom_tile(data = subset(to_plo, is.na(anom)),
geom_tile(data = subset(to_plo, is.na(to_plo$anom)),
aes(x = year, y = month), fill = NA
) +
scale_fill_gradient2(name = ylab,
Expand All @@ -922,7 +925,8 @@ plot_summary.swmpr <- function(swmpr_in, param, years = NULL, ...){
# annual anomalies
yr_avg <- mean(yr_agg[, param], na.rm = T)
yr_agg$anom <- yr_agg[, param] - yr_avg
p6 <- ggplot(yr_agg, aes(x = year, y = anom, group = 1, fill = anom)) +
p6 <- ggplot(yr_agg,
aes_string(x = 'year', y = 'anom', group = '1', fill = 'anom')) +
geom_bar(stat = 'identity') +
scale_fill_gradient2(name = ylab,
low = 'lightblue', mid = 'lightgreen', high = 'tomato', midpoint = 0
Expand Down Expand Up @@ -1083,9 +1087,14 @@ ecometab.swmpr <- function(swmpr_in, depth_val = NULL, metab_units = 'mmol', tra
months <- as.character(format(dat$datetimestamp, '%m'))
hours <- as.character(format(dat$datetimestamp, '%H'))
clim_means <-dplyr:: mutate(dat, months = months, hours = hours)
clim_means <- dplyr::select(clim_means, months, hours, atemp, wspd, bp)
clim_means <- dplyr::group_by(clim_means, months, hours)
clim_means <- dplyr::summarise_each(clim_means, dplyr::funs(mean(., na.rm = T)))
clim_means <- clim_means[, c('months', 'hours', 'atemp', 'wspd', 'bp')]
clim_means <- reshape2::melt(clim_means,
measure.vars = c('atemp', 'wspd', 'bp')
)
clim_means <- dplyr::group_by(clim_means, 'months', 'hours', 'variable')
clim_means <- aggregate(value ~ months + hours + variable, clim_means,
FUN = mean, na.rm = T)
clim_means <- tidyr::spread(clim_means, 'variable', 'value')

# merge with original data
to_join <- data.frame(datetimestamp = dat$datetimestamp, months,
Expand Down Expand Up @@ -1118,10 +1127,10 @@ ecometab.swmpr <- function(swmpr_in, depth_val = NULL, metab_units = 'mmol', tra
dosat <- with(dat, do_mgl/(oxySol(temp * (1000 + sigt)/1000, sal)))

#station depth, defaults to mean depth value, floored at 1 in case not on bottom
#uses 'depth.val' if provided
#uses 'depth_val' if provided
if(is.null(depth_val))
H <- rep(0.5 + mean(pmax(1, dat$depth), na.rm = T), nrow(dat))
else H <- rep(depth.val, nrow(dat))
else H <- rep(depth_val, nrow(dat))

#use metab_day to add columns indicating light/day, date, and hours of sunlight
dat <- metab_day(dat, stat)
Expand Down Expand Up @@ -1269,13 +1278,14 @@ plot_metab.swmpr <- function(swmpr_in, by = 'months', alpha = 0.05, width = 10,
to_plo <- aggregate_metab(swmpr_in, by = by, alpha = alpha)

## base plot
p <- ggplot(to_plo, aes(x = date, y = means, group = Estimate)) +
p <- ggplot(to_plo, aes_string(x = 'date', y = 'means', group = 'Estimate')) +
geom_line()

# add bars if not days and alpha not null
if(by != 'days' & !is.null(alpha))
p <- p +
geom_errorbar(aes(ymin = lower, ymax = upper, group = Estimate),
geom_errorbar(
aes_string(ymin = 'lower', ymax = 'upper', group = 'Estimate'),
width = width)

# return blank
Expand All @@ -1288,16 +1298,16 @@ plot_metab.swmpr <- function(swmpr_in, by = 'months', alpha = 0.05, width = 10,
ylabs <- expression(paste('g ', O [2], ' ', m^-2, d^-1))

p <- p +
geom_line(aes(colour = Estimate)) +
geom_point(aes(colour = Estimate)) +
geom_line(aes_string(colour = 'Estimate')) +
geom_point(aes_string(colour = 'Estimate')) +
theme_bw() +
theme(axis.title.x = element_blank()) +
scale_y_continuous(ylabs)

if(by != 'days' & !is.null(alpha))
p <- p +
geom_errorbar(aes(ymin = lower, ymax = upper,
colour = Estimate, group = Estimate), width = width)
geom_errorbar(aes_string(ymin = 'lower', ymax = 'upper',
colour = 'Estimate', group = 'Estimate'), width = width)

return(p)

Expand Down
11 changes: 7 additions & 4 deletions R/swmpr_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ param_names <- function(param_type = c('nut', 'wq', 'met')){
map_reserve <- function(nerr_site_id, zoom = 11, text_sz = 6, text_col = 'black', map_type = 'terrain'){

# subset stat_locs by reserve
stats <- stat_locs[grepl(paste0('^', nerr_site_id), stat_locs$station_code), ]
dat_locs <- get('stat_locs')
stats <- dat_locs[grepl(paste0('^', nerr_site_id), dat_locs$station_code), ]

# base map
mapImageData <- ggmap::get_map(
Expand All @@ -312,8 +313,8 @@ map_reserve <- function(nerr_site_id, zoom = 11, text_sz = 6, text_col = 'black'
p <- ggmap::ggmap(mapImageData,
extent = "panel"
) +
geom_text(data = stats, aes(x = longitude, y = latitude,
label= station_code), size = text_sz, colour = text_col
geom_text(data = stats, aes_string(x = 'longitude', y = 'latitude',
label= 'station_code'), size = text_sz, colour = text_col
) +
ylab('Latitude') +
xlab('Longitude')
Expand Down Expand Up @@ -503,7 +504,9 @@ map_reserve <- function(nerr_site_id, zoom = 11, text_sz = 6, text_col = 'black'
#'
metab_day <- function(dat_in, stat_in){

stat_meta <- stat_locs[grep(gsub('wq$', '', stat_in), stat_locs$station_code),]
# station locations
dat_locs <- get('stat_locs')
stat_meta <- dat_locs[grep(gsub('wq$', '', stat_in), dat_locs$station_code),]

# all times are standard - no DST!
gmt_tab <- data.frame(
Expand Down
2 changes: 1 addition & 1 deletion R/swmpr_organize.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ qaqcchk.swmpr <- function(swmpr_in){
# format output as data.frame
out <- melt(out)
names(out) <- c('flag', 'count', 'variable')
out <- tidyr::spread(out, variable, count)
out <- tidyr::spread(out, 'variable', 'count')
out[is.na(out)] <- 0

# return output
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SWMPr is an R package that contains functions for retrieving, organizing, and an

To cite this package:

*Beck MW. 2015. SWMPr: An R package for the National Estuarine Research Reserve System. Version 1.9.1. https://github.com/fawda123/SWMPr*
*Beck MW. 2015. SWMPr: An R package for the National Estuarine Research Reserve System. Version 1.9.2. https://github.com/fawda123/SWMPr*

#Installing the package

Expand Down
2 changes: 1 addition & 1 deletion README.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ <h1>Overview</h1>
<p>SWMPr is an R package that contains functions for retrieving, organizing, and analyzing estuary monitoring data from the System Wide Monitoring Program (<a href="http://nerrs.noaa.gov/RCDefault.aspx?ID=18">SWMP</a>). SWMP was implemented by the National Estuarine Research Reserve System (<a href="http://nerrs.noaa.gov/">NERRS</a>) in 1995 to provide continuous monitoring data at over 300 stations in 28 estuaries across the United States. SWMP data are maintained online by the Centralized Data Management Office (CDMO). This R package provides several functions to retrieve, organize, and analyze SWMP data from the CDMO. All data obtained from the CDMO should be <a href="http://cdmo.baruch.sc.edu/data/citation.cfm">cited</a> using the format:</p>
<p><em>National Estuarine Research Reserve System (NERRS). 2012. System-wide Monitoring Program. Data accessed from the NOAA NERRS Centralized Data Management Office website: <a href="http://cdmo.baruch.sc.edu/" class="uri">http://cdmo.baruch.sc.edu/</a>; accessed 12 October 2012.</em></p>
<p>To cite this package:</p>
<p><em>Beck MW. 2015. SWMPr: An R package for the National Estuarine Research Reserve System. Version 1.9.1. <a href="https://github.com/fawda123/SWMPr" class="uri">https://github.com/fawda123/SWMPr</a></em></p>
<p><em>Beck MW. 2015. SWMPr: An R package for the National Estuarine Research Reserve System. Version 1.9.2. <a href="https://github.com/fawda123/SWMPr" class="uri">https://github.com/fawda123/SWMPr</a></em></p>
</div>
<div id="installing-the-package" class="section level1">
<h1>Installing the package</h1>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SWMPr is an R package that contains functions for retrieving, organizing, and an

To cite this package:

*Beck MW. 2015. SWMPr: An R package for the National Estuarine Research Reserve System. Version 1.9.1. https://github.com/fawda123/SWMPr*
*Beck MW. 2015. SWMPr: An R package for the National Estuarine Research Reserve System. Version 1.9.2. https://github.com/fawda123/SWMPr*

#Installing the package

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ install:

build_script:
- travis-tool.sh install_deps
- travis-tool.sh install_r Rcpp
- travis-tool.sh install_r Rcpp # this is important

test_script:
- travis-tool.sh run_tests
Expand Down
Binary file modified ggmapTemp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d457662

Please sign in to comment.