首先,加载recharts:

library(recharts)

1 介绍Introduction

setTheme是recharts的主要美工函数,可用来

  • 设置预置主题
  • 设置色版
  • 设置其他美工属性

2 用法Function Call

所有函数名都有小驼峰法和小写连划线(Hadly Wickham推荐此法)两种形式。

setTheme(chart, theme = c("asis", "default", "macarons", "infographic",
  "blue", "dark", "gray", "green", "helianthus", "macarons2", "mint", "red",
  "roma", "sakura", "shine", "vintage"), palette = "asis", bgColor = NULL,
  renderAsImage = FALSE, calculable = FALSE, calculableColor = NULL,
  calculableHolderColor = NULL, animation = TRUE, animationEasing = NULL,
  animationDuration = NULL, width = NULL, height = NULL, ...)

set_theme(chart, theme = c("asis", "default", "macarons", "infographic",
  "blue", "dark", "gray", "green", "helianthus", "macarons2", "mint", "red",
  "roma", "sakura", "shine", "vintage"), palette = "asis", bgColor = NULL,
  renderAsImage = FALSE, calculable = FALSE, calculableColor = NULL,
  calculableHolderColor = NULL, animation = TRUE, animationEasing = NULL,
  animationDuration = NULL, width = NULL, height = NULL, ...)
参数 要求

chart

echartRechart创建的Echarts对象.

theme

主题名或主题对象

  • 预置的主题
    • ‘default’, ‘macarons’, ‘infographic’, ‘blue’, ‘dark’, ‘gray’, ‘green’, ‘helianthus’, ‘macarons2’, ‘mint’, ‘red’, ‘roma’, ‘sakura’, ‘shine’, ‘vintage’
  • user-defined themes
    • 主题对象,列表形式,如list(color=c('#ff7f50', '#87cefa', '#da70d6', '#32cd32', '#6495ed'), title=list(x='left', y='top'), legend=list(orient='horizontal'))

palette

色版名或颜色向量。默认为NULL(echarts默认色版).

  • ‘asis’
    • 沿用当前echarts对象使用的主题
  • Echarts主题色版
    • “_default“,”_macarons“,”_infographic“,”_blue“,”_dark“,”_gray“,”_green“,”_helianthus“,”_macarons2“,”_mint“,”_red“,”_roma“,”_sakura“,”_shine“,”_vintage"
  • RColorBrewer色版
    • ‘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色版
    • ‘calc’, ‘economist’, ‘economist_white’, ‘economist_stata’, ‘excel’, ‘exel_fill’, ‘excel_line’, ‘excel_new’, ‘few’, ‘fivethirtyeight’, ‘538’, ‘manyeyes’, ‘gdocs’, ‘pander’, ‘tableau’, ‘stata’, ‘stata1’,‘stata1r’,‘statamono’, ‘ptol’, ‘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’
  • 其他
    • ‘rainbow’, ‘terrain’, ‘topo’, ‘heat’, ‘cm’

用法

  • 忽略不设,使用函数默认
  • palette=NULL,使用Echarts默认
  • palette=palette name,使用上述色版名
  • palette=palette name(number)限定色版内取色个数 (如palette='calc(3)'可随机从’calc’色版取3种颜色)
  • palette=c(color1, color2, color3, ...)定义一个自定义色版,该向量都要用颜色名或十六进制颜色值。用colors()查看合法颜色名,用demo(colors)查看实际效果.

bgColor

背景颜色名/值,默认透明 (‘rgba(0,0,0,0)’)

renderAsImage

逻辑型。如为TRUE,交互效果被禁用。默认为FALSE.

calculable

逻辑型。如为TRUE,启用拖曳重算。默认为FALSE.

calculableColor

拖曳重算提示边框颜色,默认为’rgba(255,165,0,0.6)’.

calculableHolderColor

可计算占位的颜色,默认为’#ccc’.

animation

逻辑型。如为TRUE, 起始时显示动画。IE8最好设为FALSE.

animationEasing

主要元素的微小移动效果,默认为’ExponentialOut’. 其他选择有 ‘Linear’, ‘QuadraticIn’, ‘QuadraticOut’, ‘QuadraticInOut’, ‘CubicIn’, ‘CubicOut’, ‘CubicInOut’, ‘QuarticIn’, ‘QuarticOut’, ‘QuarticInOut’, ‘QuinticIn’, ‘QuinticOut’, ‘QuinticInOut’, ‘SinusoidalIn’, ‘SinusoidalOut’, ‘SinusoidalInOut’, ‘ExponentialIn’, ‘ExponentialInOut’, ‘CircularIn’, ‘CircularOut’, ‘CircularInOut’, ‘ElasticIn’, ‘ElasticOut’, ‘ElasticInOut’, ‘BackIn’, ‘BackOut’, ‘BackInOut’, ‘BounceIn’, ‘BounceOut’, ‘BounceInOut’

animationDuration

多久后进入动画,默认为2000 (ms).

width

图的宽度(px),默认为NULL (自动).

height

图的高度(px),默认为NULL (自动).

省略号

2.1 自定义主题User-defined Theme (UDT)

可以通过http://echarts.baidu.com/echarts2/doc/example/themeDesigner.html构建自定义主题。

echarts的自定义主题是一个JS对象。可以用列表形式改写,setTheme会把它解析成JSON字符串。

一个典型的JS主题结构:

{
    color: ['#ff7f50','#87cefa','#da70d6','#32cd32','#6495ed'], 
    title: {x: 'left', y: 'top'}, 
    legend: {orient: 'horizontal'} 
}

可以写成下述形式:

list(
     color=c('#ff7f50','#87cefa','#da70d6','#32cd32','#6495ed'), 
     title=list(x='left', y='top'), 
     legend=list(orient='horizontal')
)

3 举例Showcase

3.1 内置主题Pre-installed Themes

3.1.1 默认default

g <- echartr(mtcars, wt, mpg, carb, weight=hp, z=am, type='bubble') %>% 
    setSymbols('circle')
g

3.1.2 macarons

g %>% setTheme('macarons')

3.1.3 infographic

g %>% setTheme('infographic')

3.1.4 blue

g %>% setTheme('blue')

3.1.5 dark

g %>% setTheme('dark')

3.1.6 gray

g %>% setTheme('gray')

3.1.7 green

g %>% setTheme('green')

3.1.8 helianthus

g %>% setTheme('helianthus')

3.1.9 macarons2

g %>% setTheme('macarons2')

3.1.10 mint

g %>% setTheme('mint')

3.1.11 red

g %>% setTheme('red')

3.1.12 roma

g %>% setTheme('roma')

3.1.13 sakura

g %>% setTheme('sakura')

3.1.14 shine

g %>% setTheme('shine')

3.1.15 vintage

g %>% setTheme('vintage')

3.2 自定义主题User-defined Theme

写一个自定义主题。

udt <- list(
    backgroundColor="#d5e4eb", grid=list(borderWidth=0),
    categoryAxis=list(axisLine=list(lineStyle=list(width=2)),
                      splitLine=list(lineStyle=list(width=1, color='white'))),
    valueAxis=list(splitLine=list(lineStyle=list(width=1, color='white')),
                   axisLine=list(lineStyle=list(width=0)))
)
g %>% setXAxis(splitLine=list(show=FALSE)) %>% setTheme(udt)

3.3 色版Palette

3.3.1 主题色版Theme Palettes

所有内置主题都带色版。如果你不想套用整个主题,只想要它的色版,可以设置palette='_theme name'。记住,要在主题名前加一个下划线“_“。

g %>% setTheme(palette="_dark")

3.3.2 RColorBrewer色版Palettes

可以用RColorBrewer色版: ‘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’.

g %>% setTheme(palette='PRGn')

3.3.3 ggthemes色版Palettes

也可用ggthemes色版: ‘calc’, ‘economist’, ‘economist_white’, ‘economist_stata’, ‘excel’, ‘exel_fill’, ‘excel_line’, ‘excel_new’, ‘few’, ‘fivethirtyeight’, ‘538’, ‘manyeyes’, ‘gdocs’, ‘pander’, ‘tableau’, ‘stata’, ‘stata1’,‘stata1r’,‘statamono’, ‘ptol’, ‘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’.

g %>% setTheme(palette='tableau20')

3.3.4 其他色版Other Palettes

还可以用其他色版: ‘rainbow’, ‘terrain’, ‘topo’, ‘heat’, ‘cm’.

g %>% setTheme(palette='terrain')

3.3.5 自定义色版User-defined Palettes

也可以自己写一个色版向量,比如:

  • c(‘red’, ‘yellow’, ‘green’)
  • c(‘#FF0000’, ‘#FFFF00’, ‘#00FF00’)
g %>% setTheme(palette=c(
    'red', 'yellow', 'orange', 'limegreen', 'cyan', 'violet'))

3.4 其他美工属性Other Aesthetics

3.4.1 背景色bgColor

g %>% setTheme(bgColor='darkblue')

3.4.2 静态图片renderAsImage

禁用交互效果。

g %>% setTheme(renderAsImage=TRUE)

3.4.3 拖曳重算calculable

用饼图为例。试着拖走一个扇形。

echartr(mtcars, am, hp, type='ring') %>% 
    setTheme(calculable=TRUE, calculableColor='yellow', 
             calculableHolderColor='gold')

3.4.4 动画animation

g %>% setTheme(animation=TRUE, animationEasing='BounceInOut',
               animationDuration=500)

3.4.5 宽度width和高度height

g %>% setTheme(width=400, height=300)