R/geom_sigmark_total.R

Defines functions geom_sigmark_total

Documented in geom_sigmark_total

#' 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 "level".
#' @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 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 == "2016",
#'                            conlegis %in% c("A GREAT DEAL", "ONLY SOME", "HARDLY ANY"))
#' my_results <- freq_prop_test(gss_data1, "conlegis", "region", level = "E. NOR. CENTRAL", weight = "wtssall")
#' sig <- tbl_sig(my_results, "region", space_label = 0.05, compare = "total")
#' chart <- tbl_chart(gss_data1, "conlegis", "region", weight = "wtssall")
#' chart <- dplyr::filter(chart, region == "E. NOR. CENTRAL")
#' chart$conlegis <- forcats::fct_relevel(chart$conlegis, "HARDLY ANY", "ONLY SOME", "A GREAT DEAL")
#'
#' library(ggplot2)
#' ggplot() +
#' geom_col(data = chart, aes(x = conlegis, y = prop)) +
#' 	coord_flip() +
#' 	geom_text(data = chart, aes(x = conlegis, y = prop, label = scales::percent(round(prop, 2))), hjust = -0.2) +
#' 	geom_sigmark_total(sig)

geom_sigmark_total <- function(tbl_sig, x = "level", y = "pos", group = NULL, colour = NULL, direction = "Direction",
															 labels = c("Higher", "Lower"), 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")
	}

	icons <- base::c(icon_up, icon_down)
	base::names(icons) <- labels

	base::levels(tbl_sig[[direction]]) <- labels

	geom_sigmark_total <- base::list(
		ggplot2::geom_point(data = tbl_sig, ggplot2::aes_string(x = x, y = y, group = group, colour = colour, 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))
	)
	return(geom_sigmark_total)
}
philstraforelli/ggsigmark documentation built on May 20, 2019, 1:59 p.m.