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

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

```{r, warning=FALSE, message=FALSE}
library(ggchord2)
library(ggplot2)
library(dplyr)
```

Set up some data:

```{r}
set.seed(123)
n <- 15
flows <- expand.grid(
  source = 1:n,
  target = 1:n
) |>
  filter(source < target) |>
  mutate(
    source = paste0("Category ", LETTERS[source]),
    target = paste0("Category ", LETTERS[target]),
    freq = rpois(((n^2 - n) / 2), 4)
  )
```

Create a basic chord diagram without labels:

```{r}
g <- ggplot(
  data = flows,
  mapping = aes(
    source = source,
    target = target,
    freq = freq
  )
) +
  geom_chord_arcs(
    mapping = aes(fill = source),
    alpha = 0.5
  ) +
  geom_chord_sectors(
    mapping = aes(fill = source),
    colour = "white",
    linewidth = 0.8
  ) +
  coord_fixed() +
  theme_void() +
  theme(legend.position = "none")
```

If you have many segments, there may not be enough space for the labels using the default `geom_chord_labels()` function. 

```{r}
g +
  geom_chord_labels(
    mapping = aes(colour = source),
    size = 4
  )
```

A simple approach is to increase the limits of the x axis. The default is that the chord diagram is on a circle centred at `[0, 0]` with radius 1.

```{r}
g +
  geom_chord_labels(
    mapping = aes(colour = source),
    size = 4
  ) +
  scale_x_continuous(limits = c(-1.5, 1.5))
```

You can use perpendicular labels instead. Again, you may need to adjust the x and y limits to fit all the labels in:

```{r}
g +
  geom_chord_labels_perp(
    mapping = aes(colour = source),
    size = 4
  ) +
  scale_x_continuous(limits = c(-1.5, 1.5)) +
  scale_y_continuous(limits = c(-1.5, 1.5))
```

You can also use curved labels through [`geomtextpath`](https://github.com/AllanCameron/geomtextpath):

```{r}
g +
  geom_chord_labels_curve(
    mapping = aes(colour = source),
    size = 4
  ) +
  scale_x_continuous(limits = c(-1.5, 1.5))
```
