问题
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).
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")))
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
回答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.
Find where the zero is located
p <- (0 - min(heatmap_matrix)) / (max(heatmap_matrix) - min(heatmap_matrix))
Set a custom list
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)
Control arguments
hchart(heatmap_matrix) %>%
hc_colorAxis(stops = stops, startOnTick = F, endOnTick = F)
来源:https://stackoverflow.com/questions/54143215/setting-heat-map-color-range-as-per-given-sequence-in-hcharter