Search code examples
rhighchartsscatter-plotr-highcharter

R highcharter scatter plot legend color is not mapped correctly from the point color as costume palette


I'm trying to draw a scatter plot with a costume color map. So I define my costume color based on the unique values of a data frame column.

library('dplyr')
library('leaflet')
library('highcharter')

n <- length(unique(mtcars$carb))

col_vector <- c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4',
                '#46f0f0', '#f032e6', '#bcf60c', '#008080', '#e6beff',
                '#9a6324', '#800000', '#aaffc3', '#808000', '#000075', '#808080')
set.seed(2)

if(n > length(col_vector)){
  
  col=sample(col_vector, n,replace = TRUE)
  
}else {
  
  col=sample(col_vector, n,replace = FALSE)
}

pal <- leaflet::colorFactor(pal = col, domain = unique(mtcars$carb))

Now simply I wanna draw this graph:

df <- dplyr::mutate(mtcars,
                colorCode = pal(mtcars[['carb']])
                )



highchart() %>%
  hc_add_series(df, type = "scatter",
                hcaes(x = mpg, 
                      y = disp, 
                      group = carb
                ),
                color = unique(df$colorCode)

  )

It works with both points and legend but there is a strange behavior. If you check the df dataframe, you'll see that the color code for carb = 4 is #F032E6 while the graph shows something different.Look at data frame below: enter image description here

While the graph looks like this: enter image description here

As you can see in the plot carb = 4 is not #F032E6

defining the color inside hcaes works but the legend is not updated with same color palette.

n <- length(unique(mtcars$carb))

col_vector <- c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4',
                '#46f0f0', '#f032e6', '#bcf60c', '#008080', '#e6beff',
                '#9a6324', '#800000', '#aaffc3', '#808000', '#000075', '#808080')
set.seed(2)

if(n > length(col_vector)){
  
  col=sample(col_vector, n,replace = TRUE)
  
}else {
  
  col=sample(col_vector, n,replace = FALSE)
}

pal <- leaflet::colorFactor(pal = col, domain = unique(mtcars$carb))

df <- dplyr::mutate(mtcars,
                    colorCode = pal(mtcars[['carb']])
                    )



highchart() %>%
  hc_add_series(df, type = "scatter",
                hcaes(x = mpg, 
                      y = disp, 
                      group = carb,
                      color = colorCode
                      
                )
  )

Solution

  • You could use colour in your aesthetics instead of color like this

    library(dplyr)
    library(leaflet)
    library(highcharter)
    
    n <- length(unique(mtcars$cyl))
    
    col_vector <- c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4',
                             '#46f0f0', '#f032e6', '#bcf60c', '#008080', '#e6beff',
                             '#9a6324', '#800000', '#aaffc3', '#808000', '#000075', '#808080')
                             set.seed(2)
                             
                             if(n > length(col_vector)){
                               
                               col=sample(col_vector, n,replace = TRUE)
                               
                             }else {
                               
                               col=sample(col_vector, n,replace = FALSE)
                             }
                             
                             pal <- leaflet::colorFactor(pal = col, domain = unique(mtcars$cyl))
                             
                             df <- dplyr::mutate(mtcars,
                                                 colorCode = pal(mtcars[['carb']])
                             )
                             
                             highchart() %>%
                               hc_add_series(df, type = "scatter",
                                             hcaes(x = mpg, 
                                                   y = disp, 
                                                   group = cyl,
                                                   colour = colorCode))
    

    enter image description here

    Created on 2023-01-11 with reprex v2.0.2