原文地址:http://madlogos.github.io/recharts/files/echartR.html#installation-

Function echartR with package recharts

Author: N303821

Edited: 9/10/2015 11:16:04 AM CST

Contents

  • 1 Intro 前言
  • 2 Usage 用法
    • 2.1 Installation 安装
    • 2.2 Grammar 语法
  • 3 Examples 示例
    • 3.1 Scatter 散点图

      • 3.1.1 Mono-series Scatter 单系列散点图
      • 3.1.2 Multi-series Scatter 多系列散点图
    • 3.2 Bubble 气泡图
      • 3.2.1 Mono-series Bubble 单系列气泡图
      • 3.2.2 Multi-series Bubble 多系列气泡图
    • 3.3 Column 柱图
      • 3.3.1 Tiled Column 平铺柱图
      • 3.3.2 Stacked Column 堆积柱图
    • 3.4 Bar 条图
      • 3.4.1 Tiled Bar 平铺条图
      • 3.4.2 Stacked Bar 堆积条图
    • 3.5 Histogram 直方图
    • 3.6 Pie 饼图
    • 3.7 Ring 环图
      • 3.7.1 Rose Nightingale玫瑰图
    • 3.8 Line 线图
      • 3.8.1 Unstacked Line 平铺线图
      • 3.8.2 Stacked Line 堆积线图
    • 3.9 Area 面积图
      • 3.9.1 Tiled Area 平铺面积图
      • 3.9.2 Stacked Area 堆积面积图
    • 3.10 Funnel 漏斗图
      • 3.10.1 Funnel 普通漏斗图
      • 3.10.2 Pyramid 金字塔图
    • 3.11 Radar 雷达图
      • 3.11.1 Hollow Radar 空心雷达
      • 3.11.2 Solid Radar 实心雷达
    • 3.12 Map 地图
      • 3.12.1 Area 区块标注
      • 3.12.2 Point 点标注
      • 3.12.3 Line 线标注
    • 3.13 Wordcloud 词云
    • 3.14 Chord 和弦图
      • 3.14.1 Simple chord 简单和弦图
      • 3.14.2 Multi-series chord 多系列和弦图
    • 3.15 Force 力导向布局图
    • 3.16 Candlestick K线图
    • 3.17 Gauge 仪表盘
  • 4 Recognized Issues 已知的问题
#Global settings
Sys.setlocale("LC_CTYPE","Chs")
#source("~/Github/recharts/R/echartR.R")
source("C:/HMSProjects/Data Analytics/R_scripts/CommonFunctions.R")
knitr::opts_chunk$set(message=FALSE,warning=FALSE,results='asis')

1 Intro 前言

本工具来源于百度开发的国内顶尖水平的开源d3-js可视项目Echarts(Github Repo)。Yang Zhou和Taiyun Wei基于该工具开发了recharts包,经Yihui Xie修改后,可通过htmlwidgets传递js参数,大大简化了开发难度。但此包开发仍未完成。为了赶紧上手用,基于该包做了一个函数echartR,用于制作基础Echart交互图。需要R版本>=3.2.0.

This tool originates from a top-tier d3-js visualization project of China: Baidu Echarts(Github Repo). Yang Zhou and Taiyun Wei developed an experimental R package recharts based on it, which then evoluted into yihui/recharts by Yihui Xie to pass js parameters through htmlwidgets. The package is sill uder development. I developed a function echartR based on this package to make basic Echarts interation charts. This function requires R>=3.2.0.

echartR的主要工作是将Echarts参数封装成list,Yihui Xie的原型函数echart被用来处理这个list。echart函数的基本用法如下,除了数据本身,并没有提供其他参数的设置方法。

echartR majorly packs Echarts parameters into a list while echart, the prototype function developed by Yihui Xie, is used to parse the list. The basic examples of echart is as follows, which does not provide parameters entries other than dataset itself.

if (! 'recharts' %in% installed.packages()[,1]){install.packages('recharts',repos = c('http://yihui.name/xran', 'http://cran.rstudio.com'))
}
library(recharts)
echart(iris, ~Sepal.Length, ~Sepal.Width)
echart(iris, ~Sepal.Length, ~Sepal.Width, series = ~Species)

echartR则主要细化了部分参数的设置方法,可以做出如下效果的动图:

2 Usage 用法

  • 首先,安装好最新版的R和Rstudio
  • 了解最基本的R语言技巧,能够用R整理、读写数据集
  • 最好会用knitr,会写rmarkdown

2.1 Installation 安装

2.2 Grammar 语法

echartR(data, x=NULL, y, z=NULL, series=NULL, weight=NULL, xcoord=NULL, ycoord=NULL, x1=NULL, xcoord1=NULL, ycoord1=NULL, type="scatter", stack=FALSE,title=NULL, subtitle=NULL, title_url=NULL, subtitle_url=NULL,symbolList=NULL, dataZoom=NULL, dataRange=NULL, splitNumber=NULL, dataRangePalette=NULL,xAxis=list(lab=NULL,color=NULL,splitLine=T,banded=F,rotate=0), xlab=NULL,yAxis=list(lab=NULL,color=NULL,splitLine=T,banded=F,rotate=0), ylab=NULL,xAxis1=list(lab=NULL,series=NULL,reverse=F,color=NULL,splitLine=T,banded=F,rotate=0), xlab1=NULL, yAxis1=list(lab=NULL,series=NULL,reverse=F,color=NULL,splitLine=T,banded=F,rotate=0), ylab1=NULL,xyflip=FALSE, AxisAtZero=TRUE, scale=TRUE,palette='aetnagreen', tooltip=TRUE, legend=TRUE, toolbox=TRUE, pos=list(title=6, legend=11, toolbox=1, dataZoom=6, dataRange=8, roam=2),calculable=TRUE, asImage=FALSE,markLine=NULL, markLinesmooth=NULL, markPoint=NULL, theme=list(backgroundColor=NULL, borderColor=NULL,borderWidth=1),...))
  • data: 数据集 dataset
  • x: x变量,直方图、饼图、环形图、玫瑰图可省略。x variable, only omitable for histograms, pie, ring and rose charts。
  • y: y变量 y variable
  • z: z变量,只接受时间/日期变量,并打开时间轴。z variable, only accept data/time variable to open time axis
  • series: Series(系列)变量 series variable
  • weight: 权重变量,可用于直方图、气泡图等 weight variable, used in histogram, bubble, etc
  • xcoord: 纬度坐标变量,仅用于点/线标注的map。Lattitude variable, only for point/line-marking map.
  • ycoord: 经度坐标变量,仅用于点/线标注的map。Longitude variable, only for point/line-marking map.
  • x1: 备用自变量,仅用于线标注的地图、力导向图和和弦图。Backup x variable, only for line-marking map, force andchord chart.
  • xcoord1: 备用纬度坐标变量,仅用于线标注的map。Backup lattitude variable, only for line-marking map.
  • ycoord1: 备用经度坐标变量,仅用于线标注的map。Backup Longitude variable, only for line-marking map.
  • type: 默认 default scatter,可选 options ‘scatter’, ‘bubble’, ‘bar’, ‘line’, ‘linesmooth’, ‘map’, ‘k’, ‘pie’, ‘ring’, ‘rose’,‘area’, ‘areasmooth’, ‘chordribbon’, ‘chord’, ‘force’, ‘tree’, ‘treemap’, ‘wordcloud’, ‘heatmap’, ‘histogram’, ‘funnel’, ‘pyramid’, ‘radar’, ‘radarfill’
    • 如选择map,则控制项必须写作一个长度为3的向量:c(‘map’,mapType,markType)。mapType可选’world’、‘china’,或简体中文表示的具体中国地名。markType为area时,用区块颜色表示效应大小;为point时,用点在地图上做标注;为line时,用线条在地图上做标注。默认为c(‘map’,‘china’,‘area’)。If map was chosen, the control option should be a vector of length 3: c(‘map’,mapType,markType). mapType could be either ‘world’ or ‘china’, of which simplified Chinese names are required for ‘china’. When markType equals to ‘area’, the function colors polygons to show the effects; while equals to ‘point’, it ticks pins on the map; while equals to ‘line’, it ticks lines on the map.
  • stack: 默认FALSE,是否堆积。用于制作堆积条图、柱图、线图和面积图等直角坐标系图形。Default to FALSE (do not stack). Used in stacked column, bar, line and area chart, etc.
  • title: 标题 title of the figure
  • subtitle: 副标题 subtitle of the figure
  • pos: 图形要素的位置,写作一个列表,用1-12点钟方向指示。标题默认6点位,即底部居中位置。图例、工具箱、值域漫游轴、数据缩放轴、地图漫游控件默认位置分别是11、1、6、8、2点钟位。Position of image elements which are integers 1-12. Title position default to 6 (o’clock), which means bottom middle. Defaults to legend, toolbox, dataRange, dataZoom, roamController are 11, 1, 6, 8, 2, respectively.
  • title_url: 标题链接,url of the title
  • subtitle_url: 副标题链接, url of the subtitle
  • symbolList: 图形标志。可使用数组循环使用,如数组长度小于series水平数,则以最后一个标志填充。如设置为NULL或不设置,则循环显示Echarts默认的标志图形列表:c(‘circle’,‘rectangle’,‘triangle’,‘diamond’,‘emptyCircle’,‘emptyRectangle’,‘emptyTriangle’,‘emptyDiamond’)。也可任意指定’heart’,‘droplet’,‘pin’,‘arrow’,‘star5’,‘star6’等非标图形。设为’none’则不显示。A vector assigning symbols. You can use an array of symbols. If the length of the symbols array is smaller than number of levels of the series, the last symbol will be used to extend the array. If you set symbolList NULL or leave it unset, the function circulates the default symbol list of Echarts: c(’circle’,‘rectangle’,‘triangle’,‘diamond’,‘emptyCircle’,‘emptyRectangle’,‘emptyTriangle’,‘emptyDiamond’). You can also assign non-standard symbols, such as ‘heart’,‘droplet’,‘pin’,‘arrow’,‘star5’,‘star6’, ‘star7’, etc. When assigned to ‘none’, no symbols are shown.
  • dataZoom: 数据缩放轴,默认FALSE. The axis to zoom data. Default to FALSE. 如dataZoom=TRUE,默认范围为0-100%。也可用一个长度为2的向量控制初始范围,如c(30,70)显示初始30-70%。If dataZoom=TRUE, the default range is 0-100%. You can assign a vector with length of 2 to dataZoom to control the initial range. E.g.,c(30,70) means from 30% to 70% at the initial view.
  • dataRange: 数据范围漫游范围,默认不打开。如要打开,设置dataRange=c(高值标签,低值标签) The range to zoom the data. Default to FALSE. Set dataRange=c(High value label,Low value label) to enable dataRange.
  • splitNumber: 如打开数据漫游,可指定数据范围切分段数,默认为连续漫游轴(0)。在直方图里,如设定splitNumber,则将数据切分成splitNumber个块。When dataRange is on, assign splitNumber to cut the range into discrete sections. Default to 0 (continuous range). In histogram, if splitNumber is set, the y variable will be cut into splitNumber groups.
  • dataRangePalette: 如打开数据漫游,可单独指定漫游色板(同palette功能),否则采用Echarts默认值。You can independently assign palettes to dataRange (similar to overall palette). Default to NULL (applies echarts defaults).
  • xAxis: x轴参数,写作一个列表,默认为list(lab=NULL,color=NULL,splitLine=T,banded=F,rotate=0),lab为标题,color为颜色,splitLine为分割线,banded为间隔区块,rotate为轴标签旋转(-90 ~ 90)。x Axis parameters in a list, default tolist(lab=NULL,color=NULL,splitLine=T,banded=F,rotate=0).
  • yAxis: y轴参数,参考xAxis。parameters of y Axis. Refer to xAxis.
  • xlab: 也可忽略xAxis项,单独通过xlab指定x轴标题。如xAxis[['lab']]xlab冲突,取xlab。You can also omit xAxis, directly assign xAxis title. xlab has a higher priority than xAxis[['lab']].
  • ylab: 也可忽略yAxis项,单独通过ylab指定y轴标题。如yAxis[['lab']]ylab冲突,取ylab。You can also omit yAxis, directly assign yAxis title. ylab has a higher priority than yAxis[['lab']].
  • xAxis1: 次级x轴参数,多系列数据时有效。写作一个列表,默认为list(lab=NULL,series=NULL,reverse=F,color=NULL,splitLine=T,banded=F,rotate=0)。其中,series指定放于次坐标轴的数据系列,可写系列名或序号,如c(1,2)或c(‘male’,‘female’);reverse设为TRUE时将数据颠倒显示。Secondary x axis, effecitve only if there are multiple series. Written in a list, default tolist(lab=NULL,series=NULL,reverse=F,color=NULL,splitLine=T,banded=F,rotate=0)series set which series to put on secondary x-axis, which can be either a name vector or an index vector, e.g., c(1,2) or c(‘male’,‘female’). The data is fliped up when reverse is set TRUE.
  • yAxis1:级次y轴参数,参考xAxis1。
  • xlab1:次级x轴标题,优先级高于xAxis1列表中的lab项。Title of secondary x-axis, with higher priority than lab in xAxis1 list,
  • ylab1:次级y轴标题,优先级高于yAxis1列表中的lab项。Title of secondary y-axis, with higher priority than lab in yAxis1 list,
  • xyflip: 默认FALSE,是否翻转坐标轴。Flip x,y-axies. Default to FALSE.
  • AxisAtZero: 默认FALSE,坐标轴是否交叉于零点。Axes cross at zero. Default to FALSE.
  • scale: 默认TRUE,是否基于最大、最小值调整坐标尺度。Rescale the axes based on min and max values. Default to TRUE.
  • palette: 调色板。默认aetnagreen。Overall palette. Default to ‘aetnagreen’.
    • 种类 Palette names:

      • Aetna palettes: 可用 Including ‘aetnagreen’, ‘aetnablue’, ‘aetnaviolet’, ‘aetnaorange’, ‘aetnateal’, ‘aetnacranberry’
      • RColorBrewer palettes: 可用 Including ‘BrBG’, ‘PiYG’, ‘PRGn’, ‘PuOr’, ‘RdBu’, ‘RdGy’, ‘RdYlBu’, ‘RdYlGn’, ‘Spectral’, ‘Accent’, ‘Dark2’, ‘Paired’, ‘Pastel1’, ‘Pastel2’, ‘Set1’, ‘Set2’, ‘Set3’, ‘Blues’, ‘BuGn’, ‘BuPu’, ‘GnBu’, ‘Greens’, ‘Greys’, ‘Oranges’, ‘OrRd’, ‘PuBu’, ‘PuBuGn’, ‘PuRd’, ‘Purples’, ‘RdPu’, ‘Reds’, ‘YlGn’, ‘YlGnBu’, ‘YlOrBr’, ‘YlOrRd’
      • ggthemes palettes: ‘calc’, ‘economist’, ‘economist_white’, ‘economist_stata’,‘excel’, ‘exel_fill’, ‘excel_old’, ‘excel_new’, ‘few’, ‘fivethirtyeight’, ‘gdocs’, ‘pander’, ‘tableau’, ‘stata’, ‘stata1’,‘stata1r’,‘statamono’, ‘tableau20’, ‘tableau10medium’, ‘tableaugray’, ‘tableauprgy’, ‘tableaublrd’, ‘tableaugnor’, ‘tableaucyclic’, ‘tableau10light’, ‘tableaublrd12’, ‘tableauprgy12’, ‘tableaugnor12’,‘hc’,‘darkunica’, ‘solarized’,‘solarized_red’, ‘solarized_yellow’, ‘solarized_orange’,‘solarized_magenta’,‘solarized_violet’, ‘solarized_blue’, ‘solarized_cyan’, ‘solarized_green’, ‘wsj’, ‘wsj_rgby’, ‘wsj_red_green’, ‘wsj_black_green’, ‘wsj_dem_rep’, ‘colorblind’, ‘trafficlight’
      • 其他Other palettes: ‘rainbow’, ‘terrain’, ‘topo’, ‘heat’, ‘cm’
    • 用法 Usage:
      • 可以不指定,使用函数默认。Do not set the value and function defaults will be loaded
      • 可以palette=NULL,使用Echarts默认。Set palette=NULL to use Echarts defaults
      • 可以palette=palette name指定上述任何一种色板。Set palette=palette name to assign any palette listed above
      • 规定色板的同时跟个数限定,限定色板颜色的个数,如palette='calc(3)',会从calc色板中随机取3种颜色。Setpalette=palette name(number) to restrict number of colors within the palette (e.g., palette='calc(3)' picks 3 colors out of ‘calc’ RANDOMLY)
      • 可以palette=c(color1,color2,color3,...)自定义色板向量,向量可以是颜色名,也可以是Hex表达式。可以用colors()函数查看所有支持的颜色名称,demo(colors)查看颜色效果。Set palette=c(color1,color2,color3,...) to define a palette vector, made of which either color names or Hex expressions. Use colors() to check available color names and check the effects using demo(colors).
  • tooltip: 默认TRUE,鼠标指针特效。Mouse tip effects swtich. Default to TRUE.
  • legend: 图例,默认TRUE。可以写作一个长度为2的列表,list(mode='single/multiple',select=选中的系列)。如list(mode='single',select='Male')表示单选,初始仅选中显示Male系列。legend=FALSE则不显示图例。Whether to show the legend. Default to TRUE. Can also be a list length 2: list(mode='single/multiple',select=vector(...)). E.g.,list(mode='single',select='Male') means singular choice, with series ‘Male’ selected at the initial view. legend=FALSE closes legend.
  • toolbox: 默认TRUE,是否显示工具箱。Echarts Tool box switch. Default to TRUE.
  • calculable: 默认TRUE,是否支持拖曳重算(Echarts专利) Calculable switch (Echarts patent).
  • asImage: 默认FALSE,是否显示为静态图。renderAsImage switch.Deafult to FALSE.
  • markLine: 显示标线,默认不显示。格式写作一个4或8列的数据框或矩阵 Show markline, default to NULL. The grammar is a data.frame or matrix with 4 or 8 columns:
    • 缩略格式 Short form :
    series name/index * line name Line type Light effect
    String or number String / NA min/max/average/lm TRUE / FALSE
    • 完整格式 Full form :
    series name/index * line name Value P0 x P0 y P1 x P1 y Light effect
    String or number String/NA num x val y val x val y val TRUE / FALSE
    • 例子 Examples
    1. t(c('male',NA,'average',F))t(c(1,NA,'average',F))都可表示male数据系列平均值标线,只用于line, linesmooth, bar, scatter, bubble。lm可出线性回归标线,只用于散点或气泡图。如t(c(1,NA,'average',T))则表示male系列开启炫光特效。E.g., both t(c('male',NA,'average',F)) and t(c(1,NA,'average',F)) refer to an average markline of the series ‘male’, only available for line, linesmooth, bar, scatter, bubble charts. ‘lm’ refers to linear regresson markline which is only available for scatters and bubbles. t(c(1,NA,'average',T)) opens light effects of series ‘male’.
    2. t(c('male',NA,100,0,5,100,5,F))表示在’male’数据系列中画一条经过P0(0,5)和P1(100,5)的直线。E.g.,t(c('male',NA,100,0,5,100,5,F)) refers to a markline through P0(0,5) and P1(100,5) as of sereis ‘male’. 在line, bar, k, scatter图中,‘P0 x’,‘P0 y’,‘P1 x’,‘P1 y’均被理解为直角坐标系的定位。在map中,这些坐标值必须写作经纬度。t(c('male',NA,100,0,5,100,5,T))可打开male系列的炫光特效。 In line, bar, k and scatter charts, ’P0 x’,‘P0 y’,‘P1 x’,‘P1 y’ are comprehended as coordinates. In map charts, these coordinates should be lattitudes and longitudes.t(c('male',NA,100,0,5,100,5,T)) opens light effects of series ‘male’.
  • markLinesmooth: 如要用平滑标线,可用markLinesmooth替代markLine。语法完全一样。Used this instead of markLine for smooth marklines with totally identical grammar.
  • markPoint: 显示标注点,默认不显示。格式写作一个4或6列的数据框或矩阵 Show markpoints, default to NULL. The grammar is a data.frame or matrix with 4 or 6 columns:
    • 缩略格式 Short form :
    series name/index * Point name Point type Light effect
    String or number String / NA min/max TRUE / FALSE
    • 完整格式 Full form :
    series name/index * Point name Value P x P y Light effect
    String or number String / NA num x val y val TRUE / FALSE
    • 例子 Examples
    1. t(c('male',NA,'min',F))t(c(1,NA,'min',F))都可表示male数据系列最小值标注,只用于line, linesmooth, bar, scatter, bubble。t('male',NA,'min',T)则表示male系列开启炫光特效。E.g., both t(c('male',NA,'min',F)) and t(c(1,NA,'min',F))refer to a min markpoint of the series ‘male’, only available for line, linesmooth, bar, scatter, bubble charts.t(c('male',NA,'min',T)) opens light effects of series ‘male’.
    2. t(c('male',NA,100,0,5,F))表示在’male’数据系列中标注点P(0,5)。E.g., t(c('male',NA,100,0,5,F)) refers to a markpoint at P(0,5) as of sereis ‘male’. 在line, bar, k, scatter图中,‘P x’,‘P y’, … 均被理解为直角坐标系的定位。在map中,这些坐标值必须写作经纬度。 t(c('male',NA,100,0,5,T))可打开male系列的炫光特效。 In line, bar, k and scatter charts, ‘P x’,‘P y’,… are comprehended as coordinates. In map charts, these coordinates should be lattitudes and longitudes.t(c('male',NA,100,0,5,T)) opens light effects of series ‘male’.
  • theme: 主题元素设置,语法为theme=list(backgroundColor=color name/value, borderColor=color name/value, borderWidth=1),color默认均为NULL,width默认为1。

3 Examples 示例

3.1 Scatter 散点图

3.1.1 Mono-series Scatter 单系列散点图

没有数据系列,因此指定显示第1个系列的均数线(markLine=c(1,NA,'average',F))。

echartR(data = iris, x = ~Sepal.Width, y = ~Petal.Width, type = 'scatter', palette='solarized_magenta',title = 'Scatter - Sepal Width vs Petal Width', subtitle = "(source: iris)", xlab = 'Sepal Width', ylab = 'Petal Width',markLine=t(c(1,'Mean',"average",F)))

3.1.2 Multi-series Scatter 多系列散点图

指定series,且显示范围从零点开始(scale=FALSE)。在第2个数据系列(versicolor)中打开最大(max)、最小(min)、均线(average)三根标线;在第1、2、3数据系列中分别标注max、min、max点,且打开第3个系列的眩光特效(markPoint=rbind(c(1,'Max','max',F),c(2,'Min','min',F), c(3,'Max','max',T)))。 通过xAxis和yAxis设置,去掉了所有纵轴。

echartR(data = iris, x = ~Sepal.Width, y = ~Petal.Width, series = ~Species,type = 'scatter', palette='wsj_dem_rep', symbolList='circle',scale=F, xAxis = list(lab='Sepal Width', color='darkgray'),yAxis = list(lab='Petal Width',color='none'),title = 'Scatter - Sepal Width vs Petal Width, by Species',subtitle ='(source: iris)', markLine=rbind(c(2,'Mean','average',F),c('versicolor','Maximum','max',F),c(2,'Minimum','min',F)),markPoint=rbind(c(1,'Max','max',F),c(2,'Min','min',F), c(3,'Max','max',T)))

使用三套非标准图形(箭头、心形、八角星)区分数据系列(c('arrow','heart','star8'))。命令指定随机选择excel_old色版中的1种颜色,但由于数据集有3个水平(series),颜色个数限制在执行中被忽略。

同时拟合3条回归线(markLine列表写作short form,类型指定为’lm’,并开启炫光特效)。

echartR(data = iris, x = ~Sepal.Width, y = ~Petal.Width, series = ~Species,type = 'scatter', palette='excel_old(1)',symbolList=c('arrow','heart','star8'),title = 'Scatter - Sepal Width vs Petal Width, by Species',subtitle = '(source: iris)', xlab = 'Sepal Width', ylab = 'Petal Width',markLine = rbind(c('setosa','Linear Reg Coef','lm',T), c(2,'Linear Reg Coef','lm',T), c(3,'Linear Reg Coef','lm',T)))

大规模散点图(2000个点以上)同样可以秒级出图(但html文件会变得很大)。

x <- rnorm(2001)*2
e <- vector()
for (i in 1:2001)  e <- c(e,rnorm(1,0,x[i]+abs(min(x))))
df <- data.frame(x, sin=sin(x)+e/20, cos=cos(x)+e/20)
df <- melt(df,id="x")
echartR(df,x=~x,y=~value,series=~variable,type='scatter',palette='wsj_red_green',symbolList='circle', AxisAtZero=F,title='Scatter of 2,001 points', subtitle = 'Large-scale scatter')

3.2 Bubble 气泡图

气泡图同样来源于散点图。type改为bubble即可,如不指定weight变量,函数默认指定y为气泡权重。

3.2.1 Mono-series Bubble 单系列气泡图

echartR(data = iris, x = ~Sepal.Width, y = ~Petal.Width, weight = ~Petal.Length,type = 'bubble', palette='solarized_cyan',title = paste("Bubble - Sepal Width vs Petal Width,","weighed by Petal Length"),subtitle = '(source: iris)',xlab = 'Sepal Width', ylab = 'Petal Width')

3.2.2 Multi-series Bubble 多系列气泡图

symbolList=c('circle','emptyCircle')设置第1个数据系列为实心圆、剩下的数据系列以空心圆作为标志图形。palette设置为tableauGnOr(3),只随机取该色板的3种颜色。

再做一个基于全集的线性回归(lm())。由于无法显示在任何一个系列上,因此echartR生成了一个新的数据系列。前一节多系列散点图中,三个系列Sepal Width与Petal Width都正相关,而本例中,合并起来两者却呈负相关。这个例子形象地展示了分层分析的重要性。

fit <- lm(Petal.Width~Sepal.Width,iris)
pred <- data.frame(Sepal.Width=c(min(iris$Sepal.Width),max(iris$Sepal.Width)))
yhat <- round(predict(fit,pred),2)
echartR(data = iris, x = ~Sepal.Width, y = ~Petal.Width, weight = ~Petal.Length, series = ~Species, symbolList=c('emptyCircle','circle'), type = 'bubble', palette='tableaugnor(4)',title = paste('Bubble - Sepal Width vs Petal Width, by Species,','weighed by Petal Length'), subtitle = '(source: iris)', xlab = 'Sepal Width', ylab = 'Petal Width',markLine=t(c('Reg Coef.','slope',round(fit$coefficients[[2]],2), pred[1,1],yhat[[1]],pred[2,1],yhat[[2]],F)))

3.3 Column 柱图

先生成一个汇总数据集dtiris

library(reshape2)
dfiris <- iris
dfiris$id <- row.names(iris)
dfiris <- melt(dfiris,id=c("Species","id"))
names(dfiris) <- c("Species","id","Param","Value")
dtiris <- dcast(dfiris[,c(1,3,4)],Species+Param~.,value.var="Value",mean)
names(dtiris) <- c("Species","Param","Mean")
knitr::kable(dcast(dtiris,Param~Species,sum,value.var="Mean"),format='html',caption="Table: Mean of parameters (iris)")
Table: Mean of parameters (iris)
Param setosa versicolor virginica
Sepal.Length 5.006 5.936 6.588
Sepal.Width 3.428 2.770 2.974
Petal.Length 1.462 4.260 5.552
Petal.Width 0.246 1.326 2.026

3.3.1 Tiled Column 平铺柱图

echartR(data = dtiris, x = ~Param, y = ~Mean,  series = ~Species,type = 'bar', palette='fivethirtyeight',title = "Column - Parameter Mean by Species", subtitle = '(source: iris)',pos=list(legend=3),xlab = 'Parameter', ylab = 'Mean')

3.3.2 Stacked Column 堆积柱图

echartR(data = dtiris, x = ~Param, y = ~Mean, series = ~Species, stack=T,type = 'bar', palette='pander',title = "Column - Parameter Mean by Species",subtitle = '(source: iris)',xlab = 'Parameter', ylab = 'Mean', pos=list(legend=3))

堆积与否,也可以简单地通过工具栏的平铺堆积按钮切换,非常强大。

3.4 Bar 条图

条图和柱图的区别只在于xyflip开关选项。

3.4.1 Tiled Bar 平铺条图

echartR(data = dtiris, x = ~Species, y = ~Mean, series = ~Param, type = 'bar',palette='stata1', xyflip=T, title = 'Species-specific Mean by Parameters',subtitle = '(source: iris)', pos=list(legend=3),xlab = 'Species', ylab = 'Mean')

3.4.2 Stacked Bar 堆积条图

palette设为calc的随机4种颜色。打开stack (stack=TRUE)。

echartR(data = dtiris, x = ~Param, y = ~Mean, series = ~Species, stack=T, xyflip=T, type = 'bar', palette='calc(5)',title = 'Parameter Mean by Species', subtitle = '(source: iris)',xlab = 'Parameter', ylab = 'Mean', pos=list(legend=3))

3.5 Histogram 直方图

直方图是柱图的一种特例,只需要指定y变量。可通过splitNumber指定直方数(默认9)。xyflip设为TRUE则成为等价的横条图。

echartR(airquality, y=~Temp, type='histogram', splitNumber=13,palette='pastel2', title='Histogram of temperature',subtitle = '(source: airquality)')

3.6 Pie 饼图

mtcars作为作图数据集。

dtcars <- mtcars
dtcars$car <- row.names(dtcars)
dtcars$transmission <- as.factor(dtcars$am)
levels(dtcars$transmission) <- c("Automatic","Manual")
dtcars$cylinder <- as.factor(dtcars$cyl)
dtcars$carburetor <-as.factor(dtcars$carb)
echartR(dtcars, x = ~transmission,  y = ~car, type='pie', palette='darkunica', title='Number of cars by transmission', subtitle = '(source: mtcars)')

3.7 Ring 环图

环形图是饼图的变形,只需将type改为’ring’。Echarts中只需要把饼图的半径参数扩展为包含内、外径的长度为2的向量即可。

echartR(dtcars, x = ~cylinder,  y = ~car, type='ring', palette='hc', title='Number of Cylinders', subtitle = '(source: mtcars)')

3.7.1 Rose Nightingale玫瑰图

echartR(dtcars, x = ~cylinder,  y = ~car, type='rose',palette='colorblind', title='Number of Cylinders',subtitle = '(source: mtcars)')

3.8 Line 线图

3.8.1 Unstacked Line 平铺线图

打开时间轴(z=~Month),按月动画显示每日风力数据。

#airquality$Date <- strptime(paste(2015,airquality$Month,airquality$Day,sep="-"),
#                            format="%F", tz="Asia/Taipei")
airquality$strDate <- with(airquality,paste(2015,Month,Day,sep="-"))
airquality$TempG <- cut(airquality$Temp,breaks=c(0,60,70,80,100))
airquality <- rbind(airquality,c(rep(NA,4),6,31),c(rep(NA,4),9,31))
# Jun, Sep has no 31th, suppl with NAs
echartR(airquality, x = ~Day, y= ~Wind, z=~Month, type='line', symbolList='none', palette='tableauBlRd', xlab = 'Days', ylab = 'Wind',title='Day-specific Wind by month (airquality)',pos=list(title=12,toolbox=3))

线段平滑(type='linesmooth'),不显示标志图形(symbolList='none')。此外开启双坐标轴(yAxis1),将系列1、2(Ozone、Solar.R)显示于次y轴。Echarts对缺失值默认不填补,因此有很多断线。需要在数据前处理时自行作插值运算。打开数据缩放,设置初始显示30-70%(dataZoom=c(20,50))。

airq <- melt(airquality[,c("Ozone","Solar.R","Wind","Temp","strDate")],id=c("strDate"))
#airQ <- melt(airquality[,c("Wind","Temp","Date")],
#             id=c("Date"))
echartR(airq, x = ~strDate, y= ~value, series= ~variable, type='linesmooth',symbolList='none', dataZoom=c(20,50),palette='tableauPrGy', ylab = 'Wind, Temp',xlab="Day",title='Climate measures by day', subtitle = '(source: airquality)',yAxis1=list(lab="Ozone, SolorR",series=c(1,2)))
#echartR(airQ, x = ~Date, y= ~value,  series= ~variable, type='linesmooth',
#        symbolList='none', dataZoom=T,
#        palette='tableauPrGy', xlab = 'Date', ylab = 'Measure',
#        title='Climate measures by day', subtitle = 'source: airquality')

打开时间轴(z=~Month),按月显示每日天气数据。

airq <- melt(airquality[c("Ozone","Solar.R","Wind","Temp","Month","Day")],id=c("Month","Day"))
echartR(airq, x= ~Day, y= ~value, series= ~variable, z= ~Month, type='linesmooth',symbolList='none', pos=list(toolbox=3),palette='tableauPrGy', xlab = 'Day', ylab = 'Measure',title='Climate measures by day', subtitle = '(source: airquality)')

3.8.2 Stacked Line 堆积线图

设置xAxis和yAxis,关闭所有纵线(color='none'),并显示横向间条(banded=T)。

echartR(dfiris, x = ~id, y= ~Value, series= ~Param, type='line',stack=T,palette='tableauBlRd12', xlab = 'Sample ID', ylab = 'Measure',yAxis=list(color='none',banded=T),title='Parameter measures', subtitle = '(source: iris)')

线段平滑,不显示标志图形

echartR(dfiris, x = ~id, y= ~Value, series= ~Param, type='linesmooth',stack=T,palette='tableauGnOr12', xlab = 'Sample ID', ylab = 'Measure',symbolList='none', yAxis=list(color='none'),title='Parameter measures', subtitle = '(source: iris)')
131
Petal.Width : 1.9
Petal.Length : 6.1
Sepal.Width : 2.8
Sepal.Length : 7.4

3.9 Area 面积图

Echarts中,面积图本质上被定义为线图,只需通过itemStyle参数渲染颜色。

3.9.1 Tiled Area 平铺面积图

echartR(dfiris, x = ~id, y= ~Value, series= ~Param, type='area',palette='brbg', xlab = 'Sample ID', ylab = 'Measure',symbolList='emptyDiamond',title='Parameter measures',yAxis=list(color='none'), subtitle = '(source: iris)')

线段平滑type='areasmooth',并打开数据缩放dataZoom=TRUE,初始显示40%-80%。

echartR(dfiris, x = ~id, y= ~Value, series= ~Param, type='areasmooth',palette='PiYG', xlab = 'Sample ID', ylab = 'Measure', symbolList='none', dataZoom=c(40,80),yAxis=list(color='none'),title='Parameter measures', subtitle = '(source: iris)')

3.9.2 Stacked Area 堆积面积图

echartR(dfiris, x = ~id, y= ~Value, series= ~Param, type='area',stack=T,palette='PRGn', xlab = 'Sample ID', ylab = 'Measure',symbolList='emptyCircle',yAxis=list(color='none'),title='Parameter measures', subtitle = '(source: iris)')

线段平滑(type='areasmooth')。自定义色板向量。

echartR(dfiris, x = ~id, y= ~Value, series= ~Param, type='areasmooth',stack=T,palette=c('red','yellow','limegreen','skyblue'), xlab = 'Sample ID', ylab = 'Measure', symbolList='none',yAxis=list(color='none'),title='Parameter measures', subtitle = '(source: iris)')

3.10 Funnel 漏斗图

3.10.1 Funnel 普通漏斗图

echartR(dtcars, x = ~carburetor,  y = ~car, type='funnel',palette='RdBu', title='Number of carburetors of cars',subtitle = '(source: mtcars)',pos=list(legend=12))

3.10.2 Pyramid 金字塔图

金字塔图即逆序漏斗图。

echartR(dtcars, x = ~carburetor,  y = ~car, type='pyramid',palette='RdGy', title='Number of carburetors of cars',subtitle = '(source: mtcars)',pos=list(legend=12))

3.11 Radar 雷达图

雷达图就是极坐标系下的线图/面积图,通过Echarts的polar参数模块控制。

3.11.1 Hollow Radar 空心雷达

browser <- as.data.frame(matrix(c(2001,390,208,15,9,0.5,2002,380,204,20,18,2,2003,370,200,25,27,4.5,2004,360,196,30,36,8,2005,350,192,35,45,12.5,2006,340,188,40,54,18,2007,330,184,45,63,24.5,2008,320,180,50,72,32,2009,310,176,55,81,40.5,2010,300,172,60,90,50,2011,290,168,65,99,60.5,2012,280,164,70,108,72,2013,270,160,75,117,84.5,2014,260,156,80,126,98,2015,250,152,85,135,112.5,2016,240,148,90,144,128,2017,230,144,95,153,144.5,2018,220,140,100,162,162,2019,210,136,105,171,180.5,2020,200,132,110,180,200,2021,190,128,115,189,220.5,2022,180,124,120,198,242,2023,170,120,125,207,264.5,2024,160,116,130,216,288,2025,150,112,135,225,312.5,2026,140,108,140,234,338,2027,130,104,145,243,364.5,2028,120,100,150,252,392),byrow=T,ncol=6))
names(browser) <- c("Year","IE8-","IE9+","Safari","Firefox","Chrome")
browser <- melt(browser,id="Year")
echartR(browser, x= ~variable, y= ~value, series= ~Year, type='radar',palette=paste0('heat(',nlevels(as.factor(browser$Year)),")"),pos=list(legend=9), symbolList='none',title='Browser Mkt Occup Ratio', subtitle= 'Totally virtual data')

3.11.2 Solid Radar 实心雷达

player <- data.frame(name=c(rep("Philipp Lahm",8),rep("Dani Alves",8)),para=rep(c("Passing%","Key passing","Comp crosses","Crossing%","Successful dribbles","Dispossessed","Dribbled past","Fouls"),2),value=c(89.67, 1.51, 0.97, 24.32, 0.83, 0.86, 1.15, 0.47,86.62, 2.11, 0.99, 20.78, 1.58, 1.64, 0.9, 1.71))
echartR(player, x= ~para, y= ~value, series= ~name, type='radarfill',symbolList='none', palette=c('firebrick1','dodgerblue'),title='Lahm vs Alves', subtitle= '(by @mixedknuts)')

3.12 Map 地图

R和Rstudio的中文转码在Windows里一直是老大难。在本文档,Rstudio的默认编码采用CP936编码,文档用UTF-8。如直接在程序内读数据集,用iconv转码后,落单的汉字仍然会显示为乱码。可先下载ChinaGDP.txt到本地,readLines读入。

最令人orz的地方是Chrome不能正确显示地图,变成了一堆锯齿。

#gdp <- readLines("https://raw.githubusercontent.com/madlogos/Shared_Doc/master/Shared_Documents/ChinaGDP.txt")
gdp <- readLines("ChinaGDP.txt")
dtgdp <- unlist(strsplit(gdp,split=","))
dtgdp <- as.data.frame(t(matrix(dtgdp,nrow=3)),stringsAsFactors=F)
names(dtgdp) <- c('Year','Prov',"GDP")
dtgdp$GDP <- as.numeric(dtgdp$GDP)
knitr::kable(dcast(dtgdp,Prov~Year,sum,value.var="GDP"), format='html',caption="Table: 2012-2014 GDP of Provinces in China (Million USD)")
Table: 2012-2014 GDP of Provinces in China (Million USD)
Prov 2012 2013 2014
福建 312107 351347 391609
甘肃 89508 101208 111273
北京 283238 314871 347249
安徽 272666 307416 339401
吉林 189136 209608 224715
江苏 856368 955269 1059587
江西 205131 231520 255724
广东 904046 1003746 1103605
广西 206497 232158 255144
贵州 108550 129284 150599
辽宁 393607 437216 466018
宁夏 37090 41417 44802
内蒙古 251574 271788 289274
青海 29997 33925 37460
新疆 118896 134991 150812
山东 792289 882974 967419
山西 191886 203485 207714
四川 378183 424026 464555
陕西 228969 259078 287978
上海 319710 348804 383554
西藏 11105 13041 14990
天津 204259 232031 255950
海南 45236 50805 56989
浙江 549154 606609 653668
云南 163318 189255 208612
河北 420990 456976 478953
河南 468900 519212 568786
重庆 180746 204364 232230
湖北 352482 398316 445514
湖南 350958 395622 440328
黑龙江 216896 232237 244829
dtgdp$Prov <- as.factor(enc2native(dtgdp$Prov))
dtgdp$Year<- as.factor(dtgdp$Year)

3.12.1 Area 区块标注

开启数据漫游,并定义色板。subtitle_url链接为本例数据源。通过markPoint参数也可以在区块标注地图中标注某些点,但效果并不是很好。

top3 <- dcast(dtgdp[dtgdp$Prov %in% c("广东","江苏","山东"),c("Prov","GDP")],Prov~., sum)
top3 <- cbind(top3,rep("Top3",3),c(32.04,23.16,36.65),c(118.78,113.23,117.00),rep(T,3))
top3 <- top3[,c(3,1,2,4:6)]
names(top3) <- c("Series","Prov","GDP","Xcoord","Ycoord","Effect")
for (i in 1:2) top3[,i]<-as.character(top3[,i])echartR(dtgdp, x = ~Prov, y = ~GDP, series= ~Year, type=c('map','china','area'), palette='gdocs',title="GDPs of China Provinces, 2012-2014 (Million USD)",subtitle='(source: Wikipedia)',subtitle_url="https://raw.githubusercontent.com/madlogos/Shared_Doc/master/Shared_Documents/ChinaGDP.txt",dataRangePalette=c('red','orange','yellow','green','limegreen'),dataRange=c('High',"Low"),pos=list(toolbox=3),markPoint=top3)

用世界GDP前20粗糙演示世界地图。漫游轴切分为10档(splitNumber=10)。

worldgdp <- data.frame(country=c('United States of America','China','Japan','Germany','United Kingdom','France','Brazil', 'Italy','India','Russia','Canada','Australia','South Korea','Spain','Mexico','Indonesia','Netherlands','Turkey','Saudi Arabia','Switzerland'),GDP=c(17418925,10380380,4616335,3859547,2945146,2846889,2353025,2147952,2049501,1857461,1788717,1444189,1416949,1406855,1282725,888648,866354,806108,752459,712050))
echartR(worldgdp, x = ~country, y = ~GDP, type=c('map','world','area'),title="Nations with top 20 GDPs, 2014 (Million USD)",subtitle = '(source: Wikipedia)', subtitle_url="https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)",dataRangePalette='rainbow(5)', dataRange=c("High","Low"), splitNumber=10, pos=list(toolbox=3))

3.12.2 Point 点标注

将China PM2.5数据集下载到本地,readLines读入。

#chinapm2 <- readLines('https://raw.githubusercontent.com/madlogos/Shared_Doc/master/Shared_Documents/China%20PM2.5.txt')
chinapm25 <- enc2native(unlist(strsplit(paste0(readLines("China PM2.5.txt"), collapse=","),",")))
chinapm25 <- as.data.frame(matrix(chinapm25,byrow=T,ncol=4),stringsAsFactors=F)
names(chinapm25) <- c("City","PM25","ycoord","xcoord")
for (i in 2:4) chinapm25[,i] <- as.numeric(chinapm25[,i])
top5 <- head(chinapm25[order(chinapm25$PM25,decreasing=T),],5)
top5$Name <- "Top 5"
top5$effect <- T
top5 <- top5[,c(5,1,2,4,3,6)]echartR(chinapm25, x=~City, y=~PM25, xcoord=~xcoord, ycoord=~ycoord,type=c('map','china','point'),title='PM2.5 in Chinese cities',subtitle="(source: PM25.in)",subtitle_url="http://pm25.in/",dataRange=c("High","Low"), pos=list(toolbox=3), dataRangePalette=c('maroon','red','orange','yellow','lightgreen','green'),markPoint=top5)

3.12.3 Line 线标注

线标注地图比较特殊,数据集要包括y(数量)、x(起点地名)、x1(终点地名)、series(数据系列)、xcoord(起点纬度)、ycoord(起点经度)、xcoord1(终点纬度)、ycoord1(终点经度)。额外标注的话,markLine数据集格式变为下表(第2列格式为’起点’/‘终点’):

series name/index * Path Value P0 x P0 y P1 x P1 y Light effect
String or number From/To num x val y val x val y val TRUE / FALSE

下载南航航班.txt到本地。readLines读入1-2行。 作点标注的时候,按markPoint语法将数据集处理成6列:series, name, value, xcoord, ycoord, effect。

flight <- readLines("CZflight.txt")[1]
flight <- as.data.frame(matrix(unlist(strsplit(flight,",")),byrow=T,ncol=2),stringsAsFactors=F)
names(flight) <- c("From","To")
flightCoord <- readLines("CZflight.txt")[2]
flightCoord <- as.data.frame(matrix(unlist(strsplit(flightCoord,",")),byrow=T,ncol=3),stringsAsFactors=F)
for (i in 2:3) flightCoord[,i] <- as.numeric(flightCoord[,i])
names(flightCoord) <- c("City","Ycoord","Xcoord")
flight <- merge(flight,flightCoord,by.x="From",by.y="City",all.x=T)
flight <- merge(flight,flightCoord,by.x="To",by.y="City",all.x=T)
Tier1 <- dcast(flight,From~.,length)
Tier1 <- merge(Tier1,unique(flight[,c("From","Xcoord.x","Ycoord.x")]),by="From",all.x=T)
Tier1$effect <- T
Tier1 <- Tier1[,c(1,1:5)]flight$y <- NA
echartR(flight, x=~From, x1=~To, y=~y, series=~From, xcoord=~Xcoord.x, ycoord=~Ycoord.x,xcoord1=~Xcoord.y, ycoord1=~Ycoord.y, type=c('map','china','line'),pos=list(toolbox=3), title="南方航空公司主要航班线路",markPoint=Tier1)

上个例子只作标线而未注数量。也可以对这些标线的数值进行渲染。仍以CZflight.txt为例,读取3-5行(我偷懒了,存在同一个文件里)。本例通过theme设置背景为灰黑色(#1b1b1b),边界线为暗蓝色royalblue4。legend中设置单选模式,初始选中数据系列’北京’。数据集处理的具体策略是x - ycoord1变量放背景数据集,包含所有迁徙关系;markLine放三个突出系列的数量,markPoint则放终点的数据。最后的效果同时包括了背景标线、突出标线和点标。全部打开炫光特效。

migrate <- as.data.frame(matrix(unlist(strsplit(readLines("CZflight.txt")[3],",")),byrow=T,ncol=2),stringsAsFactors=F)
names(migrate) <- c("From","To")
migrateCoord <- as.data.frame(matrix(unlist(strsplit(readLines("CZflight.txt")[4],",")),byrow=T,ncol=3),stringsAsFactors=F)
for (i in 2:3) migrateCoord[,i] <- as.numeric(migrateCoord[,i])
names(migrateCoord) <- c("City","Ycoord","Xcoord")
migrate <- merge(migrate,migrateCoord,by.x="From",by.y="City",all.x=T)
migrate <- merge(migrate,migrateCoord,by.x="To",by.y="City",all.x=T)
migrate$series <- "全国"
# markLine source data
migrateEm <- as.data.frame(matrix(unlist(strsplit(readLines("CZflight.txt")[5],",")),byrow=T,ncol=3),stringsAsFactors=F)
migrateEm[,3] <- as.numeric(migrateEm[,3])
names(migrateEm) <- c("From","To","NFlights")
#migrate <- merge(migrate,migrateEm,by=c("From","To"),all.x=T)
#migrate$Val[is.na(migrate$Val)] <- "-"
migrate$NFlights <- NA
migrateEm <- merge(migrateEm, migrateCoord, by.x="From",by.y="City",all.x=T)
migrateEm <- merge(migrateEm, migrateCoord, by.x="To",by.y="City",all.x=T)
# markLine dataset (8 col)
markline <- migrateEm[,c(2,1,3,5,4,7,6)]
markline$To <- paste(markline$From,markline$To,sep="/")
markline$effect <- T
# markPoint dataset (6 col)
markpoint <- migrateEm[,c(2,1,3,7,6)]
markpoint$effect <- T
# plot
echartR(migrate, x=~From, x1=~To, y=~NFlights, series=~series, xcoord=~Xcoord.x,ycoord=~Ycoord.x, xcoord1=~Xcoord.y, ycoord1=~Ycoord.y,type=c('map','china','line'), palette=c("Gray","Orange","Green","Purple"),pos=list(toolbox=3), title="百度迁徙(模拟数据)", dataRange=c("High","Low"),dataRangePalette=c("red","orange",'gold','green1','aquamarine2','royalblue4'),legend=list(mode='single',select=c('北京')), markLinesmooth=markline, markPoint=markpoint, theme=list(backgroundColor="#1b1b1b", borderColor="royalblue4",borderWidth=0.5))

3.13 Wordcloud 词云

直接knitr时readLines读取网页会报错,此例将百度热词榜网页 的源代码保存为本地txt后再读取。

#baiduhot <- paste0(readLines("http://top.baidu.com/buzz?b=1"),collapse="")
baiduhot <- paste0(readLines("Baidu Hot Words.txt"),collapse="")
hotword <- gsub(".+?<a class=\"list-title\"[^>]+?>([^<>]+?)</a>.+?<span class=\"icon-(rise|fair|fall)\">(\\d+?)</span>.+?","\\1\t\\3\t",baiduhot)
hotword <- enc2native(gsub("^(.+?)\t{4,}.+$","\\1",hotword))
hotword <- t(matrix(unlist(strsplit(hotword,"\t")),nrow=2))
hotword <- as.data.frame(hotword,stringsAsFactors=F)
names(hotword) <- c("Keyword","Freq")
hotword$Freq <- as.numeric(hotword$Freq)
hotword <- hotword[order(hotword$Freq,decreasing=T),]
knitr::kable(hotword, format='html', row.names=F,caption="Table: Baidu hot words Aug 18 (descending order by Freq)")
Table: Baidu hot words Aug 18 (descending order by Freq)
Keyword Freq
福州市长打篮球 411949
王思聪恋情坐实 331560
爆炸现场再次着火 225524
曼谷市中心爆炸 213843
居民要求回购房屋 208383
世界大学学术排名 201859
我军舰过对马海峡 200598
湖州惨烈车祸 167020
2万卖初夜反遭抢 107439
英国疯抢状元笔记 41624
开罗宣言海报惹议 40211
猴子街上遛弯 39791
长城现帐篷电影院 38504
天津爆炸致114死 38342
啤酒节饮酒过量 38311
网曝柏青孙子失联 30810
常州华光地产破产 17157
高空自行车断裂 15771
徐若瑄剖腹产子 12456
平度孤寡老人去世 12083
袁咏仪儿子上学 11577
山寨蜡像丑哭网友 11275
女子家门口被性侵 10732
将妻子带情敌身边 10180
天津爆炸头七 8037
幼师用脚猛夹男童 7855
朱泽君接受调查 7809
谎称自杀被救索赔 6962
最帅的逆行 6908
明星真人秀片酬 6637
唐嫣被指炒作恋情 6212
张艺兴已被涂黑 5954
跳蚤叮后染重病 5205
警方调查瑞海国际 4514
倪妮将当杨颖伴娘 4372
盖浇饭门店被查封 4202
美国化工厂爆炸 3677
刘谦升级当爸 3660
印尼飞机失联 3384
法国丈夫悼念亡妻 3316
英雄没有编外 2896
移民香港人数回落 2790
夏文兰病逝 2458
色诱处长遭停职 2399
马伊琍携女儿探班 2213
工人12年不上班 1915
郑州小伙出租云朵 1623
油价调整五连跌 1213
南充暴雨 1083
给孩子换学位 1023
echartR(hotword[1:30,], x=~Keyword, y=~Freq, type="wordcloud", title="Baidu Word Search Top 30", palette=NULL,title_url="http://top.baidu.com/buzz?b=1", subtitle="Tuesday, Auguest 18, 2015")

3.14 Chord 和弦图

全部借用Echarts的例图数据,略有改动。

3.14.1 Simple chord 简单和弦图

和弦图需要用到x,x1和y变量。其中x和x1是连接关系,y为权重。

3.14.1.1 Chord without ribbons 无缎带和弦图

本例用xAxis=list(rotate=90)将标签转向。

deutsch <- data.frame(player=c('Kruse','Kramer','Neuer','Boateng','Lahm','Kroos','Muller','Gotze','Badstuber','Hummels','Weidenfeller','Reus','Gundogan'),hire=c(rep('Monchengladbach',2),rep('Bayern',7),rep('Dortmund',4)),weight=rep(1,13), role=c('Fw','Mf','Gk','Df','Df','Mf','Mf','Fw','Df','Df','Gk','Df','Md'),stringsAsFactors=F)
echartR(deutsch,x=~player, y=~weight, x1=~hire, type='chord', xAxis=list(rotate=90),title='Deutsch Soccer Team - Clubs',pos=list(legend=10,title=5))

3.14.1.2 Chord with ribbons 缎带和弦图

echartR(deutsch,x=~player, y=~weight, x1=~hire, type='chordribbon', title='Deutsch Soccer Team - Clubs',pos=list(legend=10,title=5))

也可以对矩阵作图。矩阵数据 xi,j 表示i到j的关系数据。

grpmtx <- matrix(c(11975, 5871, 8916, 2868, 1951, 10048, 2060, 6171, 8010, 16145,8090, 8045, 1013, 990, 940, 6907),byrow=T,nrow=4)
dimnames(grpmtx) <- list(LETTERS[1:4],LETTERS[1:4])
knitr::kable(as.data.frame(grpmtx),format='html',caption="Table: Group A-D Mutual Links")
Table: Group A-D Mutual Links
  A B C D
A 11975 5871 8916 2868
B 1951 10048 2060 6171
C 8010 16145 8090 8045
D 1013 990 940 6907
grpmtx <- melt(grpmtx)
echartR(grpmtx,x=~Var1, y=~value, x1=~Var2, type='chordribbon', title='Group A-D mutual links',pos=list(legend=10,title=5))

3.14.2 Multi-series chord 多系列和弦图

下载中东格局数据集到本地,read.csv读入。 多系列和弦图必须用于两两之间关系。德国国家足球队的例子不适用。

mideast <- read.csv("MidEast.csv",header=T,stringsAsFactors=F)
names(mideast[,2:16]) <- mideast$X
mideast <- melt(mideast,id="X")
mideast$attd <- gsub("(.+)/\\d+","\\1",mideast$value)
mideast$attd[mideast$attd==''] <- NA
mideast$attd <- factor(mideast$attd,levels=unique(mideast$attd))
mideast$wt <- gsub(".+/(\\d+)","\\1",mideast$value)
mideast$wt <- as.numeric(mideast$wt)
echartR(mideast,x=~X, y=~wt, x1=~variable, series=~attd, type='chordribbon', title='Relationship in Mid-east', subtitle='(source: Caixin)', palette=c('#FBB367','#80B1D2','#FB8070','#CC99FF','#B0D961','#99CCCC','#BEBBD8','#FFCC99','#8DD3C8','#FF9999','#CCEAC4','#BB81BC','#FBCCEC','#CCFF66','#99CC66','#66CC66','#FF6666','#FFED6F','#ff7f50','#87cefa'), pos=list(legend=10,title=5,toolbox=2),subtitle_url='http://international.caixin.com/2013-09-06/100579154.html')

3.15 Force 力导向布局图

下载Network.txt到本地,readLines读入。 将x整理成关系起点/关系终点/关系名称格式,x1整理成关系起点赋值/关系终点赋值格式,series整理成起点节点类型/终点节点类型格式。

netNode <- as.data.frame(matrix(unlist(strsplit(readLines('Network.txt')[1],",")),byrow=T,ncol=3),stringsAsFactors=F)
names(netNode) <- c("name","category","value")
netLink <- as.data.frame(matrix(unlist(strsplit(readLines('Network.txt')[2],",")),byrow=T,ncol=4),stringsAsFactors=F)
names(netLink) <- c("from","to","relation","weight")
netLink$weight <- as.numeric(netLink$weight)
netLink <- merge(netLink,netNode,by.x="from",by.y="name",all.x=T)
netLink <- merge(netLink,netNode,by.x="to",by.y="name",all.x=T)
rm(netNode)
netLink$category.x <- factor(netLink$category.x,levels=c("Root","Node 1","Node 2","Node 3","Leaf"))  # Order the categories
netLink <- netLink[order(netLink$category.x),]
netLink$Link <- with(netLink,paste(from,to,relation,sep="/"))
netLink$NodeVal <- with(netLink,paste(value.x,value.y,sep="/"))
netLink$Series <- with(netLink,paste(category.x,category.y,sep="/"))

按此格式读入数据集作图。

echartR(netLink,x=~Link,y=~weight,x1=~NodeVal,series=~Series,type='force',title='绍兴俞氏社会网络',pos=list(title=5,legend=10),palette=c('brown','green4','green3','lawngreen','olivedrab1'))

3.16 Candlestick K线图

K线图必须将日期整理在x,开盘、收盘、最低、最高标签整理在x1(且按该顺序排序),价格整理在y。

stockidx <- data.frame(date=c('1/24','1/25','1/28','1/29','1/30','1/31','2/1','2/4','2/5','2/6','2/7','2/8','2/18','2/19','2/20','2/21','2/22','2/25','2/26','2/27','2/28','3/1','3/4','3/5','3/6','3/7','3/8','3/11','3/12','3/13','3/14','3/15','3/18','3/19','3/20','3/21','3/22','3/25','3/26','3/27','3/28','3/29','4/1','4/2','4/3','4/8','4/9','4/10','4/11','4/12','4/15','4/16','4/17','4/18','4/19','4/22','4/23','4/24','4/25','4/26','5/2','5/3','5/6','5/7','5/8','5/9','5/10','5/13','5/14','5/15','5/16','5/17','5/20','5/21','5/22','5/23','5/24','5/27','5/28','5/29','5/30','5/31','6/3','6/4','6/5','6/6','6/7','6/13'),open=c(2320.26,2300,2295.35,2347.22,2360.75,2383.43,2377.41,2425.92,2411,2432.68,2430.69,2416.62,2441.91,2420.26,2383.49,2378.82,2322.94,2320.62,2313.74,2297.77,2322.32,2364.54,2332.08,2274.81,2333.61,2340.44,2326.42,2314.68,2309.16,2282.17,2255.77,2269.31,2267.29,2244.26,2257.74,2318.21,2321.4,2334.74,2318.58,2299.38,2273.55,2238.49,2229.46,2234.9,2232.69,2196.24,2215.47,2224.93,2236.98,2218.09,2199.91,2169.63,2195.03,2181.82,2201.12,2236.4,2242.62,2187.35,2213.19,2203.89,2170.78,2179.05,2212.5,2227.86,2242.39,2246.96,2228.82,2247.68,2238.9,2217.09,2221.34,2249.81,2286.33,2297.11,2303.75,2293.81,2281.45,2286.66,2293.4,2323.54,2316.25,2320.74,2300.21,2297.1,2270.71,2264.43,2242.26,2190.1),close=c(2302.6,2291.3,2346.5,2358.98,2382.48,2385.42,2419.02,2428.15,2433.13,2434.48,2418.53,2432.4,2421.56,2382.91,2397.18,2325.95,2314.16,2325.82,2293.34,2313.22,2365.59,2359.51,2273.4,2326.31,2347.18,2324.29,2318.61,2310.59,2286.6,2263.97,2270.28,2278.4,2240.02,2257.43,2317.37,2324.24,2328.28,2326.72,2297.67,2301.26,2236.3,2236.62,2234.4,2227.74,2225.29,2211.59,2225.77,2226.13,2219.55,2206.78,2181.94,2194.85,2193.8,2197.6,2244.64,2242.17,2184.54,2218.32,2199.31,2177.91,2174.12,2205.5,2231.17,2235.57,2246.3,2232.97,2246.83,2241.92,2217.01,2224.8,2251.81,2282.87,2299.99,2305.11,2302.4,2275.67,2288.53,2293.08,2321.32,2324.02,2317.75,2300.59,2299.25,2272.42,2270.93,2242.11,2210.9,2148.35),low=c(2287.3,2288.26,2295.35,2337.35,2347.89,2371.23,2369.57,2417.58,2403.3,2427.7,2394.22,2414.4,2415.43,2373.53,2370.61,2309.17,2308.76,2315.01,2289.89,2292.03,2308.92,2330.86,2259.25,2270.1,2321.6,2304.27,2314.59,2296.58,2264.83,2253.25,2253.31,2250,2239.21,2232.02,2257.42,2311.6,2314.97,2319.91,2281.12,2289,2232.91,2228.81,2227.31,2220.44,2217.25,2180.67,2215.47,2212.56,2217.26,2204.44,2177.39,2165.78,2178.47,2175.44,2200.58,2232.26,2182.81,2184.11,2191.85,2173.86,2161.14,2179.05,2212.5,2219.44,2235.42,2221.38,2225.81,2231.36,2205.87,2213.58,2210.77,2248.41,2281.9,2290.12,2292.43,2274.1,2270.25,2283.94,2281.47,2321.17,2310.49,2299.37,2294.11,2264.76,2260.87,2240.07,2205.07,2126.22),high=c(2362.94,2308.38,2346.92,2363.8,2383.76,2391.82,2421.15,2440.38,2437.42,2441.73,2433.89,2443.03,2444.8,2427.07,2397.94,2378.82,2330.88,2338.78,2340.71,2324.63,2366.16,2369.65,2333.54,2328.14,2351.44,2352.02,2333.67,2320.96,2333.29,2286.33,2276.22,2312.08,2276.05,2261.31,2317.86,2330.81,2332,2344.89,2319.99,2323.48,2273.55,2246.87,2243.95,2253.42,2241.34,2212.59,2234.73,2233.04,2242.48,2226.26,2204.99,2196.43,2197.51,2206.03,2250.11,2245.12,2242.62,2226.12,2224.63,2210.58,2179.65,2222.81,2236.07,2240.26,2255.21,2247.86,2247.67,2250.85,2239.93,2225.19,2252.87,2288.09,2309.39,2305.3,2314.18,2304.95,2292.59,2301.7,2322.1,2334.33,2325.72,2325.53,2313.43,2297.1,2276.86,2266.69,2250.63,2190.1))
stockidx <- melt(stockidx,id="date")
stockidx <- stockidx[order(stockidx$variable),]
echartR(stockidx,x=~date,x1=~variable,y=~value,type='k',title='2013年上半年上证指数',dataZoom=c(0,50))
3/11
开盘 : 2314.68 最高 : 2320.96
收盘 : 2310.59 最低 : 2296.58

3.17 Gauge 仪表盘

仪表盘的数据集比较简单,标签列在x,数值在y,单位在x1。如果要自定义表盘颜色,可以在x列标记’axisStyle’,则x1列为颜色(Hex值或颜色名),y为切断点。此外可单独通过splitNumber制定刻度数量。

gauge <- data.frame(x=c("Completion",rep("axisStyle",3)),unit=c("%","forestgreen","orange","red2"),KPI=c(74,0.5,0.8,1))
echartR(gauge,x=~x,y=~KPI,x1=~unit,type='gauge')

4 Recognized Issues 已知的问题

  1. echartR比较接近于lattice,而不是ggplot2图层叠加的思路。每次使用都要一次性编辑所有需要的参数;
  2. 函数本身写得比较笨重,技术还不行;
  3. 当没有数据系列的时候,如显示图例,会被拆成一串单字节字符;
  4. 未实现的功能:
    1. candlestick(k)还没有开发;Tree不打算开发了;
    2. 仍然不支持时间格式的坐标轴(series中数据结构有问题);
    3. tooltip不够智能;
    4. 进阶功能(包括多图联动等)仍未开发;
    5. 不支持多类型混搭;
  5. 注意:如要改进,千万不要在函数代码中set.seed(),这会全局锁定种子数,导致knitr时每做一图都按该种子随机化htmlwidget id。最终的文档中,某些图可能会无法按指定代码出图,而是重复其他的图(串id)。

Function echartR with package recharts相关推荐

  1. Lua笔记——4.Package

    module简介: Lua 5.1 加入模块管理机制module,类似于Java的packages.C++的namespaces,可以通过require用于加载模块,module用于创建模块.requ ...

  2. Go语言系列——01-HelloWorld、02-命名规范、03-变量、04-类型、05-常量、06-函数(Function)、07-包、08-if-else语句、09-循环、10-switch语句

    文章目录 01-HelloWorld 一 建立 Go 工作区 二 运行 Go 程序 2.1 hello world 程序代码介绍 02-开发环境搭建 一 下载地址 二 安装 Linux安装 Windo ...

  3. R语言入门教学(3)-不懂就问help()的使用及package的安装

    1.什么是package? 当我们安装好Rstudio后,其实R以及安装好了很多自带的package. package如其字面意思,就是包,里面被打包了很多有用的东西,是已经被前人写好的方程,或者是被 ...

  4. Making fast, good decisions with the FFTrees R package

    "-[W]e are suspicious of rapid cognition. We live in a world that assumes that the quality of a ...

  5. Java8中Function函数式接口详解及使用

    文章目录 1.函数式接口 1.1允许定义默认方法 1.2允许定义静态方法 1.3允许定义java.lang.Object的public方法 1.4已有函数式接口 2.Function函数 2.1Fun ...

  6. 《R语言与数据挖掘》⑤高级绘图工具【lattice包】【ggplot2】【交互式】

    书籍:<R语言与数据挖掘> 作者:张良均 出版社:机械工业出版社 ISBN:9787111540526 本书由北京华章图文信息有限公司授权杭州云悦读网络有限公司电子版制作与发行 版权所有· ...

  7. Golang 要注意的陷阱和常见错误

    原文: 50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs 翻译: Go的50度灰:新Golang开发者要 ...

  8. 一个植物转录组项目的实战

    一个植物转录组项目的实战 Posted on 2017年11月2日 其实这个植物是拟南芥,所以跟人类研究的数据处理大同小异. 转录组 转录组测序的研究对象为特定细胞在某一功能状态下所能转录出来的所有 ...

  9. golang 开发常见坑

    目录 初级 开大括号不能放在单独的一行 未使用的变量 未使用的Imports 简式的变量声明仅可以在函数内部使用 使用简式声明重复声明变量 不能使用短变量声明来设置字段值 Can't Use Shor ...

最新文章

  1. 在Ubuntu 14.04 64bit上搭建Scala开发环境
  2. 为TextMate扩展全屏功能
  3. jQuery调用WCF需要注意的一些问题
  4. SGU155(笛卡尔树的构造)
  5. 时间序列研(part7)--单位根检验
  6. Android Studio 开发安卓软件时下载的工程项目 Sync with gradle 失败
  7. Qt笔记-拖动文件到QWidget(获取拖动文件路径)
  8. Linux下open函数、read函数、write函数记录
  9. 嵌入式视频处理基础(一)
  10. 3.中小型企业通用自动化运维架构 -- Ansible playbook
  11. hdu 1873“看病要排队”——优先队列的应用
  12. ansys19.2安装教程
  13. DW——验证注册页面 设计JavaScript
  14. 变速齿轮”再研究-----(转帖)
  15. 171021 逆向-Xp0intCTF(re300)
  16. 更改C盘用户文件夹名
  17. 39 个奇葩代码注释,看完笑哭了。
  18. XQ6657Z35-EVM 的DSP + ZYNQ核心板,SRIO通讯
  19. wordpress不同角色显示不同的菜单栏
  20. 获奖结果公布|2020腾讯犀牛鸟云开发校园技术布道师养成计划

热门文章

  1. Blender猫咪的头部骨骼绑定【Auto Rig Pro自我摸索0.1版本】
  2. Java利用while求一等比数列_UPC-6760 Problem H  九连环  【重庆OI2018】等比数列求和JAVA高精度...
  3. Cornerstone完整的基于 Web 的医学成像平台(一)
  4. boost 框架及基础类库的编译(FCL and BCL on Boost C++)
  5. 如何刷新本地的DNS缓存,使域名解析尽快生效?
  6. 在单位中,这五句话千万不能说
  7. UCOSII消息队列
  8. 2018湖南多校第一场 F、Fleecing the Raffle
  9. Image Optimizer Pro - Compres‪s for Mac(简单易用的图像压缩器)
  10. git安装及第一次上传代码遇到的问题