## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4.5,
  fig.align = "center",
  warning = FALSE,
  message = FALSE
)

## ----load---------------------------------------------------------------------
library(Rbearcat)
library(ggplot2)
set_UC_geoms()

## ----bar-freq-----------------------------------------------------------------
bcat_plt_bar(
  df = mpg,
  x = class,
  order = TRUE,
  title = "Vehicle Count by Class",
  x_lab = NULL, y_lab = "Count"
)

## ----bar-stat-----------------------------------------------------------------
bcat_plt_bar(
  df = mpg,
  x = class,
  y = hwy,
  fill = factor(year),
  stat = "mean",
  position = "dodge",
  order = TRUE,
  coord_flip = TRUE,
  x_lab = NULL, y_lab = "Highway MPG",
  title = "Mean Highway MPG by Class and Year"
)

## ----bar-fill-----------------------------------------------------------------
bcat_plt_bar(
  df = mpg,
  x = class,
  fill = drv,
  position = "fill",
  y_scale = scale_y_continuous(labels = scales::percent_format()),
  title = "Drive Type Proportions by Class",
  x_lab = NULL, y_lab = NULL,
  legend_lab = "Drive"
)

## ----line-basic---------------------------------------------------------------
bcat_plt_line(
  df = economics,
  x = date,
  y = unemploy,
  y_scale = scale_y_continuous(labels = scales::comma_format()),
  title = "US Unemployment Over Time",
  y_lab = "Number Unemployed"
)

## ----line-multi, fig.height = 6-----------------------------------------------
bcat_plt_line(
  df = economics_long,
  x = date,
  y = value,
  color = variable,
  facet = vars(variable),
  facet_scale = "free_y",
  ncol = 1,
  x_highlight_min = as.Date(c("2007-12-01")),
  x_highlight_max = as.Date(c("2009-06-01")),
  title = "Economic Indicators with Recession Shading",
  x_lab = NULL, y_lab = NULL,
  legend_lab = NULL
)

## ----point-basic--------------------------------------------------------------
bcat_plt_point(
  df = iris,
  x = Sepal.Length,
  y = Sepal.Width,
  title = "Sepal Width vs Length",
  x_lab = "Length", y_lab = "Width"
)

## ----point-facet--------------------------------------------------------------
bcat_plt_point(
  df = iris,
  x = Sepal.Length,
  y = Sepal.Width,
  color = Species,
  facet = vars(Species),
  smooth = TRUE,
  method = "lm",
  nrow = 1,
  title = "By Species with Linear Fit",
  x_lab = "Length", y_lab = "Width",
  legend_lab = NULL
)

## ----area---------------------------------------------------------------------
set.seed(42)
d <- data.frame(
  t = rep(0:23, each = 4),
  category = rep(LETTERS[1:4], 24),
  value = round(runif(96, 10, 50))
)

bcat_plt_area(
  df = d, x = t, y = value, fill = category,
  position = "stack",
  title = "Stacked Area Chart",
  x_lab = "Hour", y_lab = "Value",
  legend_lab = "Category"
)

## ----area-fill----------------------------------------------------------------
bcat_plt_area(
  df = d, x = t, y = value, fill = category,
  position = "fill",
  title = "Proportional Area Chart",
  x_lab = "Hour", y_lab = NULL,
  legend_lab = "Category"
)

## ----hist-basic---------------------------------------------------------------
bcat_plt_hist(
  mtcars, x = mpg,
  title = "Distribution of MPG",
  x_lab = "Miles per Gallon"
)

## ----hist-density-------------------------------------------------------------
bcat_plt_hist(
  mtcars, x = mpg,
  density = TRUE,
  bins = 15,
  title = "MPG with Density Overlay",
  x_lab = "Miles per Gallon"
)

## ----hist-facet---------------------------------------------------------------
bcat_plt_hist(
  mtcars, x = mpg,
  facet = vars(cyl),
  facet_scale = "free_x",
  title = "MPG Distribution by Cylinder Count"
)

## ----box-basic----------------------------------------------------------------
bcat_plt_box(
  mtcars,
  x = factor(cyl),
  y = mpg,
  title = "MPG by Cylinder Count",
  x_lab = "Cylinders", y_lab = "MPG"
)

## ----box-violin---------------------------------------------------------------
bcat_plt_box(
  mtcars,
  x = factor(cyl),
  y = mpg,
  violin = TRUE,
  title = "MPG Distribution (Violin)",
  x_lab = "Cylinders", y_lab = "MPG"
)

## ----box-flip-----------------------------------------------------------------
bcat_plt_box(
  mtcars,
  x = factor(gear),
  y = mpg,
  order = TRUE,
  coord_flip = TRUE,
  title = "MPG by Gear Count (Ordered)",
  x_lab = "Gears", y_lab = "MPG"
)

## ----coef-single--------------------------------------------------------------
m1 <- lm(mpg ~ wt + hp + cyl + disp, data = mtcars)
bcat_plt_coef(m1, title = "OLS Coefficient Estimates")

## ----coef-multi---------------------------------------------------------------
m2 <- lm(mpg ~ wt + hp, data = mtcars)
bcat_plt_coef(
  list("Full" = m1, "Base" = m2),
  title = "Coefficient Comparison",
  subtitle = "95% Confidence Intervals"
)

## ----coef-highlight-----------------------------------------------------------
bcat_plt_coef(
  m1,
  highlight = "Wt",
  title = "Highlighting Weight"
)

## ----diag, fig.height = 6, fig.width = 8--------------------------------------
m <- lm(mpg ~ wt + hp + cyl, data = mtcars)
bcat_plt_diag(m)

## ----diag-select--------------------------------------------------------------
bcat_plt_diag(m, which = c(1, 2), tests = FALSE)

## ----ts-basic-----------------------------------------------------------------
bcat_plt_ts(
  economics,
  x = date, y = unemploy,
  y_scale = scale_y_continuous(labels = scales::comma_format()),
  title = "US Unemployment",
  y_lab = "Persons Unemployed"
)

## ----ts-highlight-------------------------------------------------------------
bcat_plt_ts(
  economics,
  x = date, y = unemploy,
  y_scale = scale_y_continuous(labels = scales::comma_format()),
  x_highlight_min = as.Date("2007-12-01"),
  x_highlight_max = as.Date("2009-06-01"),
  title = "US Unemployment with Great Recession Shading"
)

## ----ts-decompose, fig.height = 7---------------------------------------------
bcat_plt_ts(economics, x = date, y = unemploy, decompose = TRUE)

## ----ts-acf-------------------------------------------------------------------
bcat_plt_ts(economics, x = date, y = unemploy, acf = TRUE)

## ----addlayer-----------------------------------------------------------------
bcat_plt_point(iris, Sepal.Length, Sepal.Width,
               title = "Adding a Custom Annotation") +
  annotate("text", x = 7, y = 4.2, label = "Outlier region",
           color = "red", fontface = "italic")

