Plot area in rcharts NVD3 lineChart
Asked Answered
P

3

10

I want to plot the distribution of different populations with rCharts' NVD3 lineChart plot using the area=true option like in http://nvd3.org/examples/line.html.

Here what I am working on:

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

The problem I find is that I am not able to switch the area parameter to true. I have tried:

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!#'))

The result in all of them is a blank plot. Is there a way to use the area option for this type of graph from within rCharts or is it beyond the library's reach currently?

Pierrette answered 24/3, 2015 at 12:45 Comment(0)
G
3

You can use the isArea function as @seaotternerd suggested and use a custom javascript function to specifically set which area parameter you want set to true.

For examples, using:

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

Here d is the data.

You get:

enter image description here

Gourd answered 4/5, 2015 at 7:29 Comment(1)
This is what I was looking for. Tahnks, I wasn't sure if I could use a function with isArea.Pierrette
M
3

Is this roughly what you're looking for?

enter image description here

I achieved this by adding the line

denp$chart(isArea=TRUE)

to your code. Looks like the function to set the area boolean to true is called isArea (documentation).

Mcguire answered 3/5, 2015 at 20:20 Comment(1)
It is closer to what I am looking for. However, I would like one of the groups to be an area and the other a line. I have not been able to set isArea to TRUE or FALSE depending on the group.Pierrette
G
3

You can use the isArea function as @seaotternerd suggested and use a custom javascript function to specifically set which area parameter you want set to true.

For examples, using:

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

Here d is the data.

You get:

enter image description here

Gourd answered 4/5, 2015 at 7:29 Comment(1)
This is what I was looking for. Tahnks, I wasn't sure if I could use a function with isArea.Pierrette
K
1

Change type to 'stackedAreaChart'

Is that what you are after?

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

If you want to combine chart types (like in the example you link to) you have to use type = 'multiChart' see an example here

Karakalpak answered 27/4, 2015 at 2:14 Comment(1)
What I am seeking for is a line chart in which one of the lines has its area filled while the other has not. 'stackedAreaChart' is intended for stacked time series i.e. sells of a product over time by segment and the 'multiChart' is used when two y-axis are required i.e. price and sales over time. In my case I want the same y-axis for both lines and I do not need quantities to add one on the other.Pierrette

© 2022 - 2024 — McMap. All rights reserved.