Modifications in Heatmap in R

Hello,

I am plotting a heat map using the code below. I need your assistance for the following

  1. I am unable to adjust the size of color scale equal to the size of side of square of map
  2. I want to add padding to the inner border of map

Thanks in advance

code -

# Summarize the average spillover by 'From' and 'To' industries
average_spillovers <- spillovers %>%
  group_by(From, To) %>%
  summarize(Average_Spillover = mean(Spillover, na.rm = TRUE))

# Convert data to wide format for heatmap
spillover_matrix <- dcast(average_spillovers, From ~ To, value.var = "Average_Spillover")

# Set diagonal values to NA to display as white
for(i in 1:nrow(spillover_matrix)) {
  spillover_matrix[i, i+1] <- NA  # Setting diagonal values to NA
}

# Convert to long format for ggplot
spillover_long <- melt(spillover_matrix, id.vars = "From", variable.name = "To", value.name = "Average_Spillover")

# Plot the heatmap with custom color scheme, square tiles, and scaled legend height

ggplot(spillover_long, aes(x = To, y = From, fill = Average_Spillover)) +
  geom_tile() +  # No individual borders for tiles
  # Outer border around heatmap
  geom_rect(
    xmin = 0.5, xmax = num_industries + 0.5,
    ymin = 0.5, ymax = num_industries + 0.5,
    color = "black", fill = NA, size = 0.5
  ) +
  scale_fill_gradient2(
    low = "green", mid = "yellow", high = "red", 
    midpoint = median(spillover_long$Average_Spillover, na.rm = TRUE), 
    name = "Avg. Spillover",
    na.value = "white",  # Set NA values to white
    guide = guide_colorbar(barheight = unit(num_industries, "lines"))  # Guide height to match heatmap
  ) +
  labs(title = "",x = "", y = "") +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 8, face = "bold"),  # Smaller, bold text for x-axis
    axis.text.y = element_text(size = 8, face = "bold"),  # Smaller, bold text for y-axis
    plot.title = element_text(size = 10, face = "bold"),  # Bold title with smaller size
    legend.title = element_text(size = 8, face = "bold"),  # Smaller, bold legend title
    legend.text = element_text(size = 7) # Smaller legend text
  ) +
  coord_fixed(ratio = 1.1)

sample data -

> spillover_long
               From              To Average_Spillover
1              AUTO            AUTO                NA
2            BANKEX            AUTO         0.8939730
3   BASIC.MATERIALS            AUTO         0.7676022
4     CAPITAL.GOODS            AUTO         0.8426603
5         CONS.DISC            AUTO         0.4656582
6     CONS.DURABLES            AUTO         0.9398147
7            ENERGY            AUTO         0.9351838
8           FINANCE            AUTO         0.7971373
9              FMCG            AUTO         0.9218218
10       HEALTHCARE            AUTO         0.8625690
11      INDUSTRIALS            AUTO         0.6449208
12               IT            AUTO         1.1005276
13            METAL            AUTO         1.0613251
14           OILGAS            AUTO         0.9164248
15            POWER            AUTO         0.8469532
16           REALTY            AUTO         1.2975563
17             TECK            AUTO         0.9375377
18          TELECOM            AUTO         1.1276769
19        UTILITIES            AUTO         0.8770461
20             AUTO          BANKEX         0.8939730
21           BANKEX          BANKEX                NA
22  BASIC.MATERIALS          BANKEX         0.8654754
23    CAPITAL.GOODS          BANKEX         0.8555339
24        CONS.DISC          BANKEX         0.7916917
25    CONS.DURABLES          BANKEX         1.0712346
26           ENERGY          BANKEX         0.9885849
27          FINANCE          BANKEX         0.2725158
28             FMCG          BANKEX         1.0662980
29       HEALTHCARE          BANKEX         1.0379703
30      INDUSTRIALS          BANKEX         0.7772649
31               IT          BANKEX         1.2142764
32            METAL          BANKEX         1.1076683
33           OILGAS          BANKEX         0.9807627
34            POWER          BANKEX         0.9036205
35           REALTY          BANKEX         1.2286574
36             TECK          BANKEX         1.0469633
37          TELECOM          BANKEX         1.1894539
38        UTILITIES          BANKEX         0.9444720
39             AUTO BASIC.MATERIALS         0.7676022
40           BANKEX BASIC.MATERIALS         0.8654754
41  BASIC.MATERIALS BASIC.MATERIALS                NA
42    CAPITAL.GOODS BASIC.MATERIALS         0.7864130
43        CONS.DISC BASIC.MATERIALS         0.6044191
44    CONS.DURABLES BASIC.MATERIALS         0.9314916
45           ENERGY BASIC.MATERIALS         0.8596667
46          FINANCE BASIC.MATERIALS         0.7577220
47             FMCG BASIC.MATERIALS         0.9722847
48       HEALTHCARE BASIC.MATERIALS         0.8804546
49      INDUSTRIALS BASIC.MATERIALS         0.6093074
50               IT BASIC.MATERIALS         1.1294239
51            METAL BASIC.MATERIALS         0.5571058
52           OILGAS BASIC.MATERIALS         0.8353142
53            POWER BASIC.MATERIALS         0.7422130
54           REALTY BASIC.MATERIALS         1.1503154
55             TECK BASIC.MATERIALS         0.9491528
56          TELECOM BASIC.MATERIALS         1.0796162
57        UTILITIES BASIC.MATERIALS         0.7503987
58             AUTO   CAPITAL.GOODS         0.8426603
59           BANKEX   CAPITAL.GOODS         0.8555339
60  BASIC.MATERIALS   CAPITAL.GOODS         0.7864130
61    CAPITAL.GOODS   CAPITAL.GOODS                NA
62        CONS.DISC   CAPITAL.GOODS         0.7084959
63    CONS.DURABLES   CAPITAL.GOODS         0.9990116
64           ENERGY   CAPITAL.GOODS         0.9465740
65          FINANCE   CAPITAL.GOODS         0.7698761
66             FMCG   CAPITAL.GOODS         1.0346950
67       HEALTHCARE   CAPITAL.GOODS         0.9724429
68      INDUSTRIALS   CAPITAL.GOODS         0.3661428
69               IT   CAPITAL.GOODS         1.1785113
70            METAL   CAPITAL.GOODS         1.0587433
71           OILGAS   CAPITAL.GOODS         0.9207920
72            POWER   CAPITAL.GOODS         0.6928997
73           REALTY   CAPITAL.GOODS         1.2105786
74             TECK   CAPITAL.GOODS         1.0097173
75          TELECOM   CAPITAL.GOODS         1.1370189
76        UTILITIES   CAPITAL.GOODS         0.8341286
77             AUTO       CONS.DISC         0.4656582
78           BANKEX       CONS.DISC         0.7916917
79  BASIC.MATERIALS       CONS.DISC         0.6044191
80    CAPITAL.GOODS       CONS.DISC         0.7084959
81        CONS.DISC       CONS.DISC                NA
82    CONS.DURABLES       CONS.DISC         0.7247108
83           ENERGY       CONS.DISC         0.8098095
84          FINANCE       CONS.DISC         0.6608462
85             FMCG       CONS.DISC         0.7886775
86       HEALTHCARE       CONS.DISC         0.7188452
87      INDUSTRIALS       CONS.DISC         0.5255715
88               IT       CONS.DISC         1.0010885
89            METAL       CONS.DISC         0.9691155
90           OILGAS       CONS.DISC         0.7979615
91            POWER       CONS.DISC         0.6927083
92           REALTY       CONS.DISC         1.0922045
93             TECK       CONS.DISC         0.8163195
94          TELECOM       CONS.DISC         1.0182224
95        UTILITIES       CONS.DISC         0.6984005
96             AUTO   CONS.DURABLES         0.9398147
97           BANKEX   CONS.DURABLES         1.0712346
98  BASIC.MATERIALS   CONS.DURABLES         0.9314916
99    CAPITAL.GOODS   CONS.DURABLES         0.9990116
100       CONS.DISC   CONS.DURABLES         0.7247108
101   CONS.DURABLES   CONS.DURABLES                NA
102          ENERGY   CONS.DURABLES         1.0789766
103         FINANCE   CONS.DURABLES         0.9707163
104            FMCG   CONS.DURABLES         1.0278807
105      HEALTHCARE   CONS.DURABLES         0.9752749
106     INDUSTRIALS   CONS.DURABLES         0.8805600
107              IT   CONS.DURABLES         1.2115469
108           METAL   CONS.DURABLES         1.2211471
109          OILGAS   CONS.DURABLES         1.0632133
110           POWER   CONS.DURABLES         0.9957923
111          REALTY   CONS.DURABLES         1.3833788
112            TECK   CONS.DURABLES         1.0622873
113         TELECOM   CONS.DURABLES         1.2480027
114       UTILITIES   CONS.DURABLES         0.9994569
115            AUTO          ENERGY         0.9351838
116          BANKEX          ENERGY         0.9885849
117 BASIC.MATERIALS          ENERGY         0.8596667
118   CAPITAL.GOODS          ENERGY         0.9465740
119       CONS.DISC          ENERGY         0.8098095
120   CONS.DURABLES          ENERGY         1.0789766
121          ENERGY          ENERGY                NA
122         FINANCE          ENERGY         0.8960167
123            FMCG          ENERGY         1.0409895
124      HEALTHCARE          ENERGY         0.9909686
125     INDUSTRIALS          ENERGY         0.8420165
126              IT          ENERGY         1.1795488
127           METAL          ENERGY         1.0741123
128          OILGAS          ENERGY         0.2897014
129           POWER          ENERGY         0.8673203
130          REALTY          ENERGY         1.3525543
131            TECK          ENERGY         1.0078539
132         TELECOM          ENERGY         1.1602702
133       UTILITIES          ENERGY         0.8534986
134            AUTO         FINANCE         0.7971373
135          BANKEX         FINANCE         0.2725158
136 BASIC.MATERIALS         FINANCE         0.7577220
137   CAPITAL.GOODS         FINANCE         0.7698761
138       CONS.DISC         FINANCE         0.6608462
139   CONS.DURABLES         FINANCE         0.9707163
140          ENERGY         FINANCE         0.8960167
141         FINANCE         FINANCE                NA
142            FMCG         FINANCE         0.9693460
143      HEALTHCARE         FINANCE         0.9359640
144     INDUSTRIALS         FINANCE         0.6664908
145              IT         FINANCE         1.1286612
146           METAL         FINANCE         1.0354972
147          OILGAS         FINANCE         0.8875587
148           POWER         FINANCE         0.8065500
149          REALTY         FINANCE         1.1645455
150            TECK         FINANCE         0.9530077
151         TELECOM         FINANCE         1.1048329
152       UTILITIES         FINANCE         0.8383527
153            AUTO            FMCG         0.9218218
154          BANKEX            FMCG         1.0662980
155 BASIC.MATERIALS            FMCG         0.9722847
156   CAPITAL.GOODS            FMCG         1.0346950
157       CONS.DISC            FMCG         0.7886775
158   CONS.DURABLES            FMCG         1.0278807
159          ENERGY            FMCG         1.0409895
160         FINANCE            FMCG         0.9693460
161            FMCG            FMCG                NA
162      HEALTHCARE            FMCG         0.8123051
163     INDUSTRIALS            FMCG         0.9445729
164              IT            FMCG         1.0309866
165           METAL            FMCG         1.2781250
166          OILGAS            FMCG         1.0259029
167           POWER            FMCG         1.0016190
168          REALTY            FMCG         1.5080230
169            TECK            FMCG         0.9561200
170         TELECOM            FMCG         1.2120375
171       UTILITIES            FMCG         0.9956687
172            AUTO      HEALTHCARE         0.8625690
173          BANKEX      HEALTHCARE         1.0379703
174 BASIC.MATERIALS      HEALTHCARE         0.8804546
175   CAPITAL.GOODS      HEALTHCARE         0.9724429
176       CONS.DISC      HEALTHCARE         0.7188452
177   CONS.DURABLES      HEALTHCARE         0.9752749
178          ENERGY      HEALTHCARE         0.9909686
179         FINANCE      HEALTHCARE         0.9359640
180            FMCG      HEALTHCARE         0.8123051
181      HEALTHCARE      HEALTHCARE                NA
182     INDUSTRIALS      HEALTHCARE         0.8489028
183              IT      HEALTHCARE         1.0142382
184           METAL      HEALTHCARE         1.1998289
185          OILGAS      HEALTHCARE         0.9708176
186           POWER      HEALTHCARE         0.9184712
187          REALTY      HEALTHCARE         1.4193566
188            TECK      HEALTHCARE         0.8897740
189         TELECOM      HEALTHCARE         1.1663958
190       UTILITIES      HEALTHCARE         0.9124265
191            AUTO     INDUSTRIALS         0.6449208
192          BANKEX     INDUSTRIALS         0.7772649
193 BASIC.MATERIALS     INDUSTRIALS         0.6093074
194   CAPITAL.GOODS     INDUSTRIALS         0.3661428
195       CONS.DISC     INDUSTRIALS         0.5255715
196   CONS.DURABLES     INDUSTRIALS         0.8805600
197          ENERGY     INDUSTRIALS         0.8420165
198         FINANCE     INDUSTRIALS         0.6664908
199            FMCG     INDUSTRIALS         0.9445729
200      HEALTHCARE     INDUSTRIALS         0.8489028
201     INDUSTRIALS     INDUSTRIALS                NA
202              IT     INDUSTRIALS         1.0990072
203           METAL     INDUSTRIALS         0.9220720
204          OILGAS     INDUSTRIALS         0.8183563
205           POWER     INDUSTRIALS         0.6105430
206          REALTY     INDUSTRIALS         1.1033522
207            TECK     INDUSTRIALS         0.9215626
208         TELECOM     INDUSTRIALS         1.0470365
209       UTILITIES     INDUSTRIALS         0.7039563
210            AUTO              IT         1.1005276
211          BANKEX              IT         1.2142764
212 BASIC.MATERIALS              IT         1.1294239
213   CAPITAL.GOODS              IT         1.1785113
214       CONS.DISC              IT         1.0010885
215   CONS.DURABLES              IT         1.2115469
216          ENERGY              IT         1.1795488
217         FINANCE              IT         1.1286612
218            FMCG              IT         1.0309866
219      HEALTHCARE              IT         1.0142382
220     INDUSTRIALS              IT         1.0990072
221              IT              IT                NA
222           METAL              IT         1.3893706
223          OILGAS              IT         1.1785336
224           POWER              IT         1.1694342
225          REALTY              IT         1.6264505
226            TECK              IT         0.3402036
227         TELECOM              IT         1.3495068
228       UTILITIES              IT         1.1786338
229            AUTO           METAL         1.0613251
230          BANKEX           METAL         1.1076683
231 BASIC.MATERIALS           METAL         0.5571058
232   CAPITAL.GOODS           METAL         1.0587433
233       CONS.DISC           METAL         0.9691155
234   CONS.DURABLES           METAL         1.2211471
235          ENERGY           METAL         1.0741123
236         FINANCE           METAL         1.0354972
237            FMCG           METAL         1.2781250
238      HEALTHCARE           METAL         1.1998289
239     INDUSTRIALS           METAL         0.9220720
240              IT           METAL         1.3893706
241           METAL           METAL                NA
242          OILGAS           METAL         1.0587783
243           POWER           METAL         0.9978069
244          REALTY           METAL         1.3213667
245            TECK           METAL         1.2286351
246         TELECOM           METAL         1.3132923
247       UTILITIES           METAL         1.0127012
248            AUTO          OILGAS         0.9164248
249          BANKEX          OILGAS         0.9807627
250 BASIC.MATERIALS          OILGAS         0.8353142
251   CAPITAL.GOODS          OILGAS         0.9207920
252       CONS.DISC          OILGAS         0.7979615
253   CONS.DURABLES          OILGAS         1.0632133
254          ENERGY          OILGAS         0.2897014
255         FINANCE          OILGAS         0.8875587
256            FMCG          OILGAS         1.0259029
257      HEALTHCARE          OILGAS         0.9708176
258     INDUSTRIALS          OILGAS         0.8183563
259              IT          OILGAS         1.1785336
260           METAL          OILGAS         1.0587783
261          OILGAS          OILGAS                NA
262           POWER          OILGAS         0.8172480
263          REALTY          OILGAS         1.3311126
264            TECK          OILGAS         1.0038720
265         TELECOM          OILGAS         1.1498673
266       UTILITIES          OILGAS         0.7786222
267            AUTO           POWER         0.8469532
268          BANKEX           POWER         0.9036205
269 BASIC.MATERIALS           POWER         0.7422130
270   CAPITAL.GOODS           POWER         0.6928997
271       CONS.DISC           POWER         0.6927083
272   CONS.DURABLES           POWER         0.9957923
273          ENERGY           POWER         0.8673203
274         FINANCE           POWER         0.8065500
275            FMCG           POWER         1.0016190
276      HEALTHCARE           POWER         0.9184712
277     INDUSTRIALS           POWER         0.6105430
278              IT           POWER         1.1694342
279           METAL           POWER         0.9978069
280          OILGAS           POWER         0.8172480
281           POWER           POWER                NA
282          REALTY           POWER         1.1946476
283            TECK           POWER         0.9835221
284         TELECOM           POWER         1.1000263
285       UTILITIES           POWER         0.3442390
286            AUTO          REALTY         1.2975563
287          BANKEX          REALTY         1.2286574
288 BASIC.MATERIALS          REALTY         1.1503154
289   CAPITAL.GOODS          REALTY         1.2105786
290       CONS.DISC          REALTY         1.0922045
291   CONS.DURABLES          REALTY         1.3833788
292          ENERGY          REALTY         1.3525543
293         FINANCE          REALTY         1.1645455
294            FMCG          REALTY         1.5080230
295      HEALTHCARE          REALTY         1.4193566
296     INDUSTRIALS          REALTY         1.1033522
297              IT          REALTY         1.6264505
298           METAL          REALTY         1.3213667
299          OILGAS          REALTY         1.3311126
300           POWER          REALTY         1.1946476
301          REALTY          REALTY                NA
302            TECK          REALTY         1.4671653
303         TELECOM          REALTY         1.4974035
304       UTILITIES          REALTY         1.2296185
305            AUTO            TECK         0.9375377
306          BANKEX            TECK         1.0469633
307 BASIC.MATERIALS            TECK         0.9491528
308   CAPITAL.GOODS            TECK         1.0097173
309       CONS.DISC            TECK         0.8163195
310   CONS.DURABLES            TECK         1.0622873
311          ENERGY            TECK         1.0078539
312         FINANCE            TECK         0.9530077
313            FMCG            TECK         0.9561200
314      HEALTHCARE            TECK         0.8897740
315     INDUSTRIALS            TECK         0.9215626
316              IT            TECK         0.3402036
317           METAL            TECK         1.2286351
318          OILGAS            TECK         1.0038720
319           POWER            TECK         0.9835221
320          REALTY            TECK         1.4671653
321            TECK            TECK                NA
322         TELECOM            TECK         1.0347775
323       UTILITIES            TECK         0.9955422
324            AUTO         TELECOM         1.1276769
325          BANKEX         TELECOM         1.1894539
326 BASIC.MATERIALS         TELECOM         1.0796162
327   CAPITAL.GOODS         TELECOM         1.1370189
328       CONS.DISC         TELECOM         1.0182224
329   CONS.DURABLES         TELECOM         1.2480027
330          ENERGY         TELECOM         1.1602702
331         FINANCE         TELECOM         1.1048329
332            FMCG         TELECOM         1.2120375
333      HEALTHCARE         TELECOM         1.1663958

Here is the plot I get with the data you posted and your code, except that I substituted 19 for the three instances of num_industries. That variable was not defined, so I guessed it should be the number of unique values in the From column of the data. Does this achieve your goal in item 1?

library(tidyverse)
spillover_long <- read.csv("~/R/Play/Dummy.csv")


ggplot(spillover_long, aes(x = To, y = From, fill = Average_Spillover)) +
  geom_tile() +  # No individual borders for tiles
  # Outer border around heatmap
  geom_rect(
    xmin = 0.5, xmax = 19 + 0.5,
    ymin = 0.5, ymax = 19 + 0.5,
    color = "black", fill = NA, size = 0.5
  ) +
  scale_fill_gradient2(
    low = "green", mid = "yellow", high = "red", 
    midpoint = median(spillover_long$Average_Spillover, na.rm = TRUE), 
    name = "Avg. Spillover",
    na.value = "white",  # Set NA values to white
    guide = guide_colorbar(barheight = unit(19, "lines"))  # Guide height to match heatmap
  ) +
  labs(title = "",x = "", y = "") +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 8, face = "bold"),  # Smaller, bold text for x-axis
    axis.text.y = element_text(size = 8, face = "bold"),  # Smaller, bold text for y-axis
    plot.title = element_text(size = 10, face = "bold"),  # Bold title with smaller size
    legend.title = element_text(size = 8, face = "bold"),  # Smaller, bold legend title
    legend.text = element_text(size = 7) # Smaller legend text
  ) +
  coord_fixed(ratio = 1.1)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

Created on 2024-10-26 with reprex v2.1.1

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.