Search code examples
rhighchartsscatter-plot

Highlighting the cluster of scatterplot using highcharter in R


I'm using highcharter pkg in R for drawing a scatter plot.

library(highcharter) 
# Load data
data("mtcars")
df <- mtcars
# Convert cyl as a grouping variable
df$cyl <- as.factor(df$cyl)
# Inspect the data
head(df[, c("wt", "mpg", "cyl", "qsec")], 4)
df %>% 
  hchart(
    'scatter', hcaes(x = wt, y = mpg, size = qsec, group = cyl),
    maxSize = "10%"
  )

There is a cool feature in the highcharter pkg that when you hover over a cluster in the legend, the points for that specific cluster stand out from the rest of the clusters. enter image description here I don't know how I can control this feature programmatically. I want to have this view not by hovering over to the cluster but by selecting cluster from the code (maybe by defining the cluster group) and it can give me highlighted view like the picture above.

I checked highcharts documentations but I couldn't get any clue.

appreciate it


Solution

  • As your other answerer implied, it may be best to simply alter the opacity by series (each color is a different series here).

    To understand the meaning of c(.25, 1, .25) for opacity in the code below, consider what is controlling the colors.

    unique(mtcars$cyl)
    # [1] 6 4 8   # assume that HC sorted these
    

    So the first opacity value is assigned to points where cyl == 4.

    df %>% 
      hchart(opacity = c(.25, 1, .25), 
        'scatter', hcaes(x = wt, y = mpg, size = qsec, group = cyl),
        maxSize = "10%"
      )
    

    enter image description here

    You can assign the state. Manually changing the state doesn't set the styles associated with the state, though.

    In this code, I've set those points where cyl == 6 to the state of hover. However, I didn't touch the other traces. Note that this requires the assignment of an id in the hchart call.

    df %>% 
      hchart('scatter', hcaes(x = wt, y = mpg, size = qsec, group = cyl,
                              id = cyl), maxSize = "10%") %>% 
      htmlwidgets::onRender(
        "function(el) {
           var chart = $('#' + el.id).highcharts();
           var haloMe = chart.get('6');
           haloMe.setState('hover');
         }")
    

    If you look closely at the image, the first point in 6 has that halo effect. Other than that, you can't see any difference in the points. (You may notice this is a bit glitchy, as well.)

    enter image description here

    Changing all series' states doesn't help, either. In this version, I've set one series to 'hover' and the other 2 to 'inactive'. There's no difference. When rendered in your browser, you can check the state and see that they are set as shown in this code.

    df %>% 
      hchart('scatter', hcaes(x = wt, y = mpg, size = qsec, group = cyl,
                              id = cyl), maxSize = "10%") %>% 
      htmlwidgets::onRender(
        "function(el) {
           var chart = $('#' + el.id).highcharts();
           var haloMe = chart.get('6');
           haloMe.setState('hover');
           var inact4 = chart.get('4');
           var inact8 = chart.get('8');
           inact4.setState('inactive');
           inact8.setState('inactive');
         }")