- Notifications
You must be signed in to change notification settings - Fork0
KevinMcDonnell6/chorddiag
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Thechorddiag
package allows to create interactive chord diagrams using the JavaScript visualization library D3 (http://d3js.org) from within R using thehtmlwidgets
interfacing framework.. Chord diagrams show directed relationships among a group of entities. The chord diagram layout is explained in detail here:https://github.com/mbostock/d3/wiki/Chord-Layout.
To quote the explanation found there:
Consider a hypothetical population of people with different hair colors: black, blonde, brown and red. Each person in this population has a preferred hair color for a dating partner; of the 29,630 (hypothetical) people with black hair, 40% (11,975) prefer partners with the same hair color. This preference is asymmetric: for example, only 10% of people with blonde hair prefer black hair, while 20% of people with black hair prefer blonde hair. A chord diagram visualizes these relationships by drawing quadratic Bézier curves between arcs. The source and target arcs represents two mirrored subsets of the total population, such as the number of people with black hair that prefer blonde hair, and the number of people with blonde hair that prefer black hair.
To create a chord diagram for this example, we need the preferences in matrix format:
m<-matrix(c(11975,5871,8916,2868,1951,10048,2060,6171,8010,16145,8090,8045,1013,990,940,6907),byrow=TRUE,nrow=4,ncol=4)haircolors<- c("black","blonde","brown","red")dimnames(m)<-list(have=haircolors,prefer=haircolors)m#> prefer#> have black blonde brown red#> black 11975 5871 8916 2868#> blonde 1951 10048 2060 6171#> brown 8010 16145 8090 8045#> red 1013 990 940 6907
Then, thechorddiag
function creates the chord diagram:
library(chorddiag)groupColors<- c("#000000","#FFDD89","#957244","#F26223")chorddiag(m,groupColors=groupColors,groupnamePadding=20)
This diagram will be interactive when generated by the function. Interactive here means chord fading and tooltip popups on certain mouse over events. E.g. if the mouse pointer hovers over the chord connecting the "blonde" and "red" groups, a tooltip is displayed giving the numbers for the chord, and all other chords fade away.
The package's code is based onhttp://bl.ocks.org/mbostock/4062006, with modifications for fading behaviour and addition of tooltips.
The default chord diagram type isdirectional but chord diagrams can also be a useful visualization of relationships between two categories of groups, i.e. contingency tables. In this package, this type of chord diagram is calledbipartite (because there are only chordsbetween categories but notwithin categories). Here is an example for theTitanic
dataset, depicting how many passengers from the different classes and from the crew survived or died when the ship sunk:
See thevignette for more information.
You can track (and contribute to) development ofchorddiag
athttps://github.com/mattflor/chorddiag. To install it, run the following command (this requires thedevtools
package):
devtools::install_github("mattflor/chorddiag")
If this fails to install the package vignette, trydevtools::install_github("mattflor/chorddiag", build_vignettes = TRUE)
.
About
R interface to D3 chord diagrams
Resources
Stars
Watchers
Forks
Packages0
Languages
- HTML39.4%
- JavaScript37.1%
- R21.7%
- CSS1.3%
- Makefile0.5%