javascriptrhighchartsrcharts

RCharts - Choosing color by category column chart


I have a RCharts question (more generally an highchart demand). I am trying to build a column chart with 2 categories and 2 bars for each category, it's quite simple.

But I can't display colors I want. By default, each bar has the same color is each category, but I would like that each bar in one category have the same color. I can't do it.

My R code below :

col_chart <- Highcharts$new()
col_chart$xAxis(categories=list('A', 'B') ) 
col_chart$series(list(list(type="column",
                             name="1",data=c(6,7))
                        ,list(type="column",
                              name="2",data=c(1,3))
                         ))

As everyone likes there, this is a very minimal reproductible example. There, each modality "1" is blue and each modality "2" is dark. I would like to make a difference between A and B, not between 1 and 2.

I thank you in advance for your help.


Solution

  • First solution.

    It is possibile to specify the color of each bar using the colors and colorByPoint options:

    library(rCharts)
    col_chart <- Highcharts$new()
    col_chart$xAxis(categories=list('A', 'B') ) 
    col_chart$series(list(
        list(type="column", name="1",data=c(6,7), 
             colors=list('#7cb5ec', '#FF0000'), colorByPoint=TRUE),
        list(type="column", name="2",data=c(1,3), 
             colors=list('#7cb5ec', '#FF0000'), colorByPoint=TRUE)
    ))
    col_chart
    

    enter image description here

    Second solution.

    col_chart <- Highcharts$new()
    col_chart$xAxis(categories=list('A', 'B') ) 
    col_chart$series(list(
        list(type="column", name="1", 
             data=list(list(color='#7cb5ec',y=6),list(color='#FF0000',y=7))
        ),
        list(type="column", name="2", 
             data=list(list(color='#7cb5ec',y=1),list(color='#FF0000',y=3))
        )
    ))
    col_chart
    

    enter image description here