First, you should load recharts
:
library(recharts)
Chord plot includes 2 basic types:
The keys are:
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.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 |
y |
numeric dependent variable. For node/link mode, only the first column of |
series |
series variable which will be coerced to factors. Each level of |
t |
timeline variable which will be coerced to factors. Only the first one is accepted if multiple variables are provided. |
type |
‘chord’. |
subtype |
|
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).
knitr::kable(deutsch[deutsch$year==2014,])
player | club | weight | role | year |
---|---|---|---|---|
Kruse | Monchengladbach | 1 | Fw | 2014 |
Kramer | Monchengladbach | 1 | Mf | 2014 |
Neuer | Bayern | 1 | Gk | 2014 |
Boateng | Bayern | 1 | Df | 2014 |
Lahm | Bayern | 1 | Df | 2014 |
Kroos | Bayern | 1 | Mf | 2014 |
Muller | Bayern | 1 | Mf | 2014 |
Gotze | Bayern | 1 | Fw | 2014 |
Badstuber | Bayern | 1 | Df | 2014 |
Hummels | Dortmund | 1 | Df | 2014 |
Weidenfeller | Dortmund | 1 | Gk | 2014 |
Reus | Dortmund | 1 | Df | 2014 |
Gundogan | Dortmund | 1 | Md | 2014 |
There is not a numeric matrix structure inside the data.frame, so it is node/link mode. There are no NAs in column 1 or 2, so in this case, recharts
will build a node data.frame based on the data.frame given, which looks like:
name | value | series |
---|---|---|
Badstuber | 1 | |
Boateng | 1 | |
Gotze | 2 | |
Gundogan | 1 | |
Howedes | 1 | |
Hummels | 2 | |
Kimmich | 1 | |
Kramer | 1 | |
Kroos | 2 | |
Kruse | 1 | |
Lahm | 1 | |
Leno | 1 | |
Meyer | 1 | |
Muller | 2 | |
Mustafi | 1 | |
Neuer | 2 | |
Ozil | 1 | |
Reus | 1 | |
Tah | 1 | |
Volland | 1 | |
Weidenfeller | 1 | |
Arsenal | 2 | |
Bayern | 11 | |
Dortmund | 5 | |
Leverkusen | 3 | |
Madrid | 1 | |
Monchengladbach | 2 | |
Schalke | 2 |
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')
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)')
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')
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')
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.