简体   繁体   English

当存在负值时,highcharter 热图不遵循 hc_coloraxis

[英]highcharter heatmap not following hc_coloraxis when there's negative values


Hi. 你好。
I am trying to set colors from -1 to 1 as designed in the stops argument of hc_colorAxis. 我试图按照 hc_colorAxis 的停止参数中的设计将颜色设置为从 -1 到 1。 This works fine if there are no negative values in the correlation matrix. 如果相关矩阵中没有负值,这可以正常工作。 However, when there are negative values it resets to these default colors of blue, white, and red. 但是,当存在负值时,它会重置为蓝色、白色和红色这些默认颜色。

Here is the code with data:这是带有数据的代码:

 library(BatchGetSymbols) library(tidyverse) library(highcharter) t1 = BatchGetSymbols(c("MSFT", "SPY", "SH"))[[2]] t2 = t1[,c("ticker", "ref.date", "ret.adjusted.prices")] t3 = na.omit(pivot_wider(t2, names_from = ticker, values_from = "ret.adjusted.prices")) t4 = cor(t3[,2:4]) hchart(type = "heatmap", t4) %>% hc_legend(layout = "vertical", verticalAlign = "middle", align = "right") %>% hc_colorAxis(min = -1, max = 1, stops = color_stops(3, c("#d51f3a", "#FFFFFF", "#5daa45")))

Here is what I am getting:这是我得到的: 在此处输入图片说明


This is what I am trying to get (replace "SH" in t1 for "AAPL"): 这就是我想要得到的(将 t1 中的“SH”替换为“AAPL”):

在此处输入图片说明

Any help is appreciated!任何帮助表示赞赏!

What do you think about looping through all points, finding ones that have negative colorValue and then changing their color?您如何看待遍历所有点,找到具有负 colorValue 的点然后更改它们的颜色?

Like this demo: https://jsfiddle.net/BlackLabel/7p0ufcma喜欢这个演示: https : //jsfiddle.net/BlackLabel/7p0ufcma

Then you could use the Highcharts JS API.然后你可以使用 Highcharts JS API。 You can see a handy article on how to do it here: https://www.highcharts.com/blog/tutorials/working-with-highcharts-javascript-syntax-in-r/?fbclid=IwAR3UzztbrY4rdCbOw1W7DmSq4mWJswJZxIw-xOqGbjIRExj-gsuVEO2zM00您可以在此处查看有关如何执行此操作的便捷文章: https : //www.highcharts.com/blog/tutorials/working-with-highcharts-javascript-syntax-in-r/?fbclid=IwAR3UzztbrY4rdCbOw1W7DmSq4mWJswJZxIw-xOqGbsu0G02M2M

  chart.series[0].data.forEach(point => {
    if (point.colorValue < 0) {
      point.update({
        color: 'red'
      });
    }
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM