---
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

> &#x1F4DD; **Note** 
>
> The `backend = "highcharter"` or `backend = "ggplot2"` argument in `hd_make()` and `hd()` functions will be replaced by
> `mode = "dynamic"` or `mode = "static"` repectively in a future release. Users of **highdir ver 0.5.0** should continue to use
> `backend`. The examples below use the forthcoming `mode` argument.
>
> See the [changelog](https://folkehelsestats.github.io/highdir/news/index.html#highdir-060-dev) for more information.

## 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)                       # dynamic (default)
hd_make(spec, "column", opts, mode = "static")      # use backend for ver 0.5.0 
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", mode = "static")
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", mode = "static") +
  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()
```

