JoséCarlosMartínez
Posts: 25
Joined: Sun Mar 28, 2021 8:12 pm

Same color element of the legend and the graph bubble

(Apologies for my English)

I use this R code to build a highcharts chart that I include in a report created with rmardown from RStudio.

Code: Select all

dfaus <- dfaus[order(dfaus$Municipio),]
hc <- dfaus %>%
 hchart(
  type = "scatter",
  hcaes(
   x = dfaus$`Esfuerzo Social por Habitante`,
   y = dfaus$`Umbral Pobreza`,
   size = dfaus$`Relevancia Esfuerzo Social` * 100,
   group = dfaus$`Municipio`,
   color = ifelse(dfaus$Municipio == "Murcia", "#C31A00", "#8673A1")
   ),
  color = ifelse(dfaus$Municipio == "Murcia", "#C31A00", "#8673A1"),
  tooltip = list(
   valueDecimals = 2,
   valueSuffix = " %"
  )
 )

But the color of the element of the legend does not correspond to the bubble of the chart.

I have detected that the order of the elements of the legend is affected by the stressed words, for example, "Málaga" is before "Murcia", however, it appears after. The same happens with "Córdoba" and "Coruña (A)". I think that HighCharts when sorting the legend elements does not take into account the latin characters but their unicode values.

Thanks for your help.
Greetings.
Attachments
gráfico_highcharts.png
gráfico_highcharts.png (141.3 KiB) Viewed 153 times
jedrzej.r
Posts: 725
Joined: Tue Jan 24, 2023 11:21 am

Re: Same color element of the legend and the graph bubble

Hi!

Thanks for reaching out to us with your question!

It seems that this problem could be related to R wrapper, as i assumed you are using Highcharter. Instead of changing colors in config, you could utilize native Highcharts load event and update the colors of bubble and legend item after the initial load:

Code: Select all

hc_chart(events = list(load = JS("function () {
   this.series.forEach(function (series) {
    series.update({
      color: series.data[0].color
    }, false);
   });
   this.redraw();
  }"))) %>%
Under this URL you can find a full example of adding such function as a string to the chart config: https://stackoverflow.com/questions/622 ... 1#62262591

Let me know if this what you were looking for.
Best regards!
Jędrzej Ruta
Highcharts Developer
JoséCarlosMartínez
Posts: 25
Joined: Sun Mar 28, 2021 8:12 pm

Re: Same color element of the legend and the graph bubble

Checked and works perfectly.

Thanks for your help.
Greetings.

Return to “Highcharts Usage”