Search code examples
rhighchartsr-highchartermultiple-axes

R highcharter - Two barchart in same plot with different X-axis


I am trying to do the following:

I have a two datasets about my company. The first one has, say, the top 20 growing sellers. The second one has the bottom 20 losing sellers. So, it's something like this:

growing_seller <- c("a","b","c","d","e","f","g","h","i","h")
sales_yoy_growing <- c(100000,90000,75000,50000,37500,21000,15000,12000,10000,8000)
top_growing <- data.frame(growing_seller,sales_yoy_growing)


losing_seller <- c("i","j","k","l","m","n","o","p","q","r")
sales_yoy_losing <- c(-90000,-75000,-50000,-37500,-21000,-15000,-12000,-10000,-8000,-5000)
bottom_losing <- data.frame(losing_seller,sales_yoy_losing)

I am trying to plot both charts in the same plot using DIFFERENT categories, corresponding to the sellers' name. So what I have so far is this:

library(highcharter)

growing_seller <- c("a","b","c","d","e","f","g","h","i","h")
sales_yoy_growing <- c(100000,90000,75000,50000,37500,21000,15000,12000,10000,8000)
top_growing <- data.frame(growing_seller,sales_yoy_growing)

losing_seller <- c("i","j","k","l","m","n","o","p","q","r")
sales_yoy_losing <- c(-90000,-75000,-50000,-37500,-21000,-15000,-12000,-10000,-8000,-5000)
bottom_losing <- data.frame(losing_seller,sales_yoy_losing)

highchart() %>%
  hc_add_series(
    data = top_growing$sales_yoy_growing,
    type = "column",
    grouping = FALSE
  ) %>%

  hc_add_series(
    data = bottom_losing$sales_yoy_losing,
    type = "column"
  )

This is what I want to achieve graphically: Chart example
Now,I would like to have a different category array per each independent x-axis: something like the possibility to have "two hc_xAxis" controls, where I could specify per each plotted series its own categories.
My final aim is to, then, have the seller's name as I parse over each of the different columns.

Hope I was clear enough :)
Thanks


Solution

  • Highcharts displays the point's name in the tooltip by default. You just need to point the name value in your data. You can do it this way: top_growing <- data.frame(name = growing_seller, y = sales_yoy_growing)

    This is the whole code:

    library(highcharter)
    
    growing_seller <- c("a","b","c","d","e","f","g","h","i","h")
    sales_yoy_growing <- c(100000,90000,75000,50000,37500,21000,15000,12000,10000,8000)
    top_growing <- data.frame(name = growing_seller, y = sales_yoy_growing)
    
    losing_seller <- c("i","j","k","l","m","n","o","p","q","r")
    sales_yoy_losing <- c(-90000,-75000,-50000,-37500,-21000,-15000,-12000,-10000,-8000,-5000)
    bottom_losing <- data.frame(name = losing_seller, y = sales_yoy_losing)
    
    highchart() %>%
      hc_add_series(
        data = top_growing,
        type = "column",
        grouping = FALSE
      ) %>%
    
      hc_add_series(
        data = bottom_losing,
        type = "column"
      )