The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Positioning labels

library(ggchord2)
library(ggplot2)
library(dplyr)

Set up some data:

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:

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.

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.

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:

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:

g +
  geom_chord_labels_curve(
    mapping = aes(colour = source),
    size = 4
  ) +
  scale_x_continuous(limits = c(-1.5, 1.5))
#> Warning: The text offset exceeds the curvature in one or more paths. This will result in
#> displaced letters. Consider reducing the vjust or text size, or use the hjust
#> parameter to move the string to a different point on the path.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.