First, you should load recharts
:
library(recharts)
Scatter plot includes 2 basic types:
The keys are:
x
and y
weight
for a bubble chartechartr(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. |
t |
timeline variable which will be coerced to factors. Only the first one is accepted if multiple variables are provided. |
type |
|
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') ...
echartr(iris, x=Sepal.Width, y=Petal.Width, series=Species)
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)
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))
Bubble chart with timeline, multiple series are similar to ordinary scatter plots.
Then you can configure the widgets, add markLines and/or markPoints, fortify the chart.
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.