Search code examples
javascriptrhighchartsr-highcharter

Is there a way to use two series (or some kind of group) in highcharter wordcloud?


I am trying to create a wordcloud with Highcharter R package (based on Highcharts library) to show two categories of sentiments (positive and negative) in just one wordcloud.

The point is that I want to show a legend too. My problem is that when I got to show the legend, then the words are not being aligned. And when I got to show the data in the right way, then I could not show the legend.

The simplest case showing my issue is the one that follows:

library(tidyverse)
library(highcharter)

positive <-
  c(
    "tranquilo",
    "tranquila",
    "nova"  ,
    "burocratico" ,
    "bom"      ,
    "assertivo"   ,
    "rapido"    ,
    "transparente"
  )

negative <-
  c(
    "trabalhoso" ,
    "conduzida" ,
    "passa"  ,
    "congelada" ,
    "pessima"  ,
    "moroso"   ,
    "pouco",
    "opinar"  ,
    "passado"  ,
    "afastado"
  )

df <- list(
  tibble("term" = positive,
         "sentiment" = "positive"),
  tibble("term" = negative,
         "sentiment" = "negative")
) %>% bind_rows()

df %>%
  hchart(
    "wordcloud",
    hcaes(name = "term", group = "sentiment"),
    showInLegend = TRUE,
    colorByPoint = FALSE
  ) %>%
  hc_colors(c("#E0362C", "#189D3E"))

The result is:

If I change "group" parameter by "color" hcaes(name = "term", color = "sentiment"), then what I get is: enter image description here

Thanks in advance. Wlademir.

PS: I think that a solution in JS could also help me.


Solution

  • It is impossible to position words in 2 separate series. You can use one wordcloud series where every point has defined seriesId index. Now, you can use another 2 fake line series (they cannot be the wordcloud type) and you can write a custom legendItemClick event logic on them. Whenever the user clicks on the legend item, the algorithm loops through all words and "hides" the proper ones.

    You can take a look at this example here: https://jsfiddle.net/BlackLabel/7tq01sn5/

    Let me know what do you think and if this solution suits you. When rewriting to R, you can use JS("") function to convert your JavaScript function code to R.