将一个单元划分为一个单独的热图/具有多个颜色代码 [英] Divide one cell into one separate heatmap/with multiple color code

查看:33
本文介绍了将一个单元划分为一个单独的热图/具有多个颜色代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

样本数据-

 Country segments variable  value
      US    A   Kerosene    16.09
      US    B   Kerosene    2.81
      US    C   Kerosene    -7.96
      US    D   Kerosene    25.76
      US    E   Kerosene    75.47
      US    A   Petrol  26.82
      US    B   Petrol  8.72
      US    C   Petrol  2.07
      US    D   Petrol  16
      US    E   Petrol  -11
      US    A   Edible Oil  4.02
      US    B   Edible Oil  10.6
      US    C   Edible Oil  49.05
      US    D   Edible Oil  16.07
      US    E   Edible Oil  51.87
      US    A   LPG -1.07
      US    B   LPG 49.19
      US    C   LPG 37.9
      US    D   LPG 33.05
      US    E   LPG 102

这是美国的数据集,就像我在文件中拥有其他14个国家的数据集一样.现在,我用分层方法制作了15个单独的热图.x轴是变量,y轴是细分市场和热图,每个国家/地区都有关注值.

This is the data set for US, like that I have dataset of 14 other countries in the file. Now a days I make 15 separate heatmap in hierarchical approach. The x-axis is the variable and y-axis the segments and the heatmap with concern values based on each country.

我当时的想法是与所有国家一起制作一张幻灯片.因此,最终结果是-y轴与段相同,x轴与国家/地区相同,每个像元是一个热图,在单个像元中具有四个不同的变量和相应的值.

What I was thinking is to make a single slide with all the countries as well. So the final result with be - y-axis same with the segments, x-axis with countries and each cell is a heat map with four different variable and corresponding value in single cell.

任何帮助都将是非常有用的-任何有关如何编码和制作具有四个不同值和颜色代码的堆图的单个单元格的想法.如下所示-这将是一个包含8个国家和4个细分市场以及4个细分市场的示例变量.

Any help will be of great- Any idea of how I can code and make a single cell to a heapmap with four different values and color code.Something like below - That will be an example of 8 countries and 4 segments with 4 variables.

推荐答案

由于您没有提供完整的可复制示例(或您已经尝试过的任何代码),因此我制作了一些虚拟数据来说明您可能会怎么做它.

Since you didn't provide a full reproducible example (or any code you've already tried), I made up some dummy data to show how you might do it.

library(dplyr)
library(ggplot2)

fake.df <- 
expand.grid(Country = c("US", "Canada", "Mexico"), 
            segments = LETTERS[1:5], 
            variable = c("Kerosene", "Petrol",
                         "Edible Oil", "LPG")) %>% 
  mutate(value = runif(length(Country), -20, 50))

fake.df.adj <- 
fake.df %>% 
  mutate(xadj = ifelse(variable %in% c("Kerosene", "Edible Oil"), -0.33/2,  0.33/2),
         yadj = ifelse(variable %in% c("Kerosene", "Petrol"),      0.33/2, -0.33/2),
         xpos = as.numeric(factor(Country))  + xadj,
         ypos = as.numeric(factor(segments)) + yadj)

mutant 代码在要绘制的每个中心点周围创建一个调整框.对于煤油"和食用油",这些图块向左移动每个 Country 之间的空间的1/3(因此,每个图块的中心为-0.33/2);其他的则向右移动.与y调整相同,但上下左右而不是左右.

The mutate code creates a box of adjustments around each center point you want to plot. For "Kerosene" and "Edible Oil", those tiles are shifted to the left by 1/3 of the space between each Country (so the center of each tile is -0.33/2); the others are shifted right. Same with the y adjustment, but up and down instead of left and right.

然后将因子( Country segments )转换为数字位置,并将它们加在一起.

Then you convert your factors (Country and segments) to numeric positions and add them together.

fake.df.adj

   Country segments   variable       value   xadj   yadj  xpos  ypos
1       US        A   Kerosene -11.8318607 -0.165  0.165 0.835 1.165
2   Canada        A   Kerosene  39.1181835 -0.165  0.165 1.835 1.165
3   Mexico        A   Kerosene  25.9354644 -0.165  0.165 2.835 1.165
4       US        B   Kerosene -19.3503525 -0.165  0.165 0.835 2.165
5   Canada        B   Kerosene -12.4459506 -0.165  0.165 1.835 2.165
6   Mexico        B   Kerosene   9.9017016 -0.165  0.165 2.835 2.165
7       US        C   Kerosene  37.9767562 -0.165  0.165 0.835 3.165
8   Canada        C   Kerosene   3.4687111 -0.165  0.165 1.835 3.165
9   Mexico        C   Kerosene   0.6280239 -0.165  0.165 2.835 3.165
10      US        D   Kerosene  43.4739464 -0.165  0.165 0.835 4.165
11  Canada        D   Kerosene   8.4671510 -0.165  0.165 1.835 4.165
12  Mexico        D   Kerosene  19.7357609 -0.165  0.165 2.835 4.165
13      US        E   Kerosene  -2.6120700 -0.165  0.165 0.835 5.165
14  Canada        E   Kerosene -10.6013792 -0.165  0.165 1.835 5.165
15  Mexico        E   Kerosene  -6.2059698 -0.165  0.165 2.835 5.165
16      US        A     Petrol  17.1079974  0.165  0.165 1.165 1.165
17  Canada        A     Petrol  46.8944785  0.165  0.165 2.165 1.165
18  Mexico        A     Petrol   9.3977815  0.165  0.165 3.165 1.165
19      US        B     Petrol  38.7547489  0.165  0.165 1.165 2.165
20  Canada        B     Petrol -14.4210703  0.165  0.165 2.165 2.165
21  Mexico        B     Petrol  32.5160861  0.165  0.165 3.165 2.165
22      US        C     Petrol  -1.3750645  0.165  0.165 1.165 3.165
23  Canada        C     Petrol  17.0540527  0.165  0.165 2.165 3.165
24  Mexico        C     Petrol -16.8449931  0.165  0.165 3.165 3.165
25      US        D     Petrol  33.8465349  0.165  0.165 1.165 4.165
26  Canada        D     Petrol  43.7369153  0.165  0.165 2.165 4.165
27  Mexico        D     Petrol  32.2145640  0.165  0.165 3.165 4.165
28      US        E     Petrol -13.1811532  0.165  0.165 1.165 5.165
29  Canada        E     Petrol  46.1913082  0.165  0.165 2.165 5.165
30  Mexico        E     Petrol  -7.5030316  0.165  0.165 3.165 5.165
31      US        A Edible Oil  36.1643957 -0.165 -0.165 0.835 0.835
32  Canada        A Edible Oil  26.9887728 -0.165 -0.165 1.835 0.835
33  Mexico        A Edible Oil   9.2825143 -0.165 -0.165 2.835 0.835
34      US        B Edible Oil  32.7455557 -0.165 -0.165 0.835 1.835
35  Canada        B Edible Oil   9.0447777 -0.165 -0.165 1.835 1.835
36  Mexico        B Edible Oil  21.5822081 -0.165 -0.165 2.835 1.835
37      US        C Edible Oil   2.3906442 -0.165 -0.165 0.835 2.835
38  Canada        C Edible Oil  25.2536855 -0.165 -0.165 1.835 2.835
39  Mexico        C Edible Oil   5.0618632 -0.165 -0.165 2.835 2.835
40      US        D Edible Oil  41.0292115 -0.165 -0.165 0.835 3.835
41  Canada        D Edible Oil   8.4515698 -0.165 -0.165 1.835 3.835
42  Mexico        D Edible Oil  11.9080938 -0.165 -0.165 2.835 3.835
43      US        E Edible Oil  42.2518838 -0.165 -0.165 0.835 4.835
44  Canada        E Edible Oil  25.6458033 -0.165 -0.165 1.835 4.835
45  Mexico        E Edible Oil -19.3037443 -0.165 -0.165 2.835 4.835
46      US        A        LPG  25.8891215  0.165 -0.165 1.165 0.835
47  Canada        A        LPG  -3.1028641  0.165 -0.165 2.165 0.835
48  Mexico        A        LPG  47.5884154  0.165 -0.165 3.165 0.835
49      US        B        LPG  11.3000701  0.165 -0.165 1.165 1.835
50  Canada        B        LPG  26.8041755  0.165 -0.165 2.165 1.835
51  Mexico        B        LPG -13.0209453  0.165 -0.165 3.165 1.835
52      US        C        LPG  -5.5292231  0.165 -0.165 1.165 2.835
53  Canada        C        LPG  46.3101034  0.165 -0.165 2.165 2.835
54  Mexico        C        LPG  19.7622448  0.165 -0.165 3.165 2.835
55      US        D        LPG   8.5258791  0.165 -0.165 1.165 3.835
56  Canada        D        LPG  20.5856857  0.165 -0.165 2.165 3.835
57  Mexico        D        LPG -10.7589733  0.165 -0.165 3.165 3.835
58      US        E        LPG  14.8577291  0.165 -0.165 1.165 4.835
59  Canada        E        LPG  -6.9590007  0.165 -0.165 2.165 4.835
60  Mexico        E        LPG  -3.1651872  0.165 -0.165 3.165 4.835

fake.df.adj %>% 
  ggplot(aes(xpos, ypos, fill = value)) + 
  geom_tile(height = 0.33, width = 0.33) +
  geom_text(aes(label = paste0(round(value, 1), "%"),
                color = value < 30), show.legend = F) +
  geom_text(data = fake.df.adj %>% filter(Country == "Mexico", segments == "E"),
            aes(label = variable, 
                y = ypos + 1.75*yadj), 
            fontface = "bold") +
  scale_fill_gradient2(labels = function(breaks) paste0(breaks, "%")) +
  scale_color_manual(values = c("FALSE" = "white", "TRUE" = "black")) +
  scale_x_continuous(breaks = 1:3, name = NULL,
                     labels = levels(fake.df$Country)) +
  scale_y_continuous(breaks = 1:5, name = "Segments",
                     labels = levels(fake.df$segments)) +
  theme_classic()

现在,您可以使用 geom_tile 进行绘制,将图块的高度和宽度设置为0.33.

Now you can plot using geom_tile, setting the height and width of the tiles to be 0.33.

我在每个图块的顶部添加了标签,四舍五入到一个小数点后一位,并带有一个%符号.为了使它们在高值的黑暗背景填充下能很好地显示,我将标签的颜色美感作为逻辑表达式,然后进一步将这些值映射为黑色和白色.

I added the labels on top of each tile, rounded to one decimal place, with a % sign. In order to make them show up nicely against the dark background fill for high values, I made the color aesthetic for the labels a logical expression, then further down mapped those values to black and white.

然后无法分辨出 variable 的四种类型中的哪种类型,因此我通过将数据仅过滤到墨西哥和E并进行移位来标记右上角的那些类型.使用与以前相同的调整值,将y位置比图块的中心稍远一点.

Then there's no way to tell which of the four types of variable are in which tile, so I labeled the ones in the upper right corner by filtering the data to only Mexico and E, and shifting the y position a little bit further than the center of the tile using the same adjustment value as before.

然后我添加了一个分散的填充调色板,并在色条上标记了%符号.

Then I added a diverging fill palette and labeled the colourbar with % signs.

然后,您必须为x和y标度指定标签,因为它们现在只是数字位置.

Then you have to specify the labels for the x and y scales, since they're just numeric positions right now.

这篇关于将一个单元划分为一个单独的热图/具有多个颜色代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆