user22887359
user22887359

Reputation: 3

How can I add lables(values) on R worldmap?

I need my map to look like this: enter image description here

How can I add values on it?

Here is what I have so far:

library(rworldmap)

instorks_trends <- data.frame(
  Country = c("Portugal", "Spain", "Belgium", "Denmark", "Germany", "France", "Netherlands", "Sweden", "Switzerland","Liechtenstein", "Luxembourg","Finland", "Estonia", "Latvia", "Lithuania", "Austria", "Poland", "Russia", "Slovakia", "Slovenia", "Czech Republic", "Ukraine", "Hungary", "Belarus", "Albania", "Bulgaria", "Greece", "Croatia", "Italy", "Macedonia", "Moldova", "Romania", "Serbia", "Montenegro", "Kosovo", "Turkey", "Algeria", "Morocco", "Libya", "Tunisia", "Armenia", "Azerbaijan", "Georgia", "Iran","Iraq", "Israel", "Syria", "Uzbekistan","Tajikistan","Kazakhstan","Kyrgyzstan","Turkmenistan", "South Africa"),
  '2014' = c(11691, 46000, 105, 2, 6152, 2579, 950, 44, 376, 6, 2, 1, 5000, 13900, 19500, 391, 52735, 9000, 1231, 266, 792, 40000, 4950, 22300, 4, 5671, 2357, 1756, 262, 817, 190, 4374, 1248, 1, 40, 8693, NA, 2931, 1400,NA, 644, 278, 104, NA, 70, NA, 4, 1200, 8, 24, 20, 0, NA),
  '2004' = c(7685, 33217, 50, 3, 4482, 941, 523, 29, 198, 0, 0, 0, 4500, 10600, 13000, 395, 52500, 10200, 1331, 240, 814, 30000, 5300, 21362, 3, 4826, 2157, 1700, 70, NA, NA, 5000, NA, NA, NA, 6195, 6601, 1802, 25, 572, 548, NA, NA, NA, NA, NA, NA, 745, NA, NA, NA, NA, 6)
)

world_map <- getMap(resolution = "coarse")

joined_map <- joinCountryData2Map(instorks_trends, joinCode = "NAME", nameJoinColumn = "Country")

mapCountryData(joined_map, nameColumnToPlot = "X2014", catMethod = "quantiles", mapRegion = "Europe",
               missingCountryCol = gray(0.8), colourPalette = "red",
               mapTitle = "", addLegend = FALSE, borderCol = "black") 

Upvotes: 0

Views: 33

Answers (1)

Isaac
Isaac

Reputation: 986

One solution (not the most efficient) for this is to do it with ggplot2. Here is the code, based on the data that you provide:

# Filter for European countries
european_countries <- c("Portugal", "Spain", "Belgium", "Denmark", "Germany", "France", "Netherlands", "Sweden", "Switzerland", "Liechtenstein", "Luxembourg", "Finland", "Estonia", "Latvia", "Lithuania", "Austria", "Poland", "Russia", "Slovakia", "Slovenia", "Czech Republic", "Ukraine", "Hungary", "Belarus", "Albania", "Bulgaria", "Greece", "Croatia", "Italy", "Macedonia", "Moldova", "Romania", "Serbia", "Montenegro", "Kosovo", "Turkey")

mymap <- fortify(joined_map)
mymap <- mymap |> left_join(instorks_trends, by = c("id" = "Country"))

mymap$monarch <- mymap$id %in% instorks_trends$Country
mymap$monarch <- mymap$id %in% european_countries

# This code is to prepare the labels of each country
country_coord <- data.frame(coordinates(joined_map),stringsAsFactors=F)
country_coord <- country_coord[european_countries,]
country_coord <- country_coord |> tibble::rownames_to_column("id") 
country_coord <- country_coord |>   
  left_join(mymap |> select(id, X2014, X2004) |> distinct(id, .keep_all = T),
            by = "id")


ggplot(data = mymap, aes(x = long, y = lat)) + 
  theme_void() +
  geom_map(map = mymap,
           aes(group = group, map_id = id,
               fill = monarch),
           color = "#ffffff") +
  scale_fill_manual(values = c("#666666", "#FF0000")) +
  coord_fixed(xlim = c(-10, 50), ylim = c(35, 75)) +
  theme(legend.position = "none") +
  ggrepel::geom_label_repel(data = country_coord,
            aes(x = country_coord$X1, y = country_coord$X2, 
                label = paste(id, '\n2004:', `X2004`, '\n2014:', `X2014`)), 
            size = 2, color = "black", vjust = 1.)

enter image description here

Upvotes: 0

Related Questions