Loop to achieve dynamics plotbands with highcharter

Hello,

I use the Highcharter lib to make my graphs in my R projects.
I recently used the plotbands feature which allows me to have horizontal areas on my graph

hc<-hchart(donnees,"spline",hcaes(x=mois, y=moy, group=origine))%>%
  hc_title(text = titre, align="center")%>%
  hc_tooltip(
    crosshairs = TRUE,
    borderWidth = 5,
    sort = TRUE,
    table = TRUE)%>%
 hc_yAxis(
   title = analyse,
   min = 0,
   plotBands = list(
     list(
       from = 0,
       to = 1000000,
       color = "rgba(0, 200, 0, 0.1)",
       label = list(text="texte 1")
     ),
     list(
       from = 1000000,
       to = 1250000,
       color = "rgba(0, 255, 255, 0.1)",
       label = list(text="texte 2")
     ),
     list(
       from = 1250000,
       to = 1500000,
       color = "rgba(50, 0, 0, 0.1)",
       label = list(text="texte 3")
     ),
     list(
       from = 1500000,
       to = 2000000,
       color = "rgba(250, 255, 0, 0.1)",
       label = list(text="texte 4")
     ),
     list(
       from = 2000000,
       to = 3000000,
       color = "rgba(255, 150, 0, 0.1)",
       label = list(text="texte 5")
     ),
     list(
       from = 3000000,
       to = 5000000,
       color = "rgba(250, 0, 0, 0.1)",
       label = list(text="texte 6")
     )
   ))%>%
  hc_colors(c("#203d7d","#a0a0ed","#203d7e","#FF3333","#4EFF00"))%>%
  hc_exporting(enabled = TRUE,filename = paste0('export - ',titre))%>%
  hc_add_theme(hc_theme_economist())

Now I would like the "plotbands" part to be dynamic both in terms of the number of zones (I can have 2,3,4...), and in the parameterization of these zones.
I have all the necessary data in database, I know how to recover them in a dataframe.

below I have a dataframe with 4 lines to make my 4 zones

  lib point lim_bas lim_haut
1          A         0       0    50000
2          B        -3   51000   100000
3          C       -20  101000   200000
4          D       -50  201000   999999

if plotband takes a list of lists, the simplest way I know looks like this


df1 <- data.frame(txt=letters[1:3],
           from=c(0,1000,1500),
           to = c(900,1500,2000))

library(slider)
library(dplyr)

myfunc <- function(x) {
  x %>% mutate(label=list(text=txt)) %>% as.list
} 
(list1 <- slide(df1,myfunc))

This function looks very interesting!

Could you help me to integrate this function in highcharter, at plotbands level, I don't see how to do it :frowning:

plotBands = list1

at the point where you make plotBands to equal a handcrafted list. use a named object instead.
i.e list1 in my example.
You probably need to do additional work to include colours in your dataframe if you want that feature

I'm going to try this !

Perfect, it's work
thanks a lot !

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.