First, you should load recharts:

library(recharts)

1 Introduction

Scatter plot includes 2 basic types:

  • scatter/point
  • bubble

The keys are:

  • numeric x and y
  • valid weight for a bubble chart

2 Function Call

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

data

source data in the form of data.frame

x

numeric independent variable. Only the first one is accepted if multiple variables are provided.

y

numeric dependent variable. Only the first one is accepted if multiple variables are provided.

series

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

weight

numeric weight variable. Only the first one is accepted if multiple variables are provided. weight is associated with dataRange widget. If type is set ‘bubble’, a bubble chart will display.

t

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

type

  • x and y are both numeric, you can omit ‘type’ or assign it ‘auto’.
  • You can also assign it ‘scatter’, ‘point’ or ‘bubble’

3 Showcase

3.1 Scatter Plot

3.1.1 Singular Series

If series is not assigned, the chart gives no legend.

echartr(iris, x=Sepal.Width, y=Petal.Width)

It is equivalent to

echartr(iris, Sepal.Width, Petal.Width, type='scatter')
echartr(iris, ~Sepal.Width, Petal.Width, type='point')
echartr(iris, Sepal.Width, "Petal.Width", type='bubble')
echartr(iris, ~Sepal.Width, "Petal.Width", type='auto')
...

3.1.2 Multiple Series

echartr(iris, x=Sepal.Width, y=Petal.Width, series=Species)

3.1.3 Scatter Plot with Timeline

Timeline calls for numeric or time variable. When a character variable is passed in, echarts coerces it to factors and uses its numeric values for plotting and character levels for timelien labels.

echartr(iris, Sepal.Width, Petal.Width, z=Species)

3.2 Bubble Chart

The key is to pass a valid weight variable. If weight is accepted, and type is ‘bubble’, a bubble chart will display.

echartr(iris, Sepal.Width, Petal.Width, weight=Petal.Length, type='bubble')

If type is ‘scatter’ or ‘point’, the bubbles are not shown, but weight is mapped to the dataRange widget.

echartr(iris, Sepal.Width, Petal.Width, weight=Petal.Length) %>%
    setDataRange(calculable=TRUE, splitNumber=0, labels=c('Big', 'Small'),
                 color=c('red', 'yellow', 'green'), valueRange=c(0, 2.5))

3.2.1 Other Kinds of Bubbles

Bubble chart with timeline, multiple series are similar to ordinary scatter plots.

4 Futher Setup

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

4.1 addMarkLine And addMarkPoint

You can fit a linear regression model and define two points for the markLine.

lm <- with(iris, lm(Petal.Width~Sepal.Width))
pred <- predict(lm, data.frame(Sepal.Width=c(2, 4.5)))

echartr(iris, Sepal.Width, Petal.Width, Species) %>%
    addML(series=1, data=data.frame(name1='Max', type='max')) %>%
    addML(series=2, data=data.frame(name1='Mean', type='average')) %>%
    addML(series=3, data=data.frame(name1='Min', type='min')) %>%
    addMP(series=2, data=data.frame(name='Max', type='max')) %>%
    addML(series='Linear Reg', data=data.frame(
        name1='Reg', value=lm$coefficients[2], 
        xAxis1=2, yAxis1=pred[1], xAxis2=4.5, yAxis2=pred[2]))

You can add marklines/markPoints series by series, just as the example did. But sometimes you may want to add markLines/markPoints for muliple sereis at one time, when you can simply provide a mapping varible series in the data and assign corresponding series variable in the function call.

data <- data.frame(
    name1=c('Max', 'Mean', 'Min'), type=c('max', 'average', 'min'),
    series=levels(iris$Species))
echartr(iris, Sepal.Width, Petal.Width, Species) %>%
    addML(series=1:3, data=data) %>%
    addMP(series=2, data=data.frame(name='Max', type='max')) %>%
    addML(series='Linear Reg', data=data.frame(
        name1='Reg', value=lm$coefficients[2], 
        xAxis1=2, yAxis1=pred[1], xAxis2=4.5, yAxis2=pred[2]))

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