R/graph_describe.R

Defines functions graph_describe graph_describe.default graph_describe_quant graph_describe.numeric graph_describe.Date graph_describe.factor graph_describe.character graph_describe.logical

graph_describe <- function(x) UseMethod("graph_describe", x)

graph_describe.default <- function(x) {
  stop(paste(
    "No `graph_describe` method available for object of class:", 
    class(x))
  )
}

graph_describe_quant <- function(x) {

  ggplot2::ggplot(tibble::tibble(x = x), ggplot2::aes(x = x)) +
    ggplot2::geom_histogram(
      ggplot2::aes(y = ..density..),
      bins = min(round(sqrt(length(x)), digits = 0), 50),
      fill = pkg_env$univar_color, 
      alpha = 0.6
    ) +
    ggplot2::geom_density(
      color = pkg_env$univar_color,
      fill = pkg_env$univar_color, 
      alpha = 0.3
    ) +
    ggplot2::geom_vline(xintercept = median(x), color = "gray30", linetype = "dotted") +
    ggplot2::geom_vline(xintercept = Q1(x), color = "gray", linetype = "dotted") +
    ggplot2::geom_vline(xintercept = Q3(x), color = "gray", linetype = "dotted") +
    ggplot2::theme_light() + 
    ggplot2::xlab("")
}

graph_describe.numeric <- function(x) graph_describe_quant(x)

graph_describe.Date <- function(x) graph_describe_quant(x)


graph_describe.factor <- function(x) {
  
  x <- fact_reorder_freq(x)
  
  ggplot2::ggplot(tibble::tibble(x = x), ggplot2::aes(x = x)) +
    ggplot2::geom_bar(
      ggplot2::aes(y = ..count..),
      fill = pkg_env$univar_color, 
      alpha = 0.6
    ) +
    ggplot2::theme_light() +
    ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1)) + 
    ggplot2::xlab("")
}

graph_describe.character <- function(x) graph_describe(as.factor(x))

graph_describe.logical <- function(x) graph_describe(as.factor(x))
AdrienLeGuillou/descriptor documentation built on May 22, 2019, 7:55 p.m.