First, you should load recharts:

library(recharts)

1 Introduction

Force plot includes 2 basic types:

  • Force chart with curve
  • Force chart with line

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.
  • force chart and chord chart are exchangeble by the toolbox buttons.

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. 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

  • force: c(“arrow”, “triangle”)
    • arrow: The end symbol of the connection lines is arrow.
    • triangle: The end symbol of the connection lines is triangle
  • force_line: c(“arrow”, “triangle”)

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 Force Chart

3.2.1 Force with Curve

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'))

3.2.2 Force with Line

3.2.2.1 Matrix Mode

echartr(grpmtx, Name, c(Group1, Group2, Group3, Group4), type='force_line') %>% 
  setTitle('Test Data', 'Force with ribbon')

3.2.3 Force with Timeline

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')

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.