简体   繁体   中英

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. 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"):

在此处输入图片说明

Any help is appreciated!

What do you think about looping through all points, finding ones that have negative colorValue and then changing their color?

Like this demo: https://jsfiddle.net/BlackLabel/7p0ufcma

Then you could use the 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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