Title: | Visualize and Report Soil Health Data |
---|---|
Description: | Collection of soil health data visualization and reporting tools, including a RStudio project template with everything you need to generate custom HTML and Microsoft Word reports for each participant in your soil health survey. |
Authors: | Jadey N Ryan [cre, aut] , Molly McIlquham [aut] , Kwabena A Sarpong [aut], Leslie M Michel [aut], Teal S Potter [aut] , Deirdre Griffin LaHue [aut] , Dani L Gelardi [aut] , Washington State Department of Agriculture [cph, fnd] |
Maintainer: | Jadey N Ryan <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0 |
Built: | 2024-11-05 22:23:50 UTC |
Source: | https://github.com/WA-Department-of-Agriculture/soils |
Add a legend to the texture triangle
add_legend( x = 1, y = 0.7, box = FALSE, legend = c("Your fields", "Same county", "Same crop", "Other fields"), color = c("#a60f2dCC", "#3E3D3D99", "#3E3D3D99", "#ccc29c80"), pch = c(15, 17, 18, 19), size = c(2.4, 2.16, 2.16, 1.36), vertical_spacing = 1.5, ... )
add_legend( x = 1, y = 0.7, box = FALSE, legend = c("Your fields", "Same county", "Same crop", "Other fields"), color = c("#a60f2dCC", "#3E3D3D99", "#3E3D3D99", "#ccc29c80"), pch = c(15, 17, 18, 19), size = c(2.4, 2.16, 2.16, 1.36), vertical_spacing = 1.5, ... )
x , y
|
X and Y coordinates used to position the legend. Location may also
be specified by setting |
box |
Boolean. |
legend |
Character vector to appear in legend. |
color |
Character vector of the color of the points. |
pch |
Numeric vector of plotting symbols. See |
size |
Numeric expansion factor for points. |
vertical_spacing |
Numeric spacing factor for vertical line distances between each legend item. |
... |
Other arguments passed to |
A list with list components for the legend's box and legend's text(s).
texture <- washi_data |> dplyr::select( sand = sand_percent, silt = silt_percent, clay = clay_percent ) make_texture_triangle(body_font = "sans") # Add gray points add_texture_points( tail(texture, 5), color = "#3E3D3D90", pch = 19 ) # Add red points add_texture_points( head(texture, 5), color = "#a60f2dCC", pch = 15 ) # Add legend add_legend( legend = c("Red squares", "Gray circles"), color = c("#a60f2dCC", "#3E3D3D90"), pch = c(15, 19), vertical_spacing = 2 ) # Note the text appears squished in this example since the width, height, # and resolution have been optimized to print the figure 6 in wide in the # report.
texture <- washi_data |> dplyr::select( sand = sand_percent, silt = silt_percent, clay = clay_percent ) make_texture_triangle(body_font = "sans") # Add gray points add_texture_points( tail(texture, 5), color = "#3E3D3D90", pch = 19 ) # Add red points add_texture_points( head(texture, 5), color = "#a60f2dCC", pch = 15 ) # Add legend add_legend( legend = c("Red squares", "Gray circles"), color = c("#a60f2dCC", "#3E3D3D90"), pch = c(15, 19), vertical_spacing = 2 ) # Note the text appears squished in this example since the width, height, # and resolution have been optimized to print the figure 6 in wide in the # report.
To vary color, symbol, and size of points by a grouping variable, call this function once for each value of the grouping variable. Add layers from bottom to top. The below example adds the red points last so they are plotted on top of the gray points.
add_texture_points( texture_df = NULL, color = "#a60f2dCC", pch = 19, size = 1.5, ... )
add_texture_points( texture_df = NULL, color = "#a60f2dCC", pch = 19, size = 1.5, ... )
texture_df |
Data frame or matrix where each row is a soil sample and three numeric columns contain sand, silt, and clay percentages or proportions. The order of sand, silt, clay is required for correct plotting. |
color |
Color of the points. Defaults to WaSHI red. |
pch |
Numeric value of plotting symbol. See |
size |
Numeric expansion factor for points. Defaults to 1.5. |
... |
Other arguments passed to |
A list of x, y coordinates of the soil textures plotted.
Adapted from plotrix: https://github.com/plotrix/plotrix/blob/0d4c2b065e2c2d327358ac8cdc0b0d46b89bea7f/R/soil.texture.R
texture <- soils::washi_data |> dplyr::select( sand = sand_percent, silt = silt_percent, clay = clay_percent ) make_texture_triangle(body_font = "sans") # Add gray points add_texture_points( tail(texture, 5), color = "#3E3D3D90", pch = 19 ) # Add red points add_texture_points( head(texture, 5), color = "#a60f2dCC", pch = 15 ) # Note the text appears squished in this example since the width, height, # and resolution have been optimized to print the figure 6 in wide in the # report.
texture <- soils::washi_data |> dplyr::select( sand = sand_percent, silt = silt_percent, clay = clay_percent ) make_texture_triangle(body_font = "sans") # Add gray points add_texture_points( tail(texture, 5), color = "#3E3D3D90", pch = 19 ) # Add red points add_texture_points( head(texture, 5), color = "#a60f2dCC", pch = 15 ) # Note the text appears squished in this example since the width, height, # and resolution have been optimized to print the figure 6 in wide in the # report.
Calculate the mode of categorical variable
calculate_mode(x)
calculate_mode(x)
x |
Character vector to calculate mode from. |
The value that occurred most often.
calculate_mode(washi_data$crop)
calculate_mode(washi_data$crop)
ggplot2
plot to an interactive ggiraph
Convert a ggplot2
plot to an interactive ggiraph
convert_ggiraph(plot, ..., body_font = "Poppins", width = 6, height = 4)
convert_ggiraph(plot, ..., body_font = "Poppins", width = 6, height = 4)
plot |
|
... |
Other arguments passed to |
body_font |
Font family to use throughout plot. Defaults to
|
width |
Width of SVG output in inches. Defaults to 6. |
height |
Height of SVG output in inches. Defaults to 4. |
Facetted strip plots with classes of girafe
and htmlwidget
.
# Read in wrangled example plot data df_plot_path <- soils_example("df_plot.RDS") df_plot <- readRDS(df_plot_path) # Make strip plot with all measurements and set scales based on # the category column and then apply theme. # Subset df to just biological measurement group df_plot_bio <- df_plot |> dplyr::filter(measurement_group == "biological") # NOTE: the plot gets piped into the `set_scales()` function, which gets # added to `theme_facet_strip()`. plot <- make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales() + theme_facet_strip(body_font = "sans") # Convert static plot to interactive `ggiraph` convert_ggiraph(plot)
# Read in wrangled example plot data df_plot_path <- soils_example("df_plot.RDS") df_plot <- readRDS(df_plot_path) # Make strip plot with all measurements and set scales based on # the category column and then apply theme. # Subset df to just biological measurement group df_plot_bio <- df_plot |> dplyr::filter(measurement_group == "biological") # NOTE: the plot gets piped into the `set_scales()` function, which gets # added to `theme_facet_strip()`. plot <- make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales() + theme_facet_strip(body_font = "sans") # Convert static plot to interactive `ggiraph` convert_ggiraph(plot)
Creates an RStudio project containing Quarto template and resources (images, style sheets, render.R script).
create_soils(path, template = "English", overwrite = FALSE, open = TRUE)
create_soils(path, template = "English", overwrite = FALSE, open = TRUE)
path |
Name of project directory to be created. |
template |
Template type. Either "English" or "Spanish". |
overwrite |
Boolean. Overwrite the existing project? |
open |
Boolean. Open the newly created project? |
A new project directory containing template and resources.
Adapted from golem::create_golem()
.
## Not run: # Create temporary directory dir <- tempdir() # Create soils project create_soils(dir, overwrite = TRUE) # Delete temporary directory unlink(dir, recursive = TRUE) ## End(Not run)
## Not run: # Create temporary directory dir <- tempdir() # Create soils project create_soils(dir, overwrite = TRUE) # Delete temporary directory unlink(dir, recursive = TRUE) ## End(Not run)
An example data dictionary for the Washington Soil Health Initiative (WaSHI) State of the Soils Assessment anonymized data.
data_dictionary
data_dictionary
data_dictionary
A data frame with 32 rows and 7 columns.Name to group measurements by
Label of measurement group to be used as heading
Name of column in data set, used for joining
Order of how measurements are presented within each measurement_group
Abbreviated measurement name for labels
Measurement unit
HTML formatted abbreviation with unit for plots and tables
...
Color the background cells based on how the value compares to the project average. The project average must be the last row of the table. A footnote is added to the table describing what the dark and light colors mean.
format_ft_colors( ft, lighter_color = "#F2F0E6", darker_color = "#ccc29c", language = "English" )
format_ft_colors( ft, lighter_color = "#F2F0E6", darker_color = "#ccc29c", language = "English" )
ft |
Flextable object |
lighter_color |
Lighter background color. Defaults to WaSHI cream. |
darker_color |
Darker background color. Defaults to WaSHI tan. |
language |
Language of the footnote. "English" (default) or "Spanish". |
# Read in wrangled example table data tables_path <- soils_example("tables.RDS") tables <- readRDS(tables_path) # Make the table ft <- flextable::flextable(tables$biological) ft # Conditionally format background cell colors format_ft_colors(ft)
# Read in wrangled example table data tables_path <- soils_example("tables.RDS") tables <- readRDS(tables_path) # Make the table ft <- flextable::flextable(tables$biological) ft # Conditionally format background cell colors format_ft_colors(ft)
This function is used in summarize_by_var
.
get_n_texture_by_var(results_long, producer_info, var)
get_n_texture_by_var(results_long, producer_info, var)
results_long |
Dataframe in tidy, long format with columns: |
producer_info |
Vector of producer's values for the grouping variable. |
var |
Variable to group and summarize by. |
This function uses the data dictionary to create a new dataframe of the abbreviations and units for each measurement group for flextable
get_table_headers(dictionary, group)
get_table_headers(dictionary, group)
dictionary |
Dataframe containing columns |
group |
Character |
Make a flextable with column names from another dataframe
make_ft(table, header)
make_ft(table, header)
table |
A dataframe with the contents of the desired flextable output. |
header |
Another dataframe with three columns:
|
Formatted flextable object.
# Read in wrangled table data headers_path <- soils_example("headers.RDS") headers <- readRDS(headers_path) tables_path <- soils_example("tables.RDS") tables <- readRDS(tables_path) # Input dataframes headers$chemical tables$chemical # Make the flextable make_ft( table = tables$chemical, header = headers$chemical ) |> # Style the flextable style_ft() |> # Add the white line under the columns with the same units unit_hline(header = headers$chemical)
# Read in wrangled table data headers_path <- soils_example("headers.RDS") headers <- readRDS(headers_path) tables_path <- soils_example("tables.RDS") tables <- readRDS(tables_path) # Input dataframes headers$chemical tables$chemical # Make the flextable make_ft( table = tables$chemical, header = headers$chemical ) |> # Style the flextable style_ft() |> # Add the white line under the columns with the same units unit_hline(header = headers$chemical)
Make leaflet map
make_leaflet(df, primary_color = "#a60f2d")
make_leaflet(df, primary_color = "#a60f2d")
df |
Dataframe containing columns: |
primary_color |
Color of points. Defaults to WaSHI red. |
Leaflet map.
JavaScript code adapted from leaflet.extras.
gis_df <- washi_data |> dplyr::distinct(latitude, longitude, .keep_all = TRUE) |> head(3) |> prep_for_map(label_heading = field_name, label_body = crop) dplyr::glimpse(gis_df) # Make leaflet make_leaflet(gis_df)
gis_df <- washi_data |> dplyr::distinct(latitude, longitude, .keep_all = TRUE) |> head(3) |> prep_for_map(label_heading = field_name, label_body = crop) dplyr::glimpse(gis_df) # Make leaflet make_leaflet(gis_df)
Make a facetted strip plot
make_strip_plot( df, ..., x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, language = "English" )
make_strip_plot( df, ..., x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, language = "English" )
df |
Data frame to plot. |
... |
Other arguments passed to |
x |
Column for x-axis. For these strip plots, we recommend using a dummy
variable to act as a placeholder. Defaults to a column named |
y |
Column for y-axis. Defaults to |
id |
Column with unique identifiers for each sample to use as |
group |
Column to facet by. Defaults to |
tooltip |
Column with tooltip labels for interactive plots. |
language |
Language of the footnote. "English" (default) or "Spanish". |
Facetted ggplot2
strip plots.
# Read in wrangled example plot data df_plot_path <- soils_example("df_plot.RDS") df_plot <- readRDS(df_plot_path) # Subset df to just biological measurement group df_plot_bio <- df_plot |> dplyr::filter(measurement_group == "biological") # Make strip plot with all measurements and set scales based on # the category column and then apply theme. # NOTE: the plot gets piped into the `set_scales()` function, which gets # added to `theme_facet_strip()`. make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales() + theme_facet_strip(body_font = "sans") # Example of strip plot without scales or theme functions make_strip_plot(df_plot_bio) # Example of strip plot with `x` set to the facet group instead of a # dummy variable. make_strip_plot( df_plot_bio, x = abbr_unit, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales() + theme_facet_strip(body_font = "sans")
# Read in wrangled example plot data df_plot_path <- soils_example("df_plot.RDS") df_plot <- readRDS(df_plot_path) # Subset df to just biological measurement group df_plot_bio <- df_plot |> dplyr::filter(measurement_group == "biological") # Make strip plot with all measurements and set scales based on # the category column and then apply theme. # NOTE: the plot gets piped into the `set_scales()` function, which gets # added to `theme_facet_strip()`. make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales() + theme_facet_strip(body_font = "sans") # Example of strip plot without scales or theme functions make_strip_plot(df_plot_bio) # Example of strip plot with `x` set to the facet group instead of a # dummy variable. make_strip_plot( df_plot_bio, x = abbr_unit, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales() + theme_facet_strip(body_font = "sans")
Make a texture triangle with USDA textural classes.
make_texture_triangle( body_font = "Poppins", show_names = TRUE, show_lines = TRUE, show_grid = FALSE )
make_texture_triangle( body_font = "Poppins", show_names = TRUE, show_lines = TRUE, show_grid = FALSE )
body_font |
Font family to use throughout plot. Defaults to |
show_names |
Boolean. Defaults to |
show_lines |
Boolean. Defaults to |
show_grid |
Boolean. Defaults to |
Opens the graphics device with a triangle plot containing USDA textural classes.
Adapted from plotrix: https://github.com/plotrix/plotrix/blob/0d4c2b065e2c2d327358ac8cdc0b0d46b89bea7f/R/soil.texture.R
# Note the text appears squished in this example since the width, height, # and resolution have been optimized to print the figure 6 in wide in the # report. make_texture_triangle(body_font = "sans")
# Note the text appears squished in this example since the width, height, # and resolution have been optimized to print the figure 6 in wide in the # report. make_texture_triangle(body_font = "sans")
Prep data to gis df
prep_for_map(df, label_heading, label_body)
prep_for_map(df, label_heading, label_body)
df |
Dataframe containing columns: |
label_heading |
Column in |
label_body |
Column in |
Dataframe to be input into make_leaflet()
.
washi_data |> dplyr::distinct(latitude, longitude, .keep_all = TRUE) |> head(3) |> prep_for_map(label_heading = field_name, label_body = crop) |> dplyr::glimpse()
washi_data |> dplyr::distinct(latitude, longitude, .keep_all = TRUE) |> head(3) |> prep_for_map(label_heading = field_name, label_body = crop) |> dplyr::glimpse()
Pull unique values from one column of dataframe
pull_unique(df, target)
pull_unique(df, target)
df |
Dataframe with column to extract unique values from. |
target |
Variable to pull unique vector of (i.e. crop or county). |
Vector of unique values from target column.
washi_data |> pull_unique(crop)
washi_data |> pull_unique(crop)
Define styles for producer's samples versus all samples
set_scales( plot, primary_color = "#a60f2d", secondary_color = "#3E3D3D", other_color = "#ccc29c", language = "English" )
set_scales( plot, primary_color = "#a60f2d", secondary_color = "#3E3D3D", other_color = "#ccc29c", language = "English" )
plot |
|
primary_color |
Color of producer's sample points. Defaults to WaSHI red |
secondary_color |
Color of sample points with |
other_color |
Color of sample points with |
language |
Language of the footnote. "English" (default) or "Spanish". |
ggplot
object with manual alpha, color, shape, and size scales
applied.
# Read in wrangled example plot data df_plot_path <- soils_example("df_plot.RDS") df_plot <- readRDS(df_plot_path) # Subset df to just biological measurement group df_plot_bio <- df_plot |> dplyr::filter(measurement_group == "biological") # Make strip plot make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales() + theme_facet_strip(body_font = "sans") # Example without setting scales make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label ) + theme_facet_strip(body_font = "sans")
# Read in wrangled example plot data df_plot_path <- soils_example("df_plot.RDS") df_plot <- readRDS(df_plot_path) # Subset df to just biological measurement group df_plot_bio <- df_plot |> dplyr::filter(measurement_group == "biological") # Make strip plot make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales() + theme_facet_strip(body_font = "sans") # Example without setting scales make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label ) + theme_facet_strip(body_font = "sans")
soils
comes bundled with some example files in its inst/extdata
directory. This function make them easy to access.
soils_example(file = NULL)
soils_example(file = NULL)
file |
Name of file. If |
Adapted from readxl::readxl_example()
.
soils_example() soils_example("df_plot.RDS")
soils_example() soils_example("df_plot.RDS")
Style a flextable
style_ft( ft, header_font = "Lato", body_font = "Poppins", header_color = "#023B2C", header_text_color = "white", border_color = "#3E3D3D" )
style_ft( ft, header_font = "Lato", body_font = "Poppins", header_color = "#023B2C", header_text_color = "white", border_color = "#3E3D3D" )
ft |
Flextable object. |
header_font |
Font of header text. Defaults to |
body_font |
Font of body text. Defaults to |
header_color |
Background color of header cells. Defaults to WaSHI green. |
header_text_color |
Color of header text. Defaults to white. |
border_color |
Color of border lines. Defaults to WaSHI gray. |
Styled flextable object.
# Read in wrangled example table data tables_path <- soils_example("tables.RDS") tables <- readRDS(tables_path) # Make the table ft <- flextable::flextable(tables$biological) ft # Style the table style_ft(ft)
# Read in wrangled example table data tables_path <- soils_example("tables.RDS") tables <- readRDS(tables_path) # Make the table ft <- flextable::flextable(tables$biological) ft # Style the table style_ft(ft)
Summarize samples across the project
summarize_by_project(results_long)
summarize_by_project(results_long)
results_long |
Dataframe in tidy, long format with columns: |
Summarize producer's samples with averages by grouping variable
summarize_by_var(results_long, producer_samples, var)
summarize_by_var(results_long, producer_samples, var)
results_long |
Dataframe in tidy, long format with columns: |
producer_samples |
Dataframe in tidy, long format with columns:
|
var |
Variable to summarize by. |
Theme for facetted strip plots
theme_facet_strip( ..., body_font = "Poppins", strip_color = "#335c67", strip_text_color = "white" )
theme_facet_strip( ..., body_font = "Poppins", strip_color = "#335c67", strip_text_color = "white" )
... |
Other arguments to pass into |
body_font |
Font family to use throughout plot. Defaults to |
strip_color |
Color of facet strip background. Defaults to WaSHI blue. |
strip_text_color |
Color of facet strip text. Defaults to white. |
# Read in wrangled example plot data df_plot_path <- soils_example("df_plot.RDS") df_plot <- readRDS(df_plot_path) # Subset df to just biological measurement group df_plot_bio <- df_plot |> dplyr::filter(measurement_group == "biological") # Make strip plot with all measurements and set scales based on # the category column and then apply theme. # NOTE: the plot gets piped into the `set_scales()` function, which gets # added to `theme_facet_strip()`. make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales() + theme_facet_strip(body_font = "sans") # Example without setting theme make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales()
# Read in wrangled example plot data df_plot_path <- soils_example("df_plot.RDS") df_plot <- readRDS(df_plot_path) # Subset df to just biological measurement group df_plot_bio <- df_plot |> dplyr::filter(measurement_group == "biological") # Make strip plot with all measurements and set scales based on # the category column and then apply theme. # NOTE: the plot gets piped into the `set_scales()` function, which gets # added to `theme_facet_strip()`. make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales() + theme_facet_strip(body_font = "sans") # Example without setting theme make_strip_plot( df_plot_bio, x = dummy, y = value, id = sample_id, group = abbr_unit, tooltip = label, color = category, size = category, alpha = category, shape = category ) |> set_scales()
Use when columns with the same units are merged together to add a bottom border to make it more obvious those columns share units.
unit_hline(ft, header)
unit_hline(ft, header)
ft |
flextable object |
header |
Another dataframe with three columns:
|
Flextable object with bottom borders added.
# Read in wrangled table data headers_path <- soils_example("headers.RDS") headers <- readRDS(headers_path) tables_path <- soils_example("tables.RDS") tables <- readRDS(tables_path) # Input dataframes headers$chemical tables$chemical # Make the flextable make_ft( table = tables$chemical, header = headers$chemical ) |> # Style the flextable style_ft() |> # Add the white line under the columns with the same units unit_hline(header = headers$chemical) # Example without `unit_hline()` make_ft( table = tables$chemical, header = headers$chemical ) |> # Style the flextable style_ft()
# Read in wrangled table data headers_path <- soils_example("headers.RDS") headers <- readRDS(headers_path) tables_path <- soils_example("tables.RDS") tables <- readRDS(tables_path) # Input dataframes headers$chemical tables$chemical # Make the flextable make_ft( table = tables$chemical, header = headers$chemical ) |> # Style the flextable style_ft() |> # Add the white line under the columns with the same units unit_hline(header = headers$chemical) # Example without `unit_hline()` make_ft( table = tables$chemical, header = headers$chemical ) |> # Style the flextable style_ft()
A subset of the Washington Soil Health Initiative (WaSHI) State of the Soils Assessment anonymized data. This data set presents each sample in its own row, with columns for each measurement.
washi_data
washi_data
washi_data
A data frame with 100 rows and 42 columns:Year of sample
Anonymized IDs
Anonymized names
Truncated coordinates
Measured soil texture
Column name includes measurement and units; value is the measurement result
...