highcharts R highchart散点图图例颜色未从点颜色正确Map为服装调色板

5jdjgkvh  于 2023-01-13  发布在  Highcharts
关注(0)|答案(1)|浏览(246)

我试图用服装颜色图绘制散点图,所以我根据数据框列的唯一值定义服装颜色。

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))

现在我简单地画出这个图表:

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)

  )

点和图例都适用,但有一个奇怪的行为。如果你检查df数据框,你会发现carb = 4的颜色代码是#F032E6,而图形显示的是不同的东西。查看下面的数据框:

图形如下所示:

如图所示,carb = 4不是#F032E6
hcaes内定义颜色起作用,但是图例没有用相同的调色板更新。

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
                      
                )
  )
lpwwtiir

lpwwtiir1#

您可以在美学中使用colour,而不是像这样使用color

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))

创建于2023年1月11日,使用reprex v2.0.2

相关问题