--- title: "Customising Plots" author: "ssdtools Team" date: '`r format(Sys.time(), "%Y-%m-%d", tz = "UTC")`' bibliography: references.bib mathfont: Courier latex_engine: MathJax output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Customising Plots} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4 ) ``` ## Plotting the cumulative distributions The `ssdtools` package plots the cumulative distribution functions using `ssd_plot_cdf()`. For example, consider the CCME boron data from the [`ssddata`](https://github.com/open-AIMS/ssddata) package. We can fit, and then plot the cdfs as follows. ```{r warning=FALSE, message=FALSE, fig.alt="A plot of the CCME boron data with the six default distributions."} library(ssdtools) library(ggplot2) fits <- ssd_fit_dists(ssddata::ccme_boron) ssd_plot_cdf(fits) ``` This graphic is a [ggplot](https://ggplot2.tidyverse.org) object and so can be customized in the usual way. For example, we can add the model-averaged cdf by setting `average = NA`, change the string used to separate thousands using `big.mark`, customize the color scale with `scale_color_manual()` and change the theme. ```{r, fig.alt="A plot of the CCME boron data with the model average distribution and the six default distributions."} ssd_plot_cdf(fits, average = NA, big.mark = " ") + scale_color_manual(name = "Distribution", breaks = c("average", names(fits)), values = 1:7) + theme_bw() ``` ## ggplot Geoms The `ssdtools` package provides four ggplot geoms to allow you construct your own plots. ### `geom_ssdpoint()` The first is `geom_ssdpoint()` which plots species sensitivity data ```{r, fig.alt = "A plot of the CCME boron data."} ggplot(ssddata::ccme_boron) + geom_ssdpoint(aes(x = Conc)) + ylab("Probability density") + xlab("Concentration") ``` ### `geom_ssdsegments()` The second is `geom_ssdsegments()` which plots the ranges of censored species sensitivity data ```{r, fig.alt = "A plot of CCME boron data with the ranges of the censored data indicated by horizontal lines."} ggplot(ssddata::ccme_boron) + geom_ssdsegment(aes(x = Conc, xend = Conc * 4)) + ylab("Probability density") + xlab("Concenration") ``` ### `geom_xribbon()` The third is `geom_xribbon()` which plots species sensitivity confidence intervals ```{r, fig.alt="A plot of the confidence intervals for the CCME boron data."} ggplot(boron_pred) + geom_xribbon(aes(xmin = lcl, xmax = ucl, y = proportion)) + ylab("Probability density") + xlab("Concenration") ``` ### `geom_hcintersect()` And the fourth is `geom_hcintersect()` which plots hazard concentrations ```{r, fig.alt="A plot of hazard concentrations as dotted lines."} ggplot() + geom_hcintersect(xintercept = c(1, 2, 3), yintercept = c(0.05, 0.1, 0.2)) + ylab("Probability density") + xlab("Concenration") ``` ### Putting it together Geoms can be combined as follows ```{r, fig.alt="A plot of censored CCME boron data with confidence intervals"} gp <- ggplot(boron_pred, aes(x = est)) + geom_xribbon(aes(xmin = lcl, xmax = ucl, y = proportion), alpha = 0.2) + geom_line(aes(y = proportion)) + geom_ssdsegment(data = ssddata::ccme_boron, aes(x = Conc / 2, xend = Conc * 2)) + geom_ssdpoint(data = ssddata::ccme_boron, aes(x = Conc / 2)) + geom_ssdpoint(data = ssddata::ccme_boron, aes(x = Conc * 2)) + scale_y_continuous("Species Affected (%)", labels = scales::percent) + xlab("Concentration (mg/L)") + expand_limits(y = c(0, 1)) gp ``` To log the x-axis and include mathematical notation and add the HC5 value use the following code. ```{r, fig.alt="A plot of censored CCME boron data on log scale with confidence intervals, mathematical and the 5% hazard concentration."} gp + scale_x_log10( latex2exp::TeX("Boron $(\\mu g$/L)$") ) + geom_hcintersect(xintercept = ssd_hc(fits)$est, yintercept = 0.05) ``` ## Saving plots The most recent plot can be saved as a file using `ggsave()`, which also allows the user to set the resolution. ```{r, eval = FALSE} ggsave("file_name.png", dpi = 300) ```
```{r, results = "asis", echo = FALSE} cat(ssdtools::ssd_licensing_md()) ```