mig_chord {migest} | R Documentation |
Chord diagram for directional origin-destination data
Description
Adaption of circlize::chordDiagramFromDataFrame()
with defaults set to allow for more effective visualisation of directional origin-destination data
Usage
mig_chord(
x,
lab = NULL,
lab_bend1 = NULL,
lab_bend2 = NULL,
label_size = 1,
label_nudge = 0,
label_squeeze = 0,
axis_size = 0.8,
axis_breaks = NULL,
...,
no_labels = FALSE,
no_axis = FALSE,
clear_circos_par = TRUE,
zero_margin = TRUE,
start.degree = 90,
gap.degree = 4,
track.margin = c(-0.1, 0.1),
points.overflow.warning = FALSE
)
Arguments
x |
Data frame with origin in first column, destination in second column and bilateral measure in third column |
lab |
Named vector of labels for plot. If |
lab_bend1 |
Named vector of bending labels for plot. Note line breaks do not work with |
lab_bend2 |
Named vector of second row of bending labels for plot. |
label_size |
Font size of label text. |
label_nudge |
Numeric value to nudge labels towards (negative number) or away (positive number) the sector axis. |
label_squeeze |
Numeric value to nudge |
axis_size |
Font size on axis labels. |
axis_breaks |
Numeric value for how often to add axis label breaks. Default not activated, uses default from |
... |
Arguments for |
no_labels |
Logical to indicate if to include plot labels. Set to |
no_axis |
Logical to indicate if to include plot axis. Set to |
clear_circos_par |
Logical to run |
zero_margin |
Set margins of the plotting graphics device to zero. Set to |
start.degree |
Argument for |
gap.degree |
Argument for |
track.margin |
Argument for |
points.overflow.warning |
Argument for |
Value
Chord diagram based on first three columns of x
. The function tweaks the defaults of circlize::chordDiagramFromDataFrame()
for easier plotting of directional origin-destination data. Users can override these defaults and pass additional tweaks using any of the circlize::chordDiagramFromDataFrame()
arguments.
The layout of the plots are designed to specifically work on plotting images into PDF devices with widths and heights of 7 inches (the default dimension when using the pdf
function). See the end of the examples for converting PDF to PNG images in R.
Fitting the sector labels on the page is usually the most time consuming task. Use the different label options, including line breaks, label_nudge
, track height in preAllocateTracks
and font sizes in label_size
and axis_size
to find the best fit. If none of the label options produce desirable results, plot your own using circlize::circos.text
having set no_labels = TRUE
and clear_circos_par = FALSE
.
Examples
## Not run:
library(tidyverse)
library(countrycode)
# download Abel and Cohen (2019) estimates
f <- read_csv("https://ndownloader.figshare.com/files/38016762", show_col_types = FALSE)
f
# use dictionary to get region to region flows
d <- f %>%
mutate(
orig = countrycode(sourcevar = orig, custom_dict = dict_ims,
origin = "iso3c", destination = "region"),
dest = countrycode(sourcevar = dest, custom_dict = dict_ims,
origin = "iso3c", destination = "region")
) %>%
group_by(year0, orig, dest) %>%
summarise_all(sum) %>%
ungroup()
d
# 2015-2020 pseudo-Bayesian estimates for plotting
pb <- d %>%
filter(year0 == 2015) %>%
mutate(flow = da_pb_closed/1e6) %>%
select(orig, dest, flow)
pb
# pdf(file = "chord.pdf")
mig_chord(x = pb)
# dev.off()
# file.show("chord.pdf")
# pass arguments to circlize::chordDiagramFromDataFrame
# pdf(file = "chord.pdf")
mig_chord(x = pb,
# order of regions
order = unique(pb$orig)[c(1, 3, 2, 6, 4, 5)],
# spacing for labels
preAllocateTracks = list(track.height = 0.3),
# colours
grid.col = c("blue", "royalblue", "navyblue", "skyblue", "cadetblue", "darkblue")
)
# dev.off()
# file.show("chord.pdf")
# multiple line labels to fit on longer labels
r <- pb %>%
sum_region() %>%
mutate(lab = str_wrap_n(string = region, n = 2)) %>%
separate(col = lab, into = c("lab1", "lab2"), sep = "\n", remove = FALSE, fill = "right")
r
# pdf(file = "chord.pdf")
mig_chord(x = pb,
lab = r %>%
select(region, lab) %>%
deframe(),
preAllocateTracks = list(track.height = 0.25),
label_size = 0.8,
axis_size = 0.7
)
# dev.off()
# file.show("chord.pdf")
# bending labels
# pdf(file = "chord.pdf")
mig_chord(x = pb,
lab_bend1 = r %>%
select(region, lab1) %>%
deframe(),
lab_bend2 = r %>%
select(region, lab2) %>%
deframe()
)
# dev.off()
# file.show("chord.pdf")
# convert pdf to image file
# library(magick)
# p <- image_read_pdf("chord.pdf")
# image_write(image = p, path = "chord.png")
# file.show("chord.png")
## End(Not run)