First, you should load recharts
:
library(recharts)
Force 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. 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 |
‘force’/‘force_curve’, ‘force_line’. |
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).
str(yuNetwork)
## List of 2
## $ nodes:'data.frame': 49 obs. of 3 variables:
## ..$ name : chr [1:49] "曾麟书" "曾国藩" "曾纪泽" "曾纪鸿" ...
## ..$ series: chr [1:49] "Root" "Node 1" "Node 2" "Node 2" ...
## ..$ value : num [1:49] 3 9 3 3 3 3 6 3 3 6 ...
## $ links:'data.frame': 49 obs. of 4 variables:
## ..$ source : chr [1:49] "曾麟书" "曾麟书" "曾麟书" "曾国藩" ...
## ..$ target : chr [1:49] "曾国藩" "曾国荃" "曾国潢" "曾纪泽" ...
## ..$ relation: chr [1:49] "父子" "父子" "父子" "父子" ...
## ..$ weight : num [1:49] 1 1 1 1 1 1 1 1 1 1 ...
The yuNetwork dataset contains a nodes data.frame and a links data.frame. You can then combine them into one.
name
: the nodes name,series
: the series that the nodes belongs to,value
: the importance score of the nodes.source
and target
: defines the connections,relation
: the name of the connections,weight
: the importance sorce of the connections.nodes <- cbind(yuNetwork$nodes[,1], NA, yuNetwork$nodes[,2:3],
stringsAsFactors=FALSE)
names(nodes) <- names(yuNetwork$links)
yu <- rbind(yuNetwork$links, nodes, stringsAsFactors=FALSE)
Set type
‘force’.
echartr(yu, c(source, target), weight, relation, type='force') %>%
setTitle("Yu Family of Shaoxing") %>% setTheme(palette=c(
'tan3','green3','green2','lawngreen','olivedrab1'))
echartr(grpmtx, Name, c(Group1, Group2, Group3, Group4), type='force_line') %>%
setTitle('Test Data', 'Force with ribbon')
Set type
‘force_line’.
echartr(yu, c(source, target), weight, relation, type='force_line') %>%
setTitle("Yu Family of Shaoxing") %>% setTheme(palette=c(
'tan3','green3','green2','lawngreen','olivedrab1'))
Let’s use year
columns as timeline.
echartr(deutsch, c(club, player), weight, role, t=year,
type='force', sub='arrow') %>%
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.