## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(phsmethods)

## -----------------------------------------------------------------------------
(p <- as_percent(0.055))

## -----------------------------------------------------------------------------
unclass(p)

## -----------------------------------------------------------------------------
print(p)
as.character(p)
format(p)

## -----------------------------------------------------------------------------
p2 <- as_percent(p, digits = 0)

# Prints and formats to 0 decimal places
print(p2)
as.character(p2)

# Underlying data has not been rounded!
unclass(p2)

## -----------------------------------------------------------------------------
p3 <- round(p, digits = 0)

p3

# Underlying data has been rounded
unclass(p3)

## -----------------------------------------------------------------------------
# Helper to create literal percentages
percent <- function(x) {
  as_percent(x / 100)
}

## -----------------------------------------------------------------------------
percent(50) + percent(25) # = 50% + 25% = 75%
percent(50) - percent(25) # = 50% - 25% = 25%
percent(50) * percent(25) # = 50% * (1/4) = 12.5%
percent(50) / percent(25) # = 50% / (1/4) = 200%

## -----------------------------------------------------------------------------
percentages <- percent(seq(-0.1, 0.1, by = 0.05))
floor(percentages)
ceiling(percentages)
trunc(percentages)
round(percentages)
round(percentages, 1)
round(percentages, 2)

## -----------------------------------------------------------------------------
library(dplyr)

species <- starwars |>
  count(species, sort = TRUE) |>
  mutate(perc = as_percent(n / sum(n), digits = 1))

# Prints nicely
species |>
  slice_head(n = 5)

## -----------------------------------------------------------------------------
perc_summary <- species |>
  summarise(
    min = min(perc),
    max = max(perc),
    median = median(perc),
    avg = mean(perc),
    sum = sum(perc)
  )
perc_summary

## -----------------------------------------------------------------------------
library(knitr)
kable(perc_summary)

## -----------------------------------------------------------------------------
library(flextable)
qflextable(perc_summary)

## -----------------------------------------------------------------------------
library(ggplot2)

gg_data <- iris |>
  as_tibble() |>
  count(Species) |>
  mutate(
    prop = n / sum(n),
    perc = as_percent(
      prop,
      digits = 1 # To control formatting in ggplot + elsewhere
    )
  )

gg_data

species_gg <- gg_data |>
  ggplot(aes(Species)) +
  geom_col(aes(y = prop, fill = Species), width = 0.25)

## ----fig.width = 7, fig.height = 6--------------------------------------------
species_gg +
  scale_y_continuous(name = "Percentage", labels = as_percent)

## ----fig.width = 7, fig.height = 6--------------------------------------------
gg_data |>
  ggplot(aes(x = "", y = perc, fill = Species)) +
  geom_bar(stat = "identity", width = 1, color = "white") +
  coord_polar("y", start = 0) +
  theme_void() +
  geom_text(aes(label = perc), position = position_stack(vjust = 0.5)) +
  scale_fill_brewer(palette = "Set1")

