估计阅读时长: 11 分钟

在进行热图的渲染的时候,我们需要首先将需要进行渲染的数据转换为一个0到1之间的灰度值,然后基于所设定的颜色列表,将灰度值映射为颜色列表的索引号,获取某一个灰度对应的颜色,从而完成对热图的渲染过程。在这个过程中,假若我们是针对热图需要获取得到一个连续的颜色列表,则我们还需要使用插值算法针对基础的关键颜色列表进行插值计算,生成调色板。

在R环境中,有一个很好的颜色包:paletteer,在这个包之中,集合了来自于其他的各种包之中的颜色列表信息,可以让用户以一种统一的方式来使用这些颜色列表。paletteer 包提供了对 1759 种配色方案的直接访问权限,这些方案来自 50 个不同的包!安装并加载该包后,使用 paletteer 只需在 ggplot 代码中添加一行额外的代码即可:

# install.packages("paletteer")
library(paletteer);
# install.packages("ggplot2")
library(ggplot2);

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point() +
  scale_color_paletteer_d("nord::aurora");

可以使用的paletteer配色方案的,可以直接在这个网页上预览:https://r-charts.com/color-palettes/ 。同样的,在R#环境中,也直接针对paletteer包中的颜色数据进行了集成,可以直接在默认的R#环境中使用paletteer中的颜色列表,无需使用专门的函数(诸如scale_color_paletteer_d)来进行颜色的引用。下面是一个用于展示在R#环境中调用paletteer的热图例子:

require(ggplot);
require(Matrix);

# load data
data(volcano);

volcano = as.matrix(volcano);
volcano = melt(volcano, varnames = c("X", "Y"), value_name = "Height");

print(volcano, max.print = 13);

let ggplot2 = function(colors) {
  p <- ggplot(volcano, aes(x = "X", y = "Y"),padding = "padding:10% 10% 10% 12%;") +
     geom_tile(aes(fill = "Height")) +
     scale_fill_distiller(palette = colors, direction = 1) +
     theme_light(
        axis.text = element_text(family = "Cambria Math", size = 20),
        axis.title = element_text(family = "Cambria Math", size = 36),
        legend.tick = element_text(family = "Cambria Math", size = 18)
     ) +
     labs(title = "Volcano Height Map");
  p;
}

for(let colorname in [
   # internal built-in color palette
   "jet" "hot" "cool" "grays" "autumn" "spring" "summer" "winter" 
   "FlexImaging" "Typhoon" "Icefire" "Seismic" "Rainbow" 
   "viridis" "viridis:inferno" "viridis:magma" "viridis:plasma" 
   "viridis:cividis" "viridis:mako" "viridis:rocket" "viridis:turbo"
   # paletteer imported
   # colorBlindness
   "colorBlindness::Green2Magenta16Steps" "colorBlindness::Brown2Blue12Steps" 
   "colorBlindness::Brown2Blue10Steps" "colorBlindness::Blue2OrangeRed14Steps" 
   "colorBlindness::Blue2Gray8Steps"
   "colorBlindness::Blue2DarkRed18Steps" "colorBlindness::Blue2Green14Steps" 
   "colorBlindness::paletteMartin" 
   "colorBlindness::ModifiedSpectralScheme11Steps" 
   "colorBlindness::PairedColor12Steps"
   # ggsci
   "ggsci::legacy_tron" "ggsci::default_gsea" "ggsci::uniform_startrek" 
   "ggsci::red_material" 
   "ggsci::pink_material" "ggsci::purple_material"
   # ggthemes
   "ggthemes::Jewel_Bright" "ggthemes::Summer" "ggthemes::Traffic" 
   "ggthemes::Hue_Circle" "ggthemes::Classic_Cyclic" 
   # ggthemes excel
   "ggthemes::excel_Atlas"  "ggthemes::excel_Vapor_Trail" "ggthemes::excel_Celestial" 
   "ggthemes::excel_Depth" 
   "ggthemes::excel_Facet" 
   "ggthemes::excel_Ion" "ggthemes::excel_Ion_Boardroom" "ggthemes::excel_Parallax"
   "ggthemes::excel_Slice" "ggthemes::excel_Savon" "ggthemes::excel_Aspect" 
   "ggthemes::excel_Green_Yellow" 
   "ggthemes::excel_Marquee" "ggthemes::excel_Slipstream"
   ]) {

   png(filename = relative_work(`volcano_${normalizeFileName(colorname,FALSE)}.png`), 
      width = 600, 
      height = 400, 
      bg = "white");

   plot(ggplot2(colorname ));
   dev.off();
}

热图颜色DEMO

颜色板系列 热图demo展示
内部默认调色版

内部默认viridis系列
色盲模式系列
ggsci系列
ggthemes系列
ggthemes Excel系列

原神主题色热图

在之前,为R#环境还编写了一个包,基于针对图片的主题色进行分析,提取出对应的原神角色人物的主题色,在这里也可以直接兼容这个主题色的颜色调色板数据,例如我们在这里可以通过直接修改scale_fill_distiller(palette = colors, direction = 1)的颜色名称为对应的原神角色人物名称即可使用:

require(ggplot);
require(Matrix);
require(scale_colour_genshin);

# load data
data(volcano);

volcano = as.matrix(volcano);
volcano = melt(volcano, varnames = c("X", "Y"), value_name = "Height");

print(volcano, max.print = 13);

let ggplot2 = function(genshin_colors) {
  p <- ggplot(volcano, aes(x = "X", y = "Y"),padding = "padding:10% 10% 10% 12%;") +
     geom_tile(aes(fill = "Height")) +
     scale_fill_distiller(palette = genshin_colors, direction = 1) +
     theme_light(
        axis.text = element_text(family = "Cambria Math", size = 20),
        axis.title = element_text(family = "Cambria Math", size = 36),
        legend.tick = element_text(family = "Cambria Math", size = 18)
     ) +
     labs(title = "Volcano Height Map");
  p;
}

for(let color_name in scale_colour_genshin::keys()) {
   png(filename = relative_work(`volcano_genshin_${color_name}.png`), 
      width = 600, 
      height = 400, 
      bg = "white");

   plot(ggplot2(`genshin:${color_name}`));
   dev.off();
}
原神角色名称 热图效果示例 原神角色名称 热图效果示例 原神角色名称 热图效果示例
香菱 散兵 罗莎莉亚
雷电将军 妮露 莫娜
莱伊拉 甘雨 菲谢尔
北斗 艾尔海森
谢桂纲
Latest posts by 谢桂纲 (see all)

Attachments

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

博客文章
June 2025
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930