Reputation: 21
I am trying to create a time-series plot using the plotting interface of rCharts to the Highcharts library. I am trying to figure out how I can set the color of an individual point depending on its y-value. I found a way to have different colors for the line and the points, but only as a group, not for the data points individually.
Here's the test code:
library(rCharts)
library(rjson)
TransformDate <- function(x){
as.numeric(as.POSIXct(x, origin="1970-01-01")) * 1000
}
x <- TransformDate(c('2013-01-01 11:05:35', '2013-03-03 04:50:35', '2013-05-05 21:09:37', '2013-07-07 12:49:05'))
y <- c(1,56,123,1000)
w<-TransformDate(c('2013-01-10 11:05:35', '2013-03-13 04:50:35', '2013-05-15 21:09:37', '2013-07-17 12:49:05'))
z<-c(10, 100, 70, 500)
df1 <- data.frame(x = x,y = y)
df2 <- data.frame(x = w, y = z)
combo <- rCharts:::Highcharts$new()
combo$series(list(list(data = rCharts::toJSONArray2(df1, json = F, names = F), name = "Temp1", marker = list(fillColor = c('#999'), lineWidth=6, lineColor=c('#999'))),
list(data = rCharts::toJSONArray2(df2, json = F, names = F), name = "Temp2")))
combo$xAxis(type='datetime')
combo$chart(type = "scatter")
combo$chart(zoomType="x")
combo
I believe that this can be done in Polycharts but the reason why I am using highcharts is that it plots time-series data nicely and it has also cool zoom-in functionality.
Thanks in advance for your help & suggestions. Jan
Upvotes: 2
Views: 1058
Reputation: 10540
Here's one way to control color/size for lines/markers separately:
h <- rCharts:::Highcharts$new()
h$series(list(
list(data = rCharts::toJSONArray2(df1, json = FALSE, names = FALSE),
name = "Big Reds",
color = '#FF0000',
lineWidth = 4,
marker = list(
fillColor = '#FFA500',
radius = 10)
),
list(data = rCharts::toJSONArray2(df2, json = FALSE, names = FALSE),
name = "Small Blues",
color = '#0000FF',
lineWidth = 2,
marker = list(
fillColor = '#ADD8E6',
radius = 6)
)))
h$xAxis(type = 'datetime')
h$chart(type = "scatter")
h$chart(zoomType = "x")
h
Upvotes: 0