recharts

recharts

最近打算清理一下东西,然后过几天就可以收拾收拾回家玩去了。所以这几篇都是打印资料的学习笔记。而这篇是recharts帮助文档的笔记。该帮助文档的地址为:recharts。看完之后感觉这个包在基础绘图函数部分设计的很棒,但是存在参数默认值不合理的问题。此外高级绘图函数有些设计了挺糟糕的。期待以后的新版本能有所改进。

recharts提供了ECharts和R语言的接口,现提供的图形展示接口包括:地图(eMap),柱状图(eBar),折线图(eLine),雷达图(eRadar),散点图(ePoints),漏斗图(eFunnel)以及饼图(ePie)。

recharts的安装

R
1
devtools::install_github("cosname/recharts", force = TRUE)

案例及接口

官网示例

ECharts的官网地址为:ECharts
echartsExample()函数提供了从官方实例中获取JS代码并运行的途径,除去某些需要加载json数据文件的例子,该函数支持大部分ECharts的官网示例。

R
1
2
echartsExample('http://echarts.baidu.com/examples/editor.html?c=area-rainfall')
echartsExample('http://echarts.baidu.com/examples/editor.html?c=sunburst-book')

这个函数的参数有:

  1. url:指定例子的网址;
  2. JScontent:需要执行的JS代码(方便复制例子里面的代码调试执行),需要注意要命名一个option对象用于加载数据。
  3. height:绘图区域的高度;
  4. width:绘图区域的宽度;

JScontent示例

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
JScodes = "
option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar'
}]
};
"
echartsExample(JScontent = JScodes)

基本绘图函数

散点图

散点图需要输入的是数据框,指定xvar和yvar,series为可选的颜色分度参数:

R
1
2
df <- iris[, 3:5]
ePoints(iris[, 3:5], theme = 2)

R
1
2
3
iris$Species <- as.character(iris$Species)
iris[1:20, "Species"] = "redFlower"
ePoints(iris[,3:5], xvar = ~Petal.Length, yvar = ~Petal.Width, series = ~Species, theme = 1)

R
1
ePoints(iris[, 3:5], xvar = ~Petal.Length, yvar = ~Petal.Width, series = ~Species, theme = 6)

线图

R
1
2
3
4
5
6
7
8
9
head(WorldPhones)
# N.Amer Europe Asia S.Amer Oceania Africa Mid.Amer
# 1951 45939 21574 2876 1815 1646 89 555
# 1956 60423 29990 4708 2568 2366 1411 733
# 1957 64721 32510 5230 2695 2526 1546 773
# 1958 68484 35218 6662 2845 2691 1663 836
# 1959 71799 37598 6856 3000 2868 1769 911
# 1960 76036 40341 8220 3145 3054 1905 1008
eLine(WorldPhones, theme = 2)

R
1
2
3
4
5
df2 <- data.frame(saleNum = c(10, 20, 30, 40, 50, 60, 70, 15, 25, 35, 45, 55, 65, 75, 25, 35, 45, 55, 65, 75, 85),
seller = c(rep("Yellow", 7), rep("Red", 7), rep("White", 7)),
weekDay = c(rep(c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"), 3)),
stringsAsFactors = F)
eLine(df2, xvar = ~weekDay, yvar = ~saleNum, series = ~seller)

R
1
2
3
4
ns <- rnorm(1000)
dat <- cut(ns, -4:4)
# cut()函数的功能对序列进行分组,会返回序列中每个值位于的组别,例如这里给定的组为(-4,-3] (-3,-2] (-2,-1] (-1,0] (0,1] (1,2] (2,3] (3,4]。cut()函数会将序列ns中的每个元素在这些组中找到该元素所在的组并返回。
eLine(dat)

条形图

R
1
2
3
4
5
6
7
8
9
10
11
12
require(plyr)
# 计算鸢尾花数据集中每种花每个变量的均值并返回一个数据框
dat = ddply(iris, .(Species), colwise(mean))
row.names(dat) <- dat[, 1]
dat <- dat[, -1]
dat
# Sepal.Length Sepal.Width Petal.Length Petal.Width
# redFlower 5.035000 3.480000 1.435 0.2350000
# setosa 4.986667 3.393333 1.480 0.2533333
# versicolor 5.936000 2.770000 4.260 1.3260000
# virginica 6.588000 2.974000 5.552 2.0260000
eBar(dat, theme = 3)

R
1
eBar(dat, horiz = T, theme = 4)

R
1
eBar(df2, xvar = ~seller, yvar = ~saleNum, series = ~weekDay, theme = 5)

R
1
eBar(df2, xvar = ~seller, yvar = ~saleNum, series = ~weekDay, stack = T, theme = 1, ylim = c(0, 450))

R
1
eBar(df2, xvar = ~seller, yvar = ~saleNum, series = ~weekDay, stackGroup = list(c('Sat', 'Sun')), ylim = c(0, 200))

R
1
2
dat <- df2[1:7,]
eBar(dat, ~weekDay, ~saleNum, ylim = c(0, 80))

R
1
2
dat <- cut(rnorm(10000), seq(-4, 4, 0.01))
eBar(dat)

饼图

R
1
2
3
4
(x = runif(6))
names(x) = LETTERS[1:6]
x
ePie(x) + eTitle("test")

R
1
2
(testData <- head(mapTestData_chs, 5))
ePie(testData, ~stdName, ~val1, theme = 1)

R
1
2
3
(x <- runif(6))
names(x) <- LETTERS[1:6]
ePie(x, reset_radius = c(80, 120), showL = T, labelformatter = "{b}:{c}")

高级绘图函数

堆砌区域图

R
1
2
require(plyr)
eArea(WorldPhones, theme = 1, ylim = c(0, 150000))

R
1
eArea(df2, xvar = ~weekDay, yvar = ~saleNum, series = ~seller, ylim = c(0, 250))

R
1
2
dat <- cut(rnorm(1000), -4:4)
eArea(dat)

K线图

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mat = rbind(c(2286.33,2299.99,2281.9,2309.39),
c(2297.11,2305.11,2290.12,2305.3),
c(2303.75,2302.4,2292.43,2314.18),
c(2293.81,2275.67,2274.1,2304.95),
c(2281.45,2288.53,2270.25,2292.59),
c(2286.66,2293.08,2283.94,2301.7),
c(2293.4,2321.32,2281.47,2322.1),
c(2323.54,2324.02,2321.17,2334.33),
c(2316.25,2317.75,2310.49,2325.72),
c(2320.74,2300.59,2299.37,2325.53),
c(2300.21,2299.25,2294.11,2313.43),
c(2297.1,2272.42,2264.76,2297.1),
c(2270.71,2270.93,2260.87,2276.86),
c(2264.43,2242.11,2240.07,2266.69),
c(2242.26,2210.9,2205.07,2250.63),
c(2190.1,2148.35,2126.22,2190.1)
)
rownames(mat) <- Sys.Date() - (16:1)
eCandle(mat)

R
1
2
3
4
5
6
7
8
9
library(RStata)
options("RStata.StataPath" = "/Applications/Stata/StataSE.app/Contents/MacOS/stata-se")
options("RStata.StataVersion" = 15)
df3 <- stata("cntrade2 1, index", data.out = TRUE)
df3 <- df3[-1,]
rownames(df3) <- df3$date
df3 <- df3[, c('open', 'close', 'low', 'high')]
df3 <- df3[6700:6781,]
eCandle(df3, theme = 2)

漏斗图

R
1
2
x <- c("Exposure" = 100, "Click" = 80, "Visit" = 60, "Query" = 40, "Buy" = 20)
eFunnel(x)

R
1
2
funnelDF <- data.frame(namevar = c("Exposure", "Click", "Visit", "Query", "Buy"), datavar = c(100, 80, 50, 20, 10), stringsAsFactors = T)
eFunnel(funnelDF, ~namevar, ~datavar, theme = 2)

平行坐标图

R
1
2
3
4
5
6
7
require(plyr)
parallelDf
axisList = list(
list(index = 7, type = "category", data = c("low", "middle", "high")),
list(index = 6, inverse = T, max = 50, nameLocation = "start")
)
eParallel(head(parallelDf, 20), series = ~groupName, axisList = axisList)

雷达图

R
1
2
3
4
5
require(plyr)
dat <- ddply(iris, .(Species), colwise(mean))
rownames(dat) <- dat[, 1]
dat <- dat[, -1]
eRadar(dat, theme = 1)

R
1
eRadar(df2, ~weekDay, ~saleNum, ~seller, theme = 1)

R
1
2
3
4
5
6
7
8
9
dat <- data.frame(IE8 = (40 - 1:28) * 10,
IE9 = (38 - 1:28) * 4 + 60,
Safari = 1:28 * 5 + 10,
Firefox = 1:28 * 9,
Chrome = 1:28 * 1:28 / 2)
row.names(dat) <- 2001:2028
chart <- eRadar(dat, ymax = rep(400, 5))
chart$x$visualMap = list(color = c("red", "yellow"))
chart

水球图

说句心里话,这个水球图我是不能接受的,连我的Stata命令——wball都不如。
水球图中的每个水波都是对应一个数据的,而不应该是一个简单的数字就绘制出的。

R
1
eLiquid(0.6, wave = 6)

桑基图

R
1
2
3
4
5
6
7
8
9
10
11
dat = data.frame(source=c("Agricultural 'waste'","Bio-conversion",
"Bio-conversion","Bio-conversion","Bio-conversion",
"Biofuel imports","Biomass imports","Coal imports",
"Coal reserves","Coal","District heating","District heating",
"District heating","Electricity grid","Electricity grid"),
target=c("Bio-conversion","Liquid","Losses","Solid","Gas","Liquid",
"Solid","Coal","Coal","Solid","Industry","Heating and cooling - commercial",
"Heating and cooling - homes","Over generation / exports","Heating and cooling - homes"),
value=c(124.729,0.597,26.862,280.322,81.144,35,35,11.606,63.965,75.571,
10.639,22.505,46.184,104.453,113.726))
eSankey(dat)

词云

R
1
eWordcloud(wordFreqDf_chs, namevar = ~Word, datavar = ~Freq)

地图

R
1
2
3
4
mapData <- data.frame(stdName = c("安徽", "江苏", "北京", "上海", "广州"),
val1 = c(10, 20, 30, 40, 50),
val2 = c(12, 34,54, 43, 23))
eMap(mapData, name = ~stdName, datavar = ~val1 + ~val2)

补充函数

MarkPoint

R
1
2
3
dat <- cut(rnorm(1000), -4:4)
chart = eLine(dat)
eMarkPoint(chart, type = c('max', 'min'), valueIndex = 1)

R
1
2
3
4
5
eMarkPoint(chart, dat = data.frame(2:3, rep(200, 2)), type = c('max', 'min'), valueIndex = 1)
data.frame(2:3, rep(200, 2))
# X2.3 rep.200..2.
# 1 2 200
# 2 3 200

HLine

R
1
2
chart = ePoints(iris[, 3:5], theme = 2)
eHLine(chart, 1, lineColor = 'Blue')

R
1
eHLine(chart, 'min', lineWidth = 5)

VLine

R
1
eVLine(chart, 2, lineColor = 'blue')

R
1
eVLine(chart, 'max', lineColor = 'blue', lineWidth = 5)

MarkLine

R
1
eMarkLine(chart, c(1, 0), c(7, 2.5))

eTimeLine

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
x = runif(6)
names(x) <- LETTERS[1:6]
e1 = ePie(x) + eTitle("test1")
x = runif(6)
names(x) <- LETTERS[1:6]
e2 = ePie(x) + eTitle("test2")
x = runif(6)
names(x) <- LETTERS[1:6]
e3 = ePie(x) + eTitle("test3")
x = runif(6)
names(x) <- LETTERS[1:6]
e4 = ePie(x) + eTitle("test4")
x = runif(6)
names(x) <- LETTERS[1:6]
e5 = ePie(x) + eTitle("test5")
x = runif(6)
names(x) <- LETTERS[1:6]
e6 = ePie(x) + eTitle("test6")
eTimeline(e1, e2, e3, e4, e5, e6)

从R对象中绘图(ePlot)

这一部分就很没意思了,代码复杂程度快赶上了直接用ECharts绘图了。也懒得看了。

# R

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了604.4k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×