---
title: Labels
---

```{r}
library(gglite)
df = data.frame(x = c('A', 'B', 'C'), y = c(3, 7, 2))
p  = g2(df, y ~ x)
```

Labels add text annotations to marks. Use `labels()` to configure them.
When using `+`, the first argument must be unnamed; see `?labels.g2`.

## Basic Labels on Bars

```{r}
p |> labels(text = ~ y)
```

## Label Position: Inside

```{r}
p |> labels(text = ~ y, position = 'inside')
```

## Label Position: Top

```{r}
p |> labels(text = ~ y, position = 'top')
```

## Multiple Label Layers

```{r}
p |>
  labels(text = ~ x, position = 'inside') |>
  labels(text = ~ y, position = 'top')
```

## Labels on a Scatter Plot

```{r}
df_pt = data.frame(x = 1:5, y = c(3, 7, 2, 9, 5), label = c('a', 'b', 'c', 'd', 'e'))
g2(df_pt, y ~ x) |> labels(text = ~ label)
```

## Using `+` Operator

```{r}
p + labels(~ y)
p + labels('y', position = 'inside')
```
