Home > OS >  Choropleth map and subregions
Choropleth map and subregions

Time:01-12

I am trying to plot a Choropleth map with subregions with ggplot2. Below you can see my data

 library(ggplot2)
  library(maps)
  
  
  data_arm <- data.frame(subregion = c("Aragatsotn","Ararat","Armavir",
                                    "Gegharkunik","Kotayk","Lori",
                                    "Shirak","Syunik","Tavush",
                                    "Vayots Dzor","Yerevan"),
                               Value = c(0.2560,0.083,0.0120,
                                        0.9560,0.423,0.420,
                                        0.2560,0.043,0.0820,
                                        0.4560,0.019,0.0420
                                         ))

Namely, I want to plot a map for Armenia with all subregions. Unfortunately, these subregions are not available in ggplot2. Below you will see the code for plotting, without subregions.

datar <- data.frame(GEO = c("AR"), 
                      Value = c(0.2560),
                      search_countries = factor(c("Armenia")),
                      countries = c("Armenia")
  )
  
       
  mapa <- map_data('world', region = datar$search_countries)
  
  ggplot()   
    geom_map(data = mapa, map = mapa, aes(x = long, y = lat,map_id = region))  
    geom_map(data = datar, map = mapa, aes(fill = Value, map_id = countries), color ='black', size = .25)  
    coord_map() 

enter image description here

So can anybody help me find a solution for this problem and plot an Armenian map with all subregions?

CodePudding user response:

As in my answer to your last question, I recommend you use rnaturalearth to get the data you need. Here's a full reprex. Note that I had to modify your data frame because you had 11 states but 12 values (therefore the data frame was not valid). I removed the last value and also modified one of the state names to get it to match the official names:

library(ggplot2)
library(rnaturalearth)
library(sf)
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE

data_arm <- data.frame(subregion = c("Aragatsotn","Ararat","Armavir",
                                     "Gegharkunik","Kotayk","Lori",
                                     "Shirak","Syunik","Tavush",
                                     "Vayots Dzor","Erevan"),
                       Value = c(0.2560,0.083,0.0120,
                                 0.9560,0.423,0.420,
                                 0.2560,0.043,0.0820,
                                 0.4560,0.019))

map_data <- ne_states(country = "Armenia", returnclass = "sf")

df <- dplyr::left_join(data_arm, map_data, by = c(subregion = "name"))

ggplot(df)  
  geom_sf(aes(fill = Value, geometry = geometry))

Created on 2023-01-11 with reprex v2.0.2

  • Related