nPlotの基本形は以下のような形であとはオプションを指定してカスタマイズしていきます。
library(rCharts)
n1 <- nPlot(data=mtcars, mpg~wt, group="cyl", type="scatterChart")
nPlotの基本形のパラメータはsetメソッドを使っても指定できます。
n1$set(group = "cyl", type = "scatterChart")
setメソッドの代わりにparamsに代入してもOKです。
n1$params$group <- "cyl"
n1$params$type <- "scatterChart"
指定できるパラメータは以下のような感じです。
names(n1$params)
## [1] "dom" "width" "height" "controls" "chart" "xAxis"
## [7] "x2Axis" "yAxis" "filters" "x" "y" "data"
## [13] "group" "type"
よく使うオプションについて使用例を示します。
下記は
としています。
コピペの際は、行末のカンマの抜け落ちに注意してください。
data(economics, package="ggplot2")
library(dplyr)
ecm <- economics %.%
select(date, psavert,uempmed) %.%
reshape2::melt(id.var="date")
# 列名はdate, variable, valueとなっている
n2 <- nPlot(data=ecm,
x="date", y="value", group="variable",
type="lineChart")
n2$set(
width=800, # 図の幅を設定
height=600, # 図の高さを設定
disabled=c(FALSE, TRUE) # 表示しないグループを設定
)
n2$chart(
color=c("grey","blue"),
# showLegend=FALSE, # 凡例の表示
margin=list(top=30, right=20, bottom=50, left=150),
# showControls=FALSE, #アニメーションコントロールの有無、コントロールがあるchartのみ
# showXAxis=FALSE, # 軸の表示
# showYAxis=FALSE,
# tooltips=FALSE, # ツールチップの表示
tooltipContent="#! function(key,x,y,e,graph)
{ return '<h3>'+key+'</h3><h4><p>x:'+x} !#", # ツールチップの内容(keyにはgroupで指定した内容が入る)
# forceX="#![0]!#",
forceY="#![-10,20]!#"
# transitionDuration=500, # アニメーションの速度
# showDistX=TRUE, # 垂線の表示scatterChartのみ
# showDistY=TRUE,
# showValues=TRUE, # 数値ラベルの表示barChartのみ
# donut=TRUE, # 円グラフの中心穴の有無、円グラフのみ
# donutRatio=40, # 円グラフの中心穴の有無、円グラフのみ
# 複数系列をまとめてツールチップで表示、line/areaChartのみ
# useInteractiveGuideline=TRUE
)
n2$xAxis(
axisLabel="date",
# staggerLabels = TRUE, # ラベルを入れ子にする
# tickValues="#![2000,10000]!#",
tickFormat = "#!function(d) {return d3.time.format('%Y')(new Date( d * 86400000 ));}!#"
)
n2$yAxis(
axisLabel="value",
tickValues="#![-5,0,5,10]!#"
# tickFormat = "#!function(d) {return d3.format('%')(d)}!#"
)
n2$show("iframesrc", cdn=TRUE)