'Map Sales Data as Heatmap on Europe Map Using R

I am using EU Superstore Data which consists of product details, their sales, profits and discounts on that particular product. I have calculated a margin field ( profit / sales) and grouped it by the countries in Europe.

Now I want to map this margin on the map of Europe using R as a heatmap. What I mean is that on the map, it should show the margin in percentage. Also, the states on map should be colored according to their number. The higher margin should be lighter in color and the lower margin should be darker in color.

How can I achieve that?

TIA



Solution 1:[1]

The main thing is to find EU data online :). Please note, margin is an arbitrary number in my example.

library(GADMTools)
library(tidyverse)

allCountries <- read.csv("https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv")
eu <- allCountries |>
  filter(region == "Europe") |>
  select(alpha.3) |>
  as.list()

a <- gadm_sf_loadCountries(eu$alpha.3, level = 0, basefile="/home/sapi/projekty/test/", simplify=NULL)
a$sf$margin <- 1:length(unique(a$sf$ISO))

a <- gadm_crop(a, -33, 25, 42, 85)
gadm_plot(a)

b <- st_as_sf(a$sf)
plot(b["margin"])

Created on 2022-03-26 by the reprex package (v2.0.1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grzegorz Sapijaszko