根据hcharter中的给定序列设置热图颜色范围

根据hcharter中的给定序列设置热图颜色范围

本文介绍了根据hcharter中的给定序列设置热图颜色范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我尝试使用highcharter::hcharter()创建一个热图,其中小于-1 应该是一种颜色(粉红色), -1至1 应该是透明的或白色,并且大于+1 应该是另一种颜色(紫色).

Here I am trying to create a heat map using highcharter::hcharter() where less than -1 should be one color (pink), -1 to 1 should be transparent or white color, and greater than +1 should be another color (violet).

目前,我已经使用hchart()编写了代码,并使用color_stops更改了颜色格式.但这在数据居中为0时确实很好用,但是当数据未居中为0时,可以说从 -5到+7.5 ,白色/透明色将变为1,如图所示.以下.

Presently I have written code using hchart() and used color_stops to change the color format. But this works really well when data is centered to 0, but when it isn't centered to 0 let say from -5 to +7.5 white/transparent color would be shifted to 1 as shown in the image below.

a1 <- rnorm(30,0,2)
a2 <- rnorm(30,0,2)
a3 <- rnorm(30,0,2)
a4 <- rnorm(30,0,2)
a <- cbind(a1,a2,a3,a4)
heatmap_matrix <- as.matrix(a)

library(highcharter)
hchart(heatmap_matrix) %>%
  hc_colorAxis(stops = color_stops(n = 3, colors = c("#FF1493", "white", "#800080")))

对于-5 to +7.5
之间的数据范围-5至-1应该显示粉红色渐变颜色
-1至+1应该显示白色
+1到7.5应该显示紫色渐变色

For data range between -5 to +7.5
-5 to -1 should show pink gradient color
-1 to +1 should show white color
+1 to 7.5 should show violet gradient color

推荐答案

hc_colorAxis()中的参数stops需要一个列表,该列表将最小值定义为 0 ,将最大值定义为 1 .您可以在0〜1之间设置一个数字来表示某个值的位置并设置其颜色.

The argument stops in hc_colorAxis() requires a list that defines the minimum value as 0 and the maximum value as 1. You can set a number within 0 ~ 1 to represent the position of a certain value and set its color.

查找零的位置

p <- (0 - min(heatmap_matrix)) / (max(heatmap_matrix) - min(heatmap_matrix))

设置自定义列表

stops <- data.frame(q = c(0, p, 1),
                    c = c("#FF1493", "#FFFFFF", "#800080"),
                    stringsAsFactors = FALSE)
#          q       c
# 1 0.000000 #FF1493
# 2 0.434873 #FFFFFF
# 3 1.000000 #800080

stops <- list_parse2(stops)

控制参数

hchart(heatmap_matrix) %>%
  hc_colorAxis(stops = stops, startOnTick = F, endOnTick = F)

这篇关于根据hcharter中的给定序列设置热图颜色范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 05:24