#' Significance Markers for ggplot2
#' @description Apply significance markers onto an existing \link[ggplot2]{ggplot2} chart
#' @param tbl_sig A df object created from \link[ggsigmark]{tbl_sig} functions.
#' @param x A quoted character string of the variable from the `tbl_sig` df charted on the x-axis. Typically this is "wave_pos".
#' @param y A quoted character string of the variable from the `tbl_sig` df charted on the y-axis. Typically this is "result_pos".
#' @param group An optional quoted character string of the variable from the `tbl_sig` df that is grouped. If needed, it likely needs to be "group".
#' @param direction A quoted character string of the variable from the `tbl_sig` df indicating whether the change from one wave to the next is up or down. Typically this is "direction".
#' @param labels A quoted character string vector indicating how the sig. test markers should be labeled in the legend. By default, the labels are "Increase" and "Decrease".
#' @param colour A quoted character string of the variable from the `tbl_sig` df that applies the subgroup colours. Typically this is "level".
#' @param fill A quoted character string of the variable from the `tbl_sig` df that applies the subgroup colours. Typically this is "level".
#' @param icon A quoted character string indicating the type of icon to display to denote statistical significance. Default is "full triangle".
#' @param size A numeric value indicating the size of the icon. Default is set to 5.
#'
#' @details This is essentially a wrapper for \link[ggplot2]{geom_point}
#'
#' @note The icon argument accepts one of the following strings:
#' \itemize{
#' \item full triangle
#' \item clear triangle
#' \item arrow
#' \item finger
#'}
#' @export
#'
#' @examples
#' ## Proportions
#' gss_data1 <- dplyr::filter(gss_data, year_num >= 2000, !is.na(confinan))
#' gss_data1$year <- droplevels(gss_data1$year)
#' my_results <- freq_prop_test(gss_data1, "confinan", "year", weight = "wtssall")
#' sig <- tbl_sig(my_results, "year", space_label = 0.1, space_between = 0.2, compare = "w2w")
#' chart <- tbl_chart(gss_data1, "confinan", "year", weight = "wtssall")
#'
#' library(ggplot2)
#' ggplot() +
#' geom_line(data = chart, aes(x = year, y = prop, colour = confinan, group = confinan)) +
#' geom_sigmark_waves(sig)
geom_sigmark_waves <- function(tbl_sig, x = "wave_pos", y = "result_pos", group = NULL, direction = "Direction",
labels = c("Increase", "Decrease"), colour = "level", fill = "level",
icon = "full triangle", size = 5) {
if (icon == "full triangle") {
icon_up <- base::sprintf("\u25b2")
icon_down <- base::sprintf("\u25bc")
} else if (icon == "clear triangle") {
icon_up <- base::sprintf("\u25b3")
icon_down <- base::sprintf("\u25bd")
} else if (icon == "arrow") {
icon_up <- base::sprintf("\u2191")
icon_down <- base::sprintf("\u2193")
} else if (icon == "finger") {
icon_up <- base::sprintf("\u261d")
icon_down <- base::sprintf("\u261f")
}
base::levels(tbl_sig[[direction]]) <- labels
icons <- base::c(icon_up, icon_down)
base::names(icons) <- labels
geom_sigmark_waves <- base::list(
ggplot2::geom_point(data = tbl_sig, ggplot2::aes_string(x = x, y = y, group = group, colour = colour, fill = fill, shape = direction), size = size),
ggplot2::scale_shape_manual(values = icons),
ggplot2::guides(colour = ggplot2::guide_legend(override.aes = base::list(shape = NA), order = 1),
shape = ggplot2::guide_legend(order = 9),
fill = FALSE)
)
return(geom_sigmark_waves)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.