gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:46:25 24 4
gpt4 key购买 nike

在这里,我尝试使用 highcharter::hcharter() 创建热图哪里小于 -1 应该是一种颜色(粉红色), -1 到 1 应该是透明或白色,和大于 +1 应该是另一种颜色(紫色)。

目前我已经使用 hchart() 编写了代码并使用 color_stops更改颜色格式。但是当数据以 0 为中心时这非常有效,但是当它不以 0 为中心时,比如 -5 到 +7.5 如下图所示,白色/透明颜色将变为 1。

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 应该显示紫色渐变色

Heat Map sample plot using hcharter

最佳答案

参数 stopshc_colorAxis()需要一个将最小值定义为 的列表0 最大值为 1 .可以设置一个0~1之间的数字来表示某个值的位置并设置它的颜色。

找出零的位置

foo+bar

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)

enter image description here

关于r - 根据hcharter中的给定序列设置热图颜色范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54143215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com