【问题标题】:Plot area in rcharts NVD3 lineChartrcharts NVD3 lineChart中的绘图区域
【发布时间】:2015-05-27 18:52:03
【问题描述】:

我想使用http://nvd3.org/examples/line.html 中的 area=true 选项使用 rCharts 的 NVD3 lineChart 图绘制不同人群的分布情况。

这是我的工作:

require(devtools)
install_github('ramnathv/rCharts')
require(rCharts)

df<-data.frame(X=rep(1:4,2),Y=1:8,fil=c(rep("A",4),rep("B",4)))

denp <- nPlot(Y ~ X, group = 'fil', data = df, type = 'lineChart')
denp$chart(color =c('#ff7f0e', 'blue', 'green'))
denp$yAxis(axisLabel= 'Density')
denp$xAxis(axisLabel= 'Value')
denp$chart(margin = list(left=80,bottom=80))
denp$yAxis(tickFormat = "#!function (x,y,e) { return }!#")
denp$xAxis(tickFormat = "#!function (x,y,e) { 
tickformat = ['0,01','0,1',1,10,100,1000,10000,'100k'];
return tickformat[x+2];}!#")
denp$chart(tooltipContent = "#! function(key, val, e, graph){
return '<h3>' + '<font color=blue>'+ key +'</font>'+ '</h3>' + '<p>'+ val } !#")

denp

我发现的问题是我无法将 area 参数切换为 true。 我试过了:

denp$chart(area=TRUE)
denp$chart(area=c(TRUE,TRUE,TRUE))
denp$chart(area=c('true'))
denp$chart(area=c('true','true','true'))
denp$chart(area=c('#!true!#'))
denp$chart(area=c('#!true!#','#!true!#','#!true!#'))

所有的结果都是一个空白图。 有没有办法在 rCharts 中为这种类型的图表使用 area 选项,或者它目前超出了图书馆的范围吗?

【问题讨论】:

    标签: r nvd3.js area linechart rcharts


    【解决方案1】:

    将类型更改为'stackedAreaChart'

    这就是你所追求的吗?

    denp <- nPlot(Y ~ X, group = 'fil', data = df, type = 'stackedAreaChart')
    denp$chart(color =c('#ff7f0e', 'blue', 'green'))
    denp$yAxis(axisLabel= 'Density')
    denp$xAxis(axisLabel= 'Value')
    denp$chart(margin = list(left=80,bottom=80))
    denp$yAxis(tickFormat = "#!function (x,y,e) { return }!#")
    denp$xAxis(tickFormat = "#!function (x,y,e) { 
    tickformat = ['0,01','0,1',1,10,100,1000,10000,'100k'];
    return tickformat[x+2];}!#")
    denp$chart(tooltipContent = "#! function(key, val, e, graph){
    return '<h3>' + '<font color=blue>'+ key +'</font>'+ '</h3>' + '<p>'+ val } !#")
    
    denp
    

    如果您想组合图表类型(如您链接到的示例中),您必须使用 type = 'multiChart' 查看示例 here

    【讨论】:

    • 我正在寻找的是一个折线图,其中一条线的区域被填充,而另一条线没有。 “stackedAreaChart”适用于堆叠时间序列,即按段销售产品,当需要两个 y 轴时使用“multiChart”,即随时间推移的价格和销售额。就我而言,我希望两条线都使用相同的 y 轴,并且不需要数量来在另一条线上添加一个。
    【解决方案2】:

    这大致是您要找的吗?

    我通过添加行来实现这一点

    denp$chart(isArea=TRUE)
    

    到您的代码。看起来将 area boolean 设置为 true 的函数称为 isArea (documentation)。

    【讨论】:

    • 它更接近我正在寻找的东西。但是,我希望其中一个组是一个区域,另一个是一条线。我无法根据组将 isArea 设置为 TRUE 或 FALSE。
    【解决方案3】:

    您可以按照@seaotternerd 的建议使用isArea 函数,并使用自定义javascript 函数专门设置您想要设置为true 的区域参数。

    例如,使用:

    denp$chart(isArea="#! function(d) {
               if(d.key=='A') return true;
               } !#")
    

    这里d是数据。

    你得到:

    【讨论】:

    • 这就是我要找的。 Tahnks,我不确定是否可以在 isArea 中使用函数。
    猜你喜欢
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    相关资源
    最近更新 更多