#' Plot of conditioned LOD scores against index
#'
#' Plot LOD statistics calculated by [mediation_scan()] against index
#' using ggplot2.
#'
#' @param x mediation object
#' @param col color of points (default "firebrick4")
#' @param cex character expansion (default 2)
#' @param xlab,ylab X and Y axis label (default `index_name` and "Conditioned LOD")
#' @param col_target color for target LOD line
#' @param gap gap between facets (default `25`)
#'
#' @export
#' @importFrom ggplot2 aes autoplot element_blank element_rect facet_grid geom_hline geom_point ggplot theme
#' @importFrom grid unit
#' @importFrom dplyr arrange_at
#' @rdname mediation_scan
ggplot_mediation_scan <- function(x,
col="firebrick4",
cex = 1,
xlab = index_name,
ylab = "Conditioned LOD",
col_target = "blue",
gap = 25) {
facet_name <- attr(x, "facet_name")
index_name <- attr(x, "index_name")
if(index_name != "index" & "index" %in% names(x)) {
# Make sure we don't clash with column named index.
x$index <- NULL
}
if(!is.factor(x[[facet_name]]))
x[[facet_name]] <- factor(x[[facet_name]], unique(x[[facet_name]]))
x <- dplyr::rename(x, index = index_name)
x <- dplyr::arrange_at(x, c(facet_name, "index"))
p <- ggplot2::ggplot(x) +
ggplot2::aes(index, lod, symbol = symbol) +
ggplot2::facet_grid(formula(paste("~", facet_name)),
scales = "free_x", space = "free") +
ggplot2::xlab(xlab)
if(!is.null(x$col)) {
p <- p +
ggplot2::aes(col = col) +
ggplot2::geom_point(alpha = 0.5)
} else {
p <- p +
ggplot2::geom_point(col = col, alpha = 0.5)
}
# gap between facets
p <- p +
ggplot2::theme(panel.spacing = grid::unit(gap / 10000, "npc")) +
ggplot2::theme(
panel.border = ggplot2::element_rect(colour = "black",
fill=NA))
# X axis
if(length(unique(x[[facet_name]])) > 1) {
p <- p +
ggplot2::theme(
axis.text.x = ggplot2::element_blank(),
axis.ticks.x = ggplot2::element_blank())
}
targetFit <- attr(x, "targetFit")
if(!is.null(targetFit))
p <- p + ggplot2::geom_hline(yintercept = targetFit, col = col_target)
p
}
#' @export
#' @rdname mediation_scan
#'
autoplot.mediation_scan <- function(x, ...) {
ggplot_mediation_scan(x, ...)
}
#' @export
#' @rdname mediation_scan
#'
plot.mediation_scan <- function(x, ...) {
ggplot_mediation_scan(x, ...)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.