#' Customisable three-isotope plots.
#'
#' The function plots Pb isotope ratio data in a three-isotope diagram, calculates a regression line
#' and saves the plot in PDF, PNG and SVG formats.
#'
#' @import data.table
#' @import ggplot2
#' @import grid
#' @import scales
#' @import MASS
#' @import svglite
#'
#' @param data A data.table generated by \code{corr.mbf} function and subsetted to cointain only
#' samples to be plotted in the three-isotope diagram. Data should contain the columns
#' \code{Pb208207}, \code{Pb206207}, \code{Pb208206} and \code{Pb207206} with the measured Pb
#' isotope ratios for 208Pb/207Pb, 206Pb/207Pb, 208Pb/206Pb and 207Pb/206Pb, respectively.
#'
#' @param x,y The string \code{"Pb20x20y"} to be plotted on the x and y-axis. Default values are
#' \code{"Pb208207"} and \code{"Pb206207"}, respectively.
#'
#' @param factor A factor within the data.table that can be used to differentiate data points with
#' different colours. Default value is \code{NULL}.
#'
#' @param regression Logical. If \code{TRUE}, a regression line and its \eqn{R^2} value are added to
#' the plot. Default is \code{FALSE}.
#'
#' @param save Logical. If \code{TRUE}, the plot is saved in the "output" folder with the file name
#' "data_gen" in PDF, PNG and SVG formats.
#'
#' @return The function prints a three-isotope plot on screen. Additionally, if \code{save = TRUE},
#' three files named "data_gen" are saved in the "output" folder with the file name "data_gen" in
#' PDF, PNG and SVG formats.
#'
#' @examples
#' data(pm10nya)
#' isoplot_generic(pm10nya, save = FALSE)
#'
#' @seealso \code{\link{corr_mbf}} \code{\link{isoplot_pm10}}
#'
#' @export
isoplot_generic <- function(data,
x = "Pb208207",
y = "Pb206207",
factor = NULL,
regression = FALSE,
save = FALSE){
# Preparing axis labels
x.str <- as.character(x)
y.str <- as.character(y)
x.n <- gsub("[[:alpha:]]", "", x.str)
y.n <- gsub("[[:alpha:]]", "", y.str)
x.num <- gsub("([0-9]{3}).*", "\\1", x.n)
y.num <- gsub("([0-9]{3}).*", "\\1", y.n)
x.den <- gsub(".{3}([0-9]{3})", "\\1", x.n)
y.den <- gsub(".{3}([0-9]{3})", "\\1", y.n)
xlab <- bquote(phantom(.) ^ .(x.num) * Pb ~ "/" * phantom(.) ^ .(x.den) * Pb)
ylab <- bquote(phantom(.) ^ .(y.num) * Pb ~ "/" * phantom(.) ^ .(y.den) * Pb)
# Defining the plot
p <- ggplot(data, aes_string(x = x, y = y)) +
geom_point(aes_string(colour = factor)) +
# Limits of the plot and breaks
scale_x_continuous(breaks = pretty_breaks(n = 8)) +
scale_y_continuous(breaks = pretty_breaks(n = 6)) +
xlab(xlab) + ylab(ylab) +
theme_linedraw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.title = element_blank(),
legend.justification = c(0, 1),
legend.position = c(0.01, 0.99)
)
# Computing the regression line
if (regression == FALSE) {
p
} else {
yx <- as.formula(paste(y.str, x.str, sep = " ~ "))
print(yx)
lm.sum <- summary(lm(data = data, yx))
r2 <- lm.sum$r.squared
print(lm.sum)
r2.text <- sprintf("%.3f", round(r2, 3))
r2.note <- grobTree(textGrob(
bquote(R ^ 2 * "=" ~ .(r2.text)), x = 0.98, y = 0.1, hjust = 1
))
p <- p + geom_smooth(method = rlm, se = FALSE, colour = "black", size = 0.5) +
annotation_custom(r2.note)
}
if (save == TRUE) {
filename <- gsub("\\[.*?\\]", "\\1", deparse(substitute(data)))
filename <- gsub("\\.", "", filename)
if (dir.exists("output") == TRUE) {
} else {
dir.create("output")
}
ggsave(
filename = paste0("output/", filename, "_gen", ".pdf"),
plot = p, width = 13, height = 8.03, units = "cm", device = "pdf", dpi = 600)
ggsave(
filename = paste0("output/", filename, "_gen", ".png"),
plot = p, width = 13, height = 8.03, units = "cm", device = "png", dpi = 600)
ggsave(
filename = paste0("output/", filename, "_gen", ".svg"),
plot = p, width = 13, height = 8.03, units = "cm", device = "svg", dpi = 600)
p
} else {
p
}
}
# End of file
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.