First, you should load recharts:

library(recharts)

1 Introduction

Chord plot includes 2 basic types:

  • Chord with Ribbon
  • Chord without Ribbon

The keys are:

  • data structure:
  • matrix mode: A data.frame comprising of a column of name, and a numeric matrix. Assign the name column to x, the matrix to y.
  • node/link mode: node data.frame [x, NA, series, weight]; link data.frame [x, x1, relation, value]. Combine them using rbind. If you don’t provide node data.frame, recharts will build it automatically. Assign the param list [x, x1, series/relation, weight/value] accordingly.

2 Function Call

echartr(data, x, <y>, <series>, <t>, <type>, <subtype>)
Arg Requirement

data

source data in the form of data.frame

x

character independent variable. Other type will be coerced to factors. For node/link mode, you must provide two columns of x. For matrix mode, only the first column of x is accepted.

y

numeric dependent variable. For node/link mode, only the first column of y is accepted. For matrix mode, all the columns of y are accepted.

series

series variable which will be coerced to factors. Each level of series is treated as a subsetting factor to produce separate pies. Only the first one is accepted if multiple variables are provided.

t

timeline variable which will be coerced to factors. Only the first one is accepted if multiple variables are provided.

type

‘chord’.

subtype

  • chord: c(“ribbon”,“scale”,“scaletext”,“rotatelab”,“hidelab”,“clock”,“clockwise”,“asc”,“desc”,“ascsub”,“descsub”)
    • ribbon: chort chart with ribbon. It requires dual-direction of links.
    • scale: Show scale on the ribbon.
    • scaletext: Show scale and text on the ribbon.
    • rotatelab: rotate the tick label.
    • hidelab: hide the label (default show).
    • clock/clockwise: ascending order is clockwise (default anti-clockwise).
    • asc: ascending nodes
    • desc:descending nodes
    • ascsub: asceding chords
    • descsub: desceding chords

3 Showcase

3.1 Data Preparation

3.1.1 Matrix Mode

grpmtx <- matrix(c(11975, 5871, 8916, 2868, 1951, 10048, 2060, 6171, 8010, 16145,
                   8090, 8045, 1013, 990, 940, 6907), byrow=TRUE, nrow=4)
grpmtx <- as.data.frame(grpmtx)
names(grpmtx) <- paste0('Group', 1:4)
grpmtx$Name <- paste0('Group', 1:4)
knitr::kable(grpmtx, align=c('lllll'))
Group1 Group2 Group3 Group4 Name
11975 5871 8916 2868 Group1
1951 10048 2060 6171 Group2
8010 16145 8090 8045 Group3
1013 990 940 6907 Group4

The first 4 columns are exactly a matrix structure and the last column is a name vector. So this meets data structure requirements for matrix mode.

Matrix mode can be transformed to node/link mode as well. Matrix[i, j] represents 2 nodes (i & j) and 1 link (i -> j).

3.2 Chord Chart

3.2.1 Chord with Ribbon

Set type ‘chord’ and subtype ‘ribbon’.

echartr(grpmtx, Name, c(Group1, Group2, Group3, Group4), 
        type='chord', subtype='ribbon + asc + descsub + hidelab + scaletext') %>% 
  setTitle('Test Data','From d3.js')

3.2.2 Chord without Ribbon

3.2.2.1 One-direction Relationship

Set type ‘chord’. If the data structure does not include dual-direction relationship, no matter whether you assign subtype ‘ribbon’, ribbon will not display properly.

echartr(deutsch[deutsch$year==2014,], c(club, player), weight, role, type='chord', 
        sub='asc + descsub + rotatelab') %>% 
  setTitle('Club Orientation of Deutsch Soccer Team (2014)')

3.2.2.2 Dual-direction Relationship

If you do want to display a chord with ribbon, you have to revise the data with dual-diretion relationship.

By now, deutsch14 only has one-direction relationship, which means, it only defines Gotze->Bayern, but not Bayern->Gotze.

knitr::kable(with(deutsch[deutsch$year==2014,], table(player, club)))
Bayern Dortmund Monchengladbach
Badstuber 1 0 0
Boateng 1 0 0
Gotze 1 0 0
Gundogan 0 1 0
Hummels 0 1 0
Kramer 0 0 1
Kroos 1 0 0
Kruse 0 0 1
Lahm 1 0 0
Muller 1 0 0
Neuer 1 0 0
Reus 0 1 0
Weidenfeller 0 1 0

What we need to do is duplicate the source and target columns to build a matrix.

deutsch14 <- deutsch[deutsch$year==2014,]
deutsch14 <- rbind(deutsch14, deutsch14)
deutsch14[14:26, c('player', 'club')] <- recharts:::exchange(
    deutsch14[14:26, 'player'], deutsch14[14:26, 'club'])
knitr::kable(with(deutsch14, table(player, club)))
Badstuber Bayern Boateng Dortmund Gotze Gundogan Hummels Kramer Kroos Kruse Lahm Monchengladbach Muller Neuer Reus Weidenfeller
Badstuber 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Bayern 1 0 1 0 1 0 0 0 1 0 1 0 1 1 0 0
Boateng 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Dortmund 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1
Gotze 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Gundogan 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
Hummels 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
Kramer 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
Kroos 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Kruse 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
Lahm 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Monchengladbach 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0
Muller 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Neuer 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Reus 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
Weidenfeller 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0

In this way set subtype ‘ribbon’ and we get a chord with ribbon.

echartr(deutsch14, c(club, player), weight, role, type='chord', 
        sub='asc + descsub + rotatelab + ribbon') %>% 
  setTitle('Club Orientation of Deutsch Soccer Team (2014)')

Another example is based on preinstalled dataset mideast.

mideast <- as.data.frame(mideast, col.names=mideast[1,], stringsAsFactors=FALSE)
names(mideast) <- mideast[1,]
mideast <- mideast[2:16,]
me <- data.table::melt(mideast, id=NA)
## Warning in melt_dataframe(data, as.integer(id.ind - 1),
## as.integer(measure.ind - : '.Random.seed' is not an integer vector but of
## type 'NULL', so ignored
me <- me[!is.na(me$value),]
me$series <- strsplit(me$value, '/')
me$value <- sapply(me$series, function(x) as.numeric(x[2]))
me$series <- sapply(me$series, function(x) x[1])
names(me) <- c('source', 'target', 'value', 'series')
str(me)
## 'data.frame':    82 obs. of  4 variables:
##  $ source: chr  "叙利亚反对派" "阿萨德" "伊朗" "塞西" ...
##  $ target: Factor w/ 15 levels "美国","叙利亚反对派",..: 1 1 1 1 1 1 1 1 2 2 ...
##  $ value : num  1 1 1 1 1 1 1 1 9 9 ...
##  $ series: chr  "支持" "反对" "反对" "未表态" ...
echartr(me, c(source, target), value, series, type='chord', sub='ribbon')

3.2.3 Chord with Timeline

Let’s use year columns as timeline.

echartr(deutsch, c(club, player), weight, role, t=year, type='chord', 
        sub='asc + descsub + rotatelab') %>% 
    setTimeline(show=FALSE, autoPlay=TRUE) %>%
    setTitle('Club Orientation of Deutsch Soccer Team')

4 Futher Setup

Then you can configure the widgets, add markLines and/or markPoints, fortify the chart.

You can refer to related functions to play around on your own.