Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to colour each row so the softest and strongest colour are the minimum and the maximum of each row?

I made the following graph:

enter image description here

Here’s the code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    heatmap_branquesCAT <- ggplot(varIPI_branques_CAT_long, aes(x = time, y = branques, fill=values)) +
  geom_tile(aes(width=31)) +
  geom_text(aes(label = round(values, 1)), family="serif", color="black", size=4) +
  scale_x_date(expand=c(0,0), breaks = "1 months", labels=date_format("%b %y")
               # limits = c(as.Date("2023/01/01"), NA)) +
              ) +
  scale_y_discrete(expand=c(0,0), labels=c("Subministrament d'aigua", "Indústries d'alimentació i begudes", "Altres indústries manufactureres", 
                                           "Materials i equips elèctrics, electrònics i òptics", "Energia elèctrica", 
                                           "Fabricació de productes farmacèutics", "Índex general (IPI)", "Indústries extractives",
                                           "Indústries manufactureres", "Fabricació de maquinària i equips mecànics", "Metal·lúrgia i fabricació de productes metàl·lics",
                                           "Fabricació d'altres productes minerals no metàl·lics", "Indústries del paper", "Fabricació productes cautxú i matèries plàstiques",
                                           "Indústries químiques", "Indústries tèxtils",
                                           "Fabricació de material de transport")) +
  labs(title = "Índex de Producció Industrial",
       subtitle = "% de variació interanual",
       x = "", 
       y = " ",
       caption = "Font: Idescat.") +
    scale_fill_gradient2(
      low = "#CC3300",
      mid = "white",
      high = "#66CC00",
   #   midpoint = 0, 
      limits = range(varIPI_branques_CAT_long$values)) +
  guides(color=guide_legend(title="")) +
  theme_minimal() +
  tema +
  theme(axis.text = element_text(size=12, colour = "black"), 
  legend.position="none") 

heatmap_branquesCAT

And a sample of my data:

structure(list(time = structure(c(19083, 19113, 19144, 19174, 
19205, 19236, 19266, 19297, 19327, 19358, 19389, 19417, 19448, 
18993, 19024, 19052, 19083, 19113, 19144, 19174, 19205, 19236, 
19266, 19297, 19327, 19358, 19389, 19417, 19448, 18993, 19024
), class = "Date"), branques = c("Indústries extractives", "Indústries extractives", 
"Indústries extractives", "Indústries extractives", "Indústries extractives", 
"Indústries extractives", "Indústries extractives", "Indústries extractives", 
"Indústries extractives", "Indústries extractives", "Indústries extractives", 
"Indústries extractives", "Indústries extractives", "Indústries manufactureres", 
"Indústries manufactureres", "Indústries manufactureres", "Indústries manufactureres", 
"Indústries manufactureres", "Indústries manufactureres", "Indústries manufactureres", 
"Indústries manufactureres", "Indústries manufactureres", "Indústries manufactureres", 
"Indústries manufactureres", "Indústries manufactureres", "Indústries manufactureres", 
"Indústries manufactureres", "Indústries manufactureres", "Indústries manufactureres", 
"alimentació", "alimentació"), values = c(-17.2, -12, 0.6, 
-6, 8.6, 7.5, 10, 37.1, 26.1, 28.3, 14, 16.8, -9.4, 0.8, 3.1, 
2.5, -2.7, 6.4, 1.9, -2.9, 1.5, 2.5, 0.1, -1.9, -0.1, 5.3, -1.7, 
1.8, -6.8, 4.6, 4.6), scaled_value = structure(c(-0.486164571662395, 
-0.269301398928164, 0.256174750389396, -0.0190746611578974, 0.589810400749752, 
0.543935498825203, 0.648196639562814, 1.77838740515852, 1.31963838591303, 
1.41138818976213, 0.815014464742992, 0.931786942369116, -0.160869812561049, 
0.0939546003127367, 0.338515461609155, 0.274716976053568, -0.278203232094857, 
0.689407132164886, 0.21091849049798, -0.299469393946719, 0.168386166794255, 
0.274716976053568, 0.019523033831218, -0.193138584687407, -0.00174312802064446, 
0.572443241979642, -0.171872422835544, 0.200285409572049, -0.714159550058037, 
0.59745871515621, 0.59745871515621), dim = c(31L, 1L))), row.names = 20:50, class = "data.frame")

So the problem is that now the greens are coloured according to the overall maximum (=50,2). And this gradient should be coloured according to the maximum and minimum of each row.

>Solution :

You need to create a new column where all values are scaled, and base your fill values on that column. (say between 0 and 1, -1 to 1)

For 0-1, something like this:

varIPI_branques_CAT_long <- varIPI_branques_CAT_long |>
  mutate(
    scaled_value = (value - min(value)) / (max(value) - min(value),
    .by = branques
  )

Then replace fill = value with fill = scaled_value in the ggplot code

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading