Mario Trento
Mario Trento

Reputation: 523

R. doing cool plot, no X labels

I can not get my month labels (let say Jan/15) on x. Tried all things, what the trick ? If I use YYYYMM it's kinda ok, but I need more readable. Is it possible to change background color too?

require(shiny)
require(rCharts)

x <- data.frame(Category=factor(c("Alpha", "Alpha","Alpha","Alpha","Alpha")), 
                    YYYYMM= factor(c("2/1/2015","3/1/2015","4/1/2015","5/1/2015","6/1/2015")),
                    COUNT=c(44,22,37,76,97))

str(x)
to_jsdate2 <- function(x){
  as.numeric(as.POSIXct(as.Date(x), origin="1970-01-01")) * 1000}

x$DATEPX <-  to_jsdate2(as.Date(x$YYYYMM))

myplot <- hPlot(COUNT ~ YYYYMM, data=x, type="line")
#myplot$xAxis(title = list (text = "Time"), type= "datetime")
myplot

And I doing this x labels dissappeared

myplot <- hPlot(COUNT ~ DATEPX, data=x, type="line")
myplot$xAxis(title =list (text = "Month") , type= "datetime")
myplot

Upvotes: 0

Views: 70

Answers (1)

Frash
Frash

Reputation: 728

To get the labels like "Jan/15" you need to pass the dates through strptime to get them into time since epoch and then through strftime to get them formatted accordingly. No need to multiply the dates by 1000 etc. For your case, let me know if the following works out. You can read more about the formatting of dates by doing ?strptime and ?strftime in R REPL.

x$dmy = strftime(strptime(x$YYYYMM, format = "%m/%d/%Y"), format = "%b/%y")
myplot <- hPlot(COUNT ~ dmy, data=x, type="line")
myplot

Not sure how to change the background color in rcharts

Upvotes: 1

Related Questions