Вопрос

I'm trying to plot a time series graph with nPlot and having difficulties in presenting the labels of the X-axis in a desirable way. I've been looking if this problem had come up before and it did but without solution yet (as far as i managed to find), I wonder if a solution is already available?

in this case i get the X-axis on a range between -1 and 1, and no lines on the graph:

date = c("2013-07-22", "2013-07-29" ,"2013-08-05", "2013-08-12", "2013-08-19","2013-08-26", "2013-09-02" ,"2013-09-09" ,"2013-09-16")
test = as.data.frame(date)
test$V1 = c("10","11","13","12","11","10","15","12","9")
test$V2 = c("50","51","53","52","51","50","55","52","59")     
test1 = melt(test,id = c("date"))

n1 = nPlot(value ~ date, group = "variable", data = test1, type="lineWithFocusChart") 

if I add and than plot again:

test1$date = as.Date(test1$date)

I get the wanted graph but the X-axis labels are in their numeric form (15900..)

Thanks.

Это было полезно?

Решение

Here is one way to make it work. I have made some changes to your code. One, I have made V1 and V2 numeric, since you want to be plotting numbers on the y axis. Second, I have added a utility function to_jsdate that takes the character date and converts it into a javascript date (number of milliseconds after 1970-01-01). Date handling is still a little raw in rCharts, but we are working on making it better.

date = c("2013-07-22", "2013-07-29" ,"2013-08-05", "2013-08-12", "2013-08-19",
  "2013-08-26", "2013-09-02" ,"2013-09-09" ,"2013-09-16")
test = as.data.frame(date)
test$V1 = as.numeric(c("10","11","13","12","11","10","15","12","9"))
test$V2 = as.numeric(c("50","51","53","52","51","50","55","52","59"))     
test1 = reshape2::melt(test,id = c("date"))

to_jsdate <- function(date_){
  val = as.POSIXct(as.Date(date_),origin="1970-01-01")
  as.numeric(val)
}


test1 = transform(test1, date2 = to_jsdate(date))

n1 = nPlot(value ~ date2, group = "variable", data = test1, type="lineWithFocusChart")
n1$xAxis(tickFormat = "#! function(d){ 
  return d3.time.format('%Y-%m-%d')(new Date(d*1000))
} !#") 
n1
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top