Highcharts 在R中使用hctreemap2创建具有不同颜色的树映射

Highcharts 在R中使用hctreemap2创建具有不同颜色的树映射,highcharts,treemap,r-highcharter,Highcharts,Treemap,R Highcharter,我正在尝试使用highcharter包在R中生成一个交互式树形图(顺便说一句,我喜欢这个包)。 它应该是这样的(我甚至不需要不同的级别) 示例代码: df <- data.frame(name = c("john", "jane", "herbert", "peter"), bananas = c(10, 14, 6, 3)) hctreemap2(df, group_vars = "name", s

我正在尝试使用highcharter包在R中生成一个交互式树形图(顺便说一句,我喜欢这个包)。 它应该是这样的(我甚至不需要不同的级别)

示例代码:

df <- data.frame(name = c("john", "jane", "herbert", "peter"),
                    bananas = c(10, 14, 6, 3))

hctreemap2(df, 
           group_vars = "name",
           size_var = "bananas")

df在花了整整一个上午(我的工作需要这个)没有找到解决方案后,终于在这里发布了这个问题,问了10分钟后,我自己找到了解决方案,尝试了一些突然出现在我脑海中的东西^^

下面是:

hctreemap2(df, 
           group_vars = "name",
           size_var = "bananas") %>%
    hc_plotOptions(treemap = list(colorByPoint = TRUE)) %>%        #allows points in the same serie to have different colors
    hc_colors(c("#FFFF00", "#FF0000", "#0000FF", "#00AA00")) %>%   #with this we can set the colors, note: 1st color is given to first row in the data frame (not necessarily the biggest box)
    hc_colorAxis(dataClasses = color_classes(df$name)) %>%         #defines acc. to which variable, a box gets a distinct color
    hc_legend(enabled = FALSE)                                     #suppresses legend
它给出了: 我们需要highcharter软件包的三个附加功能,但它工作得相当好

hctreemap2(df, 
           group_vars = "name",
           size_var = "bananas") %>%
    hc_plotOptions(treemap = list(colorByPoint = TRUE)) %>%        #allows points in the same serie to have different colors
    hc_colors(c("#FFFF00", "#FF0000", "#0000FF", "#00AA00")) %>%   #with this we can set the colors, note: 1st color is given to first row in the data frame (not necessarily the biggest box)
    hc_colorAxis(dataClasses = color_classes(df$name)) %>%         #defines acc. to which variable, a box gets a distinct color
    hc_legend(enabled = FALSE)                                     #suppresses legend