---
title: "Quick start"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Quick start}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library(highdir)
```

There are two ways to use `highdir`:

1. Declarative API approach 
2. Layered API approach

## Declarative API 

```{r setup, eval = FALSE}

library(highdir)

# Create dataset
df <- data.frame(
  age  = rep(c("18-24", "25-34", "35-44", "45-54"), each = 2),
  sex  = rep(c("Male", "Female"), 4),
  pct  = c(42, 38, 55, 61, 48, 52, 60, 57),
  n    = c(120, 115, 200, 210, 180, 175, 160, 155)
)

# Figure data specification 
spec <- hd_spec(df, x = "age", y = "pct", group = "sex", n = "n")

# Specify figure presentation
opts <- hd_opts(title = "Tall om Report",
                subtitle = "Source: Example data",
                caption  = "Tall om helse",
                ylim = c(0, 80))

hd_make(spec, "column", opts)                       # highcharter (default)
hd_make(spec, "column", opts, backend = "ggplot2")  # static ggplot2
hd_make(spec, "line",   opts, smooth = TRUE)        # smooth spline
hd_make(spec, "pie",    opts)                       # pie / donut

# Disable JavaScript (for static HTML export)
hd_make(spec, "column", opts, use_js = FALSE)

# Save
hd01 <- hd_make(spec, "column")
hd_save(fig = hd01, file = "chart.html")

gg01 <- hd_make(spec, "column", backend = "ggplot2")
hd_save(fig = gg01, file = "chart.png")
```

## Layered API 

```{r gg, eval = FALSE}

# Interactive
hd(df, x = "age", y = "pct", group = "sex", n = "n") +
  hd_geom_column() +
  hd_opts(title = "Tall om Report",
          subtitle = "Source: Example data",
          caption  = "Tall om helse",
          ylim = c(0, 80))

# Static ggplot2
hd(df, x = "age", y = "pct", group = "sex", n = "n", backend = "ggplot2") +
  hd_geom_column() +
  hd_opts(title = "Tall om Report",
          subtitle = "Source: Example data",
          caption  = "Tall om helse",
          ylim = c(0, 80))

```

For more example on how to use other geoms, please read in
[other geom examples](https://folkehelsestats.github.io/highdir/articles/examples.html)

## Geom 

To list all geom features.

```{r geom}
list_geoms()
```

