问题
I have a data.frame with dates and integers:
df <- data.frame(date=seq.Date(from=as.Date("2012/01/01"), to=as.Date("2012/02/28"), by="1 day"), y=1:59)
I want to plot the data.frame with rCharts nPlot:
n <- nPlot(y ~ date, group = "team", data = df, type = "lineChart")
n$xAxis(
tickFormat = "#!
function(d) {return d3.time.format('%Y-%M-%d')(new Date(d));}
!#",
rotateLabels = -90
)
n
But the x-axis isn't formatted correctly.
So how do I format the dates correctly?
回答1:
In R as.numeric(someDate) is days since origin. Here we need milliseconds from origin. So it should be
df <- data.frame(date=seq.Date(from=as.Date("2012/01/01", origin="1970-01-01"), to=as.Date("2012/02/28", origin="1970-01-01"), by="1 day"), y=1:59)
n <- nPlot(y ~ date, group = "team", data = df, type = "lineChart")
n$xAxis(
tickFormat = "#!
function(d) {return d3.time.format('%Y-%m-%d')(new Date(d*1000*3600*24));}
!#",
rotateLabels = -90
)
n
回答2:
%M is minute, what you want is %m
which will get you months:
n <- nPlot(y ~ date, group = "team", data = df, type = "lineChart")
n$xAxis(
tickFormat = "#!
function(d) {return d3.time.format('%Y-%m-%d')(new Date(d));}
!#",
rotateLabels = -90
)
n
来源:https://stackoverflow.com/questions/24344794/rcharts-nplot-formating-x-axis-with-dates