--- title: "Custom piperplots" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Custom piperplots} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- bcgwcat has a function for creating piper plots based on functions from the USGS package smwrGraphs. With `piper_plot()` we can create custom piper plots using data from EMS but with the option to add grouping variables from other data sets. This vignette will walk you through how to create custom piper plots using the `piper_plot()` function in R. > Note: Currently you can only customize your piper plots in R (not via the Shiny app). Let's get started! First we'll load the packages we want to use ```r library(bcgwcat) # To download EMS data and create piper plots library(dplyr) # To manipulate our data library(lubridate) # To manipulate dates knitr::opts_chunk$set(fig.width = 8, fig.asp = 1) ``` Now let's get our data by downloading from EMS (using the rems package under the hood). Because `rems_to_aquachem()` is generally used for exporting data to AquaChem, we use the `save = FALSE` argument to avoid saving to disk, and then use the `units_remove()` function to remove the units normally stored in the first row of AquaChem data. ```r r <- rems_to_aquachem(ems_ids = c("1401030", "1401377", "E292373"), save = FALSE) #> Checking for locally stored historical data... #> Last download was 72 days ago #> If you would like to update historical data, run 'rems::download_historic_data()' #> Checking for locally stored recent data... #> Fetching data from cache... #> For consistency EMS charge balances, anion sums, and cation sums have been replaced with recalculated values. #> See `?charge_balance` for more details. r <- units_remove(r) ``` Let's take a quick look... lots of data! ```r r #> # A tibble: 15 × 178 #> Sample_Date SampleID Coord_Lat Project Coord_Long StationID Watertype Analysis_Date shortWatertype Comment #> #> 1 1987-07-07 1401030… 49.2 BACKGR… -120. 075 Fresh Wa… NA #> 2 1991-08-07 1401030… 49.2 BACKGR… -120. 075 Fresh Wa… NA #> 3 1994-06-08 1401030… 49.2 BACKGR… -120. 075 Fresh Wa… NA #> 4 2001-09-09 1401030… 49.2 BACKGR… -120. 075 Fresh Wa… NA #> 5 2009-11-11 1401030… 49.2 BACKGR… -120. 075 Fresh Wa… NA #> 6 2010-08-09 1401030… 49.2 BACKGR… -120. 075 Fresh Wa… NA #> 7 2016-11-02 1401030… 49.2 BACKGR… -120. 075 Fresh Wa… NA #> 8 2018-06-14 1401030… 49.2 BACKGR… -120. 075 Ground W… NA #> 9 1987-07-07 1401377… 49.2 BACKGR… -120. 203 Fresh Wa… NA #> 10 1989-10-11 1401377… 49.2 BACKGR… -120. 203 Fresh Wa… NA #> 11 1994-03-24 1401377… 49.2 BACKGR… -120. 203 Fresh Wa… NA #> 12 2016-11-02 1401377… 49.2 BACKGR… -120. 203 Fresh Wa… NA #> 13 2020-06-29 1401377… 49.2 BACKGR… -120. 203 Ground W… NA #> 14 2015-03-06 E292373… 49.4 GROUND… -125. 426 Fresh Wa… NA #> 15 2017-10-11 E292373… 49.4 GROUND… -125. 426 Fresh Wa… NA #> # ℹ 168 more variables: Reference , Quality_control , Duplicate_ID , Labcode , #> # Location , Geology , X , Y , Elevation , Well_Depth , Screen_Top , #> # Screen_Mid , Screen_Bottom , Gradient , Station_Comment , Sample_Depth , #> # Temp , `14C` , `18O` , `2H` , Ag_diss , Ag_tot , Al_diss , #> # Al_tot , As_diss , As_tot , B , B_tot , Ba , Ba_tot , Benzene , #> # Br , Ca , Ca_tot , Cd_diss , Cd_tot , Cl , CN_diss , CN_tot , #> # Co_diss , Co_tot , CO3 , Cond , Cond_field , Cr_diss , … ``` By default, the piper plot creates a plot with points coloured by EMS ID. ```r piper_plot(d = r) ```
plot of chunk unnamed-chunk-4

plot of chunk unnamed-chunk-4

We can override this by specifying the `group` argument ourselves. In this case, let's create a column `Year` and colour points that. ```r r <- mutate(r, Year = year(Sample_Date)) piper_plot(d = r, group = "Year") ```
plot of chunk unnamed-chunk-5

plot of chunk unnamed-chunk-5

We can also customize the appearance of the points. First let's create some year categories to group by. We'll also turn this variable into a factor (category) and specify the order of the levels so our legend is in the correct order. ```r r <- mutate(r, Year_cat = case_when(Year < 2010 ~ "Pre 2010s", Year >= 2010 & Year <= 2015 ~ "2010-2015", Year > 2015 ~ ">2015"), Year_cat = factor(Year_cat, levels = c("Pre 2010s", "2010-2015", ">2015"))) ``` Note that we need to match the number of groups to the number of colours, shapes, etc. or supply only one value. For example, here we want all the points to be large and open, so we specify `point_size = 0.2` and `point_filled = FALSE`. ```r piper_plot(d = r, group = "Year_cat", point_colour = c("#21908C", "#440154", "#9AD93C"), point_shape = c("square", "triangle", "circle"), point_filled = FALSE, point_size = 0.2) ```
plot of chunk unnamed-chunk-7

plot of chunk unnamed-chunk-7

Hmm, those points are a bit tough to see ```r piper_plot(d = r, group = "Year_cat", point_colour = c("#21908C", "#440154", "#9AD93C"), point_shape = c("square", "triangle", "circle"), point_filled = TRUE, point_size = 0.2) ```
plot of chunk unnamed-chunk-8

plot of chunk unnamed-chunk-8

Perhaps with a bit of transparency? We can add this by adding two digits to the end of our colour codes. `"#21908C"` -> `"#21908C90`" adds a touch of transparency. (We can also omit `point_filled = TRUE` as points are filled by default.) ```r piper_plot(d = r, group = "Year_cat", point_colour = c("#21908C90", "#44015490", "#9AD93C90"), point_shape = c("square", "triangle", "circle"), point_size = 0.2) ```
plot of chunk unnamed-chunk-9

plot of chunk unnamed-chunk-9

Finally we can adjust the legend position and title ```r piper_plot(d = r, group = "Year_cat", point_colour = c("#21908C90", "#44015490", "#9AD93C90"), point_shape = c("square", "triangle", "circle"), point_size = 0.2, legend_position = "topright", legend_title = "Year") ```
plot of chunk unnamed-chunk-10

plot of chunk unnamed-chunk-10

We can put the legend anywhere we want. Think about the plot area as running from -1 to 1 from left to right (x) and -1 to 1 from top to bottom (y). We then specify the (x,y) coordinates that we want. You can even go out of the plotting area by specifying numbers out of the -1 to 1 range. ```r piper_plot(d = r, group = "Year_cat", point_colour = c("#21908C90", "#44015490", "#9AD93C90"), point_shape = c("square", "triangle", "circle"), point_size = 0.2, legend_position = c(0.6, 0.5), legend_title = "Year") ```
plot of chunk unnamed-chunk-11

plot of chunk unnamed-chunk-11