首先,加载recharts
:
library(recharts)
setTheme
是recharts的主要美工函数,可用来
所有函数名都有小驼峰法和小写连划线(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 |
|
theme |
主题名或主题对象
|
palette |
色版名或颜色向量。默认为NULL(echarts默认色版).
用法
|
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 (自动). |
… |
省略号 |
可以通过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')
)
g <- echartr(mtcars, wt, mpg, carb, weight=hp, z=am, type='bubble') %>%
setSymbols('circle')
g
g %>% setTheme('macarons')
g %>% setTheme('infographic')
g %>% setTheme('blue')
g %>% setTheme('dark')
g %>% setTheme('gray')
g %>% setTheme('green')
g %>% setTheme('helianthus')
g %>% setTheme('macarons2')
g %>% setTheme('mint')
g %>% setTheme('red')
g %>% setTheme('roma')
g %>% setTheme('sakura')
g %>% setTheme('shine')
g %>% setTheme('vintage')
写一个自定义主题。
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)
所有内置主题都带色版。如果你不想套用整个主题,只想要它的色版,可以设置palette='_theme name'
。记住,要在主题名前加一个下划线“_“。
g %>% setTheme(palette="_dark")
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')
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')
还可以用其他色版: ‘rainbow’, ‘terrain’, ‘topo’, ‘heat’, ‘cm’.
g %>% setTheme(palette='terrain')
也可以自己写一个色版向量,比如:
g %>% setTheme(palette=c(
'red', 'yellow', 'orange', 'limegreen', 'cyan', 'violet'))
bgColor
g %>% setTheme(bgColor='darkblue')
renderAsImage
禁用交互效果。
g %>% setTheme(renderAsImage=TRUE)
calculable
用饼图为例。试着拖走一个扇形。
echartr(mtcars, am, hp, type='ring') %>%
setTheme(calculable=TRUE, calculableColor='yellow',
calculableHolderColor='gold')
animation
g %>% setTheme(animation=TRUE, animationEasing='BounceInOut',
animationDuration=500)
width
和高度height
g %>% setTheme(width=400, height=300)
主题theme
http://echarts.baidu.com/echarts2/doc/example/theme.html
背景色backgroundColor
http://echarts.baidu.com/echarts2/doc/option.html#title~backgroundColor
颜色colors
http://echarts.baidu.com/echarts2/doc/option.html#title~color
http://colorbrewer2.org
其他属性other features
http://echarts.baidu.com/echarts2/doc/option.html#title~renderAsImage
http://echarts.baidu.com/echarts2/doc/option.html#title~calculable
http://echarts.baidu.com/echarts2/doc/option.html#title~animation
http://echarts.baidu.com/echarts2/doc/option.html#title~calculableColor
http://echarts.baidu.com/echarts2/doc/option.html#title~calculableHolderColor