timelyportfolio / rcdimple

htmlwidgets for rCharts + dimple
http://www.buildingwidgets.com/blog/2015/3/18/week-11-dimple-as-htmlwidget
MIT License
28 stars 9 forks source link

Use of fixed color for specific values? #14

Open happyshows opened 9 years ago

happyshows commented 9 years ago

Hi,

Is there any way to specify the values? I tried colorAxis, pallette but didn't get it to work.

for example, in groups I have 'A','B','C' 3 unique values I want to assign 3 different color to them. Even in some cases b is not in the dataset. The color scheme will remain consistent.

Could you provide a minimal example? Thanks....

timelyportfolio commented 9 years ago

I was out two weeks ago and missed this. Sorry for the delay. This is ugly, but will work. You can specify a d3 color scale and manually control the range and domain. I'll modify the code from #7.

library(rcdimple)
library(pipeR)

data.frame(
    "year" = rep(2000:2005,each = 12)
    ,"month" = rep(1:12,12)
    ,"value" = runif( 12 * 12, 80, 100 )
) %>>%
    dimple(
        x = c("month","year")
        ,y = "value"
        ,groups = "year"
        ,type = "bar"
    ) %>>%
    xAxis( grouporderRule = "year" ) %>>%
    default_colors(
        htmlwidgets::JS(
            sprintf(
                'd3.scale.category10().range(%s).domain(%s)'
                #,jsonlite::toJSON(blues9, auto_unbox=T)
                ,jsonlite::toJSON(RColorBrewer::brewer.pal(9,"Set1"), auto_unbox=T)
                ,jsonlite::toJSON(2000:2005, auto_unbox=T)
            )
        )
    )