diff --git a/NAMESPACE b/NAMESPACE index deac2af..cb0b83a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,5 +18,6 @@ export(purpleair_api) export(save_figure) export(swap) export(taylor_diagram) +export(tile_plot) export(wind_rose) importFrom(rlang,.data) diff --git a/R/plotting_tile_plot.R b/R/plotting_tile_plot.R index 2584db5..422081c 100644 --- a/R/plotting_tile_plot.R +++ b/R/plotting_tile_plot.R @@ -1,7 +1,50 @@ # TODO: add option to display n obs inside each cell # TODO: handle named periods instead of integers for (month, quarter, wday) # TODO: handle timezones? -tile_plot <- function(obs, x, y, z, FUN = mean, date_col = "date_utc", ...) { +# TODO: handle facets +#' Create tiled summary diagrams to assess relationships in a variable based on two grouping variables +#' +#' @param obs Observation data.frame with (at least) all columns in `data_cols` and (if provided) `facet_by`. +#' @param x,y,z charactor values indicating column names in `obs` to summarise (`x` and `y`) values (`z`) by using `FUN`. +#' If `x` or `y` are one of +#' `"year", "quarter", "month", "day", "wday", "hour", "minute", "second"`, +#' and those columns are not present in `obs` then they will be calulcated based on `date_col` +#' @param date_col (Optional) a single charactor value indicating the column name in `obs` containing observation dates. +#' Default assume "date_utc" exists. +#' @param facet_by (Optional) a character vector with 1 or 2 column names to use as facets in `ggplot2::facet_wrap()`. +#' If names are present they will be used as the corresponding facet titles. +#' Default (NULL) does not facet the plot. +#' @param facet_rows (Optional) a single numeric value indicating the number of rows to use in facetting if `facet_by` values provided. +#' Default is a single row. +#' @param FUN (Optional) a function to use to summarise `z` values - must take a vector of values as it's first argument and return a single value. +#' Default is to calculate the `mean` value. +#' @param ... Any other named arguments will be passed on to `FUN()` when summarizing `z` values. +#' @description +#' TODO: Add description +#' @return +#' A ggplot object of your tile plot. +#' @family Data Visualisation +#' +#' @export +#' @examples +#' \dontrun{ +#' # Make test data +#' date_range <- lubridate::ymd_h(c("2019-02-01 00", "2019-03-28 23"), tz = "America/Vancouver") +#' obs <- get_station_data("Vanderhoof, BC, Canada", date_range, sources = "BCgov")$data |> +#' dplyr::select("date_utc", "site_id", "pm25_1hr_ugm3") |> +#' dplyr::distinct() +#' # Basic usage +#' gg <- tile_plot(obs, x = "day", y = "hour", z = "p,25_1hr_ugm3") +#' # Change titles +#' gg + ggplot2::labs( +#' fill = "Legend Title", title = "Plot Title", +#' subtitle = "Plot Subtitle", caption = "Plot Caption" +#' ) +#' +#' # Save plot +#' # save_figure(gg, "./test.png") +#' } +tile_plot <- function(obs, x, y, z, date_col = "date_utc", facet_by = NULL, facet_rows = 1, FUN = mean, ...) { # Handle date-based grouping options special_cases <- c( "year", "quarter", "month", "day", "wday", @@ -22,7 +65,7 @@ tile_plot <- function(obs, x, y, z, FUN = mean, date_col = "date_utc", ...) { dplyr::summarise(z = get(z) |> FUN(...), .groups = "drop") |> dplyr::mutate(dplyr::across(c(x, y), factor)) |> tidyr::complete(x, y) - + # Make gg tile plot with good defaults pd |> ggplot2::ggplot() + diff --git a/man/get_abgov_stations.Rd b/man/get_abgov_stations.Rd index 4f612d0..9faac98 100644 --- a/man/get_abgov_stations.Rd +++ b/man/get_abgov_stations.Rd @@ -4,7 +4,7 @@ \alias{get_abgov_stations} \title{Download air quality station metadata from the Alberta (Canada) Government} \usage{ -get_abgov_stations(use_sf = FALSE) +get_abgov_stations(use_sf = FALSE, ...) } \arguments{ \item{use_sf}{(Optional) a single logical (TRUE/FALSE) value indicating whether or not to return a spatial object. using the `sf` package} diff --git a/man/save_figure.Rd b/man/save_figure.Rd index f88ec15..e6a3fc6 100644 --- a/man/save_figure.Rd +++ b/man/save_figure.Rd @@ -40,6 +40,7 @@ gg <- ggplot2::ggplot() + \seealso{ Other Data Visualisation: \code{\link{taylor_diagram}()}, +\code{\link{tile_plot}()}, \code{\link{wind_rose}()} } \concept{Data Visualisation} diff --git a/man/taylor_diagram.Rd b/man/taylor_diagram.Rd index 7890a7c..afddf09 100644 --- a/man/taylor_diagram.Rd +++ b/man/taylor_diagram.Rd @@ -168,6 +168,7 @@ taylor_diagram(data, \seealso{ Other Data Visualisation: \code{\link{save_figure}()}, +\code{\link{tile_plot}()}, \code{\link{wind_rose}()} } \concept{Data Visualisation} diff --git a/man/tile_plot.Rd b/man/tile_plot.Rd new file mode 100644 index 0000000..d644dc0 --- /dev/null +++ b/man/tile_plot.Rd @@ -0,0 +1,73 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plotting_tile_plot.R +\name{tile_plot} +\alias{tile_plot} +\title{Create tiled summary diagrams to assess relationships in a variable based on two grouping variables} +\usage{ +tile_plot( + obs, + x, + y, + z, + date_col = "date_utc", + facet_by = NULL, + facet_rows = 1, + FUN = mean, + ... +) +} +\arguments{ +\item{obs}{Observation data.frame with (at least) all columns in `data_cols` and (if provided) `facet_by`.} + +\item{x, y, z}{charactor values indicating column names in `obs` to summarise (`x` and `y`) values (`z`) by using `FUN`. +If `x` or `y` are one of +`"year", "quarter", "month", "day", "wday", "hour", "minute", "second"`, +and those columns are not present in `obs` then they will be calulcated based on `date_col`} + +\item{date_col}{(Optional) a single charactor value indicating the column name in `obs` containing observation dates. +Default assume "date_utc" exists.} + +\item{facet_by}{(Optional) a character vector with 1 or 2 column names to use as facets in `ggplot2::facet_wrap()`. +If names are present they will be used as the corresponding facet titles. +Default (NULL) does not facet the plot.} + +\item{facet_rows}{(Optional) a single numeric value indicating the number of rows to use in facetting if `facet_by` values provided. +Default is a single row.} + +\item{FUN}{(Optional) a function to use to summarise `z` values - must take a vector of values as it's first argument and return a single value. +Default is to calculate the `mean` value.} + +\item{...}{Any other named arguments will be passed on to `FUN()` when summarizing `z` values.} +} +\value{ +A ggplot object of your tile plot. +} +\description{ +TODO: Add description +} +\examples{ +\dontrun{ +# Make test data +date_range <- lubridate::ymd_h(c("2019-02-01 00", "2019-03-28 23"), tz = "America/Vancouver") +obs <- get_station_data("Vanderhoof, BC, Canada", date_range, sources = "BCgov")$data |> + dplyr::select("date_utc", "site_id", "pm25_1hr_ugm3") |> + dplyr::distinct() +# Basic usage +gg <- tile_plot(obs, x = "day", y = "hour", z = "p,25_1hr_ugm3") +# Change titles +gg + ggplot2::labs( + fill = "Legend Title", title = "Plot Title", + subtitle = "Plot Subtitle", caption = "Plot Caption" +) + +# Save plot +# save_figure(gg, "./test.png") +} +} +\seealso{ +Other Data Visualisation: +\code{\link{save_figure}()}, +\code{\link{taylor_diagram}()}, +\code{\link{wind_rose}()} +} +\concept{Data Visualisation} diff --git a/man/wind_rose.Rd b/man/wind_rose.Rd index c998987..7ca1ab2 100644 --- a/man/wind_rose.Rd +++ b/man/wind_rose.Rd @@ -22,8 +22,8 @@ wind_rose( \arguments{ \item{obs}{Observation data.frame with (at least) all columns in `data_cols` and (if provided) `facet_by`.} -\item{data_cols}{(Optional) a character vector with 2 values indication column names in `obs` to get observed and modelled values. -Default assumes columns "obs" and "mod" exist.} +\item{data_cols}{(Optional) a character vector with 2 values indicating column names in `obs` to get wind speed (ws) and wind direction (wd) values. +Default assumes columns "ws_1hr_ms" and "wd_1hr_degrees" exist.} \item{facet_by}{(Optional) a character vector with 1 or 2 column names to use as facets in `ggplot2::facet_wrap()`. If names are present they will be used as the corresponding facet titles. @@ -85,6 +85,7 @@ gg + ggplot2::labs( \seealso{ Other Data Visualisation: \code{\link{save_figure}()}, -\code{\link{taylor_diagram}()} +\code{\link{taylor_diagram}()}, +\code{\link{tile_plot}()} } \concept{Data Visualisation}