tests/testthat/test_longitudinal_profile_plot.R

context("longitudinal profile plot")
library(razviz)

# plot_number
plot_number <- 9

# hydro_model
path <- system.file("extdata/longitudinal_profiles",
                    package = "razviz")
pattern <- "Freq"
hydro_model <- razviz::combine_files(path = path, pattern = pattern)

## Create `Event` field for labeling
hydro_model$Event <- hydro_model$Freq

## Filter the Events
model_events <- c("2 Year", "100 Year", "500 Year", "100000 Year")
hydro_model_1 <- dplyr::filter(hydro_model, Event %in% model_events)

## Set Event as an ordered factor
hydro_model_1$Event <- factor(hydro_model_1$Event,
                              levels = model_events,
                              labels = model_events)
#set the variable to plot
hydro_model_1$hydro_parameter <- hydro_model_1$WS_Elev


# high_water
high_water_csv <- system.file("extdata/longitudinal_profiles",
                              "high_water_marks.csv",
                              package = "razviz")

high_water <- readr::read_csv(high_water_csv)
high_water_years <- c("2008", "2013", "2014")

high_water_events_df <- razviz::high_water_events(high_water, high_water_years)
## Set Event as an ordered factor
high_water_events_df$event <- factor(high_water_events_df$event,
                                     levels = high_water_years,
                                     labels = high_water_years)

# long_plot_pages
miles_per_plot <- 10
long_plot_pgs <- razviz::long_plot_pages(hydro_model = hydro_model_1,
                                          high_water = high_water_events_df,
                                          miles_per_plot = miles_per_plot)

# gages
gage_csv <- system.file("extdata/longitudinal_profiles",
                        "gage_locations.csv", package = "razviz")
gages <- readr::read_csv(gage_csv)

## Remove double backslashes introduced by R import
gages$name <- gsub("\\n", "\n", gages$name, fixed=TRUE)

# gage_labels
stage_interval_label <- 5
gage_labels_df <- razviz::gage_labels(gages, stage_interval_label)

# gage_boxes
stage_interval_boxes <- 1
box_width <- 0.1
gage_boxes_df <- razviz::gage_boxes(gages, stage_interval_boxes, box_width)

# levees
levees_csv <- system.file("extdata/longitudinal_profiles",
                          "levees_authorized_existing.csv",
                          package = "razviz")
levees <- readr::read_csv(levees_csv)
levees <- dplyr::filter(levees, elevation_type == "2016_NLD")

# features
features_csv <- system.file("extdata/longitudinal_profiles",
                            "river_features.csv",
                            package = "razviz")
features <- readr::read_csv(features_csv)

# bridges
bridges_csv <- system.file("extdata/longitudinal_profiles",
                           "bridge_elevations.csv",
                           package = "razviz")
bridges <- readr::read_csv(bridges_csv)

# graph_colors https://wesandersonpalettes.tumblr.com, names from colors().
graph_colors <- c("2 Year"      = "darkslategray4",
                  "100 Year"    = "cadetblue3",
                  "500 Year"    = "coral3",
                  "100000 Year" = "burlywood3",
                  "2008"        = "red",
                  "2013"        = "red",
                  "2014"        = "red",
                  "LEFT"        = "palevioletred2",
                  "RIGHT"       = "palevioletred4")

# legend_labels
legend_labels <- c("2 Year", "100 Year", "500 Year", "100000 Year",
                   "2008", "2013", "2014",
                   "Left Bank", "Right Bank")

# title
plot_labels <- list("title" = "Upper Mississippi River Hydraulic Model - Guttenburg to Clarksville",
                    "x_axis" = "Miles Above the Ohio River",
                    "y_axis" = "Elevation (NAVD88 feet)")

# longitudinal_profile_plot (without smoothed levees)
plot1 <- razviz::longitudinal_profile_plot(plot_number = plot_number,
                                           hydro_model = hydro_model_1,
                                           long_plot_pgs = long_plot_pgs,
                                           gages = gages,
                                           gage_labels_df = gage_labels_df,
                                           gage_boxes_df = gage_boxes_df,
                                           high_water = high_water_events_df,
                                           levees = levees,
                                           features = features,
                                           bridges = bridges,
                                           graph_colors = graph_colors,
                                           legend_labels = legend_labels,
                                           plot_labels = plot_labels,
                                           levee_smooth = FALSE)

# longitudinal_profile_plot (with smoothed levees)
plot2 <- razviz::longitudinal_profile_plot(plot_number = plot_number,
                                           hydro_model = hydro_model_1,
                                           long_plot_pgs = long_plot_pgs,
                                           gages = gages,
                                           gage_labels_df = gage_labels_df,
                                           gage_boxes_df = gage_boxes_df,
                                           high_water = high_water_events_df,
                                           levees = levees,
                                           features = features,
                                           bridges = bridges,
                                           graph_colors = graph_colors,
                                           legend_labels = legend_labels,
                                           plot_labels = plot_labels,
                                           levee_smooth = TRUE)

# longitudinal_profile_plot (without  any background data)
plot3 <- razviz::longitudinal_profile_plot(plot_number = plot_number,
                                            hydro_model = hydro_model_1,
                                            long_plot_pgs = long_plot_pgs,
                                            graph_colors = graph_colors,
                                            legend_labels = legend_labels,
                                            plot_labels = plot_labels)

# longitudinal_profile_plot (with only gages)
plot4 <- razviz::longitudinal_profile_plot(plot_number = plot_number,
                                           hydro_model = hydro_model_1,
                                           long_plot_pgs = long_plot_pgs,
                                           graph_colors = graph_colors,
                                           legend_labels = legend_labels,
                                           plot_labels = plot_labels,
                                           gages = gages,
                                           gage_labels_df = gage_labels_df,
                                           gage_boxes_df = gage_boxes_df,)


test_that("longitudinal profile plot", {
  expect_true(class(plot1)[[2]] == "ggplot")
  expect_true(class(plot2)[[2]] == "ggplot")
  expect_true(class(plot3)[[2]] == "ggplot")
  expect_true(class(plot4)[[2]] == "ggplot")
})
mpdougherty/razviz documentation built on April 1, 2021, 4:16 p.m.