echart制图网上的资源

why echart

参考github

教学文档

下载这些包

一个是remap包,画地图高手,还有就是我们的主角recharts包啦

library(devtools)
install_github("Lchiffon/REmap")
require(devtools)
devtools::install_github("cosname/recharts")

rechart绘制出的图,因为可以在html上互动,获得了极大的好评

开始探索

加载

library(recharts)

1.构建基本对象

在r里运行js

即使是java也跑给你看!

JScodes = "
var option = {
title: {
text: 'ECharts entry example'
},
tooltip: {},
legend: {
data:['Sales']
},
xAxis: {
data: ['shirt','cardign','chiffon shirt','pants','heels','socks']
},
yAxis: {},
series: [{
name: 'Sales',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
"
echartsExample(JScontent=JScodes)

上面的例子同样可以用r构建

# 定义数据部分
series = list(list(
  name = 'Sales',
  type = 'bar',
  data = c(5, 20, 36, 10, 10, 20)
))
# 定义坐标轴部分
ext = list(
  xAxis = list(list( 
    data = c('shirt','cardign','chiffon shirt','pants','heels','socks')
  )),
  yAxis = list(list())
)
# 绘制
ePlot(series, ext)

线柱混合图

series2 = list(
  list(
    name = '蒸发量',
    type = 'bar',
    data = c(2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3)
  ),
  list(
    name = '降水量',
    type = 'bar',
    data = c(2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3)
    
  ),
  list(
    name = '平均温度',
    type = 'line',
    yAxisIndex = 1,
    data = c(2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2)
    
  )
)

ext = list(
  xAxis = list(list(
    type = "category",
    data = c("1月", "2月", "3月", "4月", "5月", "6月",
             "7月", "8月", "9月", "10月", "11月",  "12月")
  )),
  yAxis = list(
    list(
      type =  'value',
      name =  '水量',
      min =  0,
      max =  250,
      interval =  50,
      axisLabel =  list(
        formatter =  '{value} ml'
      )
    ),
    list(
      type =  'value',
      name =  '温度',
      min =  0,
      max =  25,
      interval =  5,
      axisLabel = list(
        formatter= '{value}℃'
      )
    )
  ))
ePlot(series2, ext )

基本绘图函数

1 point

ePoints(iris[, 3:5], theme = 2)
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)
ePoints(iris[,3:5], xvar = ~Petal.Length, 
        yvar = ~Petal.Width, series = ~Species, theme = 6)

2 line

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=1)

第二种 input.

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 =FALSE
)
eLine(df2, xvar=~weekDay, yvar= ~saleNum, series=~seller)
dat <- cut(rnorm(1000), -4:4)
eLine(dat)

3 条形图

此处还需借用plyr包,也是可视化的利器(人称碧玉刀)

require(plyr)
## Loading required package: plyr
dat = ddply(iris, .(Species), colwise(mean))  
rownames(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)
eBar(dat, horiz = TRUE) #横纵坐标互换

第二种 input.

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 =FALSE
)
dat <- df2
xvar=~weekDay; yvar= ~saleNum; series=~seller
### 分组条形图
eBar(df2, xvar = ~seller, ~saleNum, ~weekDay ) 
### 累积图
eBar(df2, xvar = ~seller, ~saleNum, ~weekDay, stack=T) 
### 分组累积图
eBar(df2, xvar = ~seller, ~saleNum, ~weekDay, stackGroup = list(c('Sat','Sun')))
dat <- df2[1:7,]
eBar(dat, ~weekDay, ~saleNum)
dat <- cut(rnorm(1000), -4:4) ### 用cut函数将取值划分为区间
head(dat)
## [1] (-2,-1] (1,2]   (-1,0]  (0,1]   (0,1]   (0,1]  
## Levels: (-4,-3] (-3,-2] (-2,-1] (-1,0] (0,1] (1,2] (2,3] (3,4]
eBar(dat)

4.4 饼图

x = runif(6)
names(x) = LETTERS[1:6]
ePie(x) + eTitle("test")
testData <- head(mapTestData_chs, 5)
ePie(testData, ~stdName, ~val1)
x = runif(6)
names(x) = LETTERS[1:6]
ePie(x,reset_radius = c(80,120),showL = T,labelformatter = "{b}:{c}")

高级绘图函数

1 区域图

require(plyr)
eArea(WorldPhones, theme=1)

第二种 input.

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 =FALSE
)
eArea(df2, xvar=~weekDay, yvar= ~saleNum, series=~seller)
dat <- cut(rnorm(1000), -4:4)
eArea(dat)

2 K线图

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)
eCandle(mat, theme = 1)

3 漏斗图

x = c("Exposure" = 100, "Click" = 80, "Visit" = 60, "Query"=40, "Buy"=20)
eFunnel(x) +eTitle(title = "Funnel Plot")
funnelDf <- data.frame(namevar = c("Exposure", "Click", "Visit", "Query", "Buy"),
                       datavar = c(100, 80, 60, 40, 20), stringsAsFactors=FALSE)
eFunnel(funnelDf, ~namevar, ~datavar, theme =1)

4 平行坐标

require(plyr)

axisList = list(
  list(index=7, type="category", data = c("low", "middle", "high")),
  list(index=6, inverse=TRUE, max=50, nameLocation="start")
)
eParallel(head(parallelDf, 20), series=~groupName, axisList = axisList)

5 雷达图

require(plyr)
dat = ddply(iris, .(Species), colwise(mean))
rownames(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
eRadar(dat)
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))
)
dat <- df2
xvar=~weekDay; yvar= ~saleNum; series=~seller
eRadar(df2, ~weekDay, ~saleNum, ~seller)
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

6 水球图

个人认为最简单也最骚气的一个图

eLiquid(0.3)
eLiquid(0.6,wave = 6)

7 桑基图

用处很大的一种图

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)

8 词云

因为电脑原因,就不运行了w

太卡QAQ

eWordcloud(wordFreqDf_chs, namevar = ~Word, datavar = ~Freq)
head(wordFreqDf_chs)

9 地图

mapData <- head(mapTestData_chs, 5)
eMap(mapData, namevar=~stdName, datavar = ~val1 + val2)

以及省份的地图

provinceMapData <- mapTestData_chs[6:15,]
eMap(provinceMapData, namevar=~stdName, datavar = ~val1+val2, 
     region=unique(provinceMapData$motherName)[1])

补充函数

1 Markpoint

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

其实就是设置标志点

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

2 HLine & 3 VLine

添加辅助线

chart = ePoints(iris[, 3:5], theme = 2)
eHLine(chart,1,lineColor = 'blue')
eHLine(chart,'min',lineWidth = 5)
eVLine(chart,1,lineColor = 'blue')

4 MarkLine

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

5 eTimeline

带有时间轴的图

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")
eTimeline(e1,e2,e3,e4)