I want to plot 2 variables of different scales. A secondary axis would be my ideal solution. Does anybody know how to implement this in rCharts.
There is a related post using Highcharts: rCharts - Adding second y axis to time series but I wonder how to get this using nPlot in rCharts?
library(rCharts)
set.seed(1)
df <- data.frame(date=as.Date("2014-01-01")+1:10,y1=sample(1:200,70),y2=runif(70))
buildchart <- function(df) {
plotData<-df
temp<-aggregate(cbind(y1)~date,data=df,min)
tabledta<-summaryBy(y2~date,data=df,FUN=sum,keep.names = TRUE)
tabledta<-merge(tabledta,temp,by=c("date"),all.x=T)
filler = expand.grid(date=seq(as.Date(min(tabledta$date)),max(as.Date(tabledta$date)),by='1 day'))
df = merge(filler,
tabledta,
by=c('date'),
all.x=T)
df[is.na(df)]=0
data.melt<-melt(df,id.vars=c('date'),all=TRUE)
p <- nPlot(value ~ date, group = 'variable', data = data.melt, type = 'lineChart')
p$chart(margin=list(left=150))
p$yAxis(showMaxMin = FALSE)
p$xAxis(tickFormat ="#!function(d) {return d3.time.format('%Y-%m-%d')(new Date(d * 24 * 60 * 60 * 1000));}!#")
p
}
buildchart(df)