【问题标题】:How to change colors of a highcharter matrix/heatmap?如何更改 highcharter 矩阵/热图的颜色?
【发布时间】:2019-10-10 21:27:21
【问题描述】:

我想更改 highcharter 热图的颜色,但似乎找不到正确的方法。在以下示例中,-1 为红色,1 为蓝色。我想改变颜色。例如。 -1 为蓝色,1 为红色。

mtcars2 <- mtcars[1:20, ]
x <- cor(mtcars2)
hchart(round(x,2),"heatmap")

当变量少于 12 个时,我可以使用RColorBrewer 来更改颜色。例如:

hc_colorAxis(minColor = brewer.pal(12, "Set3")[1],
           maxColor = brewer.pal(12, "Set3")[12])

我也尝试过colorRampPalette() 启用更多颜色,但没有成功。

【问题讨论】:

    标签: r highcharts r-highcharter


    【解决方案1】:

    我以highcharter showcase中的疫苗影响图为例

    在此示例中,热图不是从矩阵调用,而是从具有 3 列的 data.frame 调用,因此我将相关矩阵转换为这种格式

    library(tidyverse) # get the latest version for pivot_longer function   
    mtcars3 <- cor(mtcars2) %>% 
                  as.data.frame() %>% # convert to dataframe
                  rownames_to_column(var="col1") %>% # add rownames as a new column
                  pivot_longer(cols=names(mtcars),
                               names_to="col2",
                               values_to='cor') # convert to a 3 column dataframe
    

    然后您可以使用您提供的代码应用颜色,并添加 dataLabels 参数以显示值

    hchart(mtcars3,"heatmap",hcaes(x=col1,y=col2,value=round(cor,2)),
                   dataLabels = list(enabled = TRUE)) %>%
       hc_colorAxis(minColor = brewer.pal(12, "Set3")[1],
               maxColor = brewer.pal(12, "Set3")[12])
    

    【讨论】:

    • 谢谢,@fmarm!看起来它与数据结构有关。我想知道为什么当变量较少时它适用于矩阵。是否可以从图表中删除或隐藏 col1 和 col2?此外,hchart(round(x,2),"heatmap") 将在图表上显示相关数字。你能把它们也显示在你的图表上吗?
    • 我已经更新了我的答案,你需要在hchart调用中添加一个dataLabels参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多