#install.packages(c("cowplot", "googleway","ggplot2", "ggrepel", "ggspatial", "libwgeom", "sf", "rnaturalearth","rnaturalearthdata"))

1 パッケージの読み込み

library("ggplot2")
theme_set(theme_bw())
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")

2 世界地図の表示(rnaturalearth)

world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
## [1] "sf"         "data.frame"
world<-world[,c(18,45)]

ggplot(data = world) +
    geom_sf()

3 ユニクロの国別工場数のデータの読み込み

library(readxl)
FR <- read_excel("FR.xlsx")
head(FR)
## # A tibble: 6 × 5
##   country    factory countryjp       ison countrycode
##   <chr>        <dbl> <chr>          <dbl> <chr>      
## 1 Bangladesh      20 バングラデシュ    50 BGD        
## 2 Bulgaria         2 ブルガリア       100 BGR        
## 3 Cambodia        10 カンボジア       116 KHM        
## 4 China          144 中国             156 CHN        
## 5 India            9 インド           356 IND        
## 6 Indonesia       14 インドネシア     360 IDN

4 工場数のデータとrnaturalearthのデータの接合(left_join)

library(dplyr)
FR$iso_a3 <- FR$countrycode
world_merged <- left_join(world, FR, by = "iso_a3")
head(world_merged)
## Simple feature collection with 6 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -70.06611 ymin: -18.01973 xmax: 74.89131 ymax: 60.40581
## Geodetic CRS:  +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
##          name iso_a3 country factory countryjp ison countrycode
## 1       Aruba    ABW    <NA>      NA      <NA>   NA        <NA>
## 2 Afghanistan    AFG    <NA>      NA      <NA>   NA        <NA>
## 3      Angola    AGO    <NA>      NA      <NA>   NA        <NA>
## 4    Anguilla    AIA    <NA>      NA      <NA>   NA        <NA>
## 5     Albania    ALB    <NA>      NA      <NA>   NA        <NA>
## 6       Aland    ALA    <NA>      NA      <NA>   NA        <NA>
##                         geometry
## 1 MULTIPOLYGON (((-69.89912 1...
## 2 MULTIPOLYGON (((74.89131 37...
## 3 MULTIPOLYGON (((14.19082 -5...
## 4 MULTIPOLYGON (((-63.00122 1...
## 5 MULTIPOLYGON (((20.06396 42...
## 6 MULTIPOLYGON (((20.61133 60...

5 各国の重心の緯度経度の取得(rworldmap)

library(rworldmap)
library(rworldxtra)
#方法1
# get world map
wmap <- getMap(resolution="high")
# wmapをデータに変換すると重心の緯度経度ある
wmap_data <- as.data.frame(wmap)
wmap_data<-wmap_data[,c(19,32,33,34)]
head(wmap_data)
##          NAME ISO3       LON       LAT
## 1       Aruba  ABW -69.97345  12.51678
## 2 Afghanistan  AFG  66.00845  33.83627
## 3      Angola  AGO  17.56405 -12.32934
## 4    Anguilla  AIA -63.05667  18.22432
## 5     Albania  ALB  20.05399  41.14258
## 6       Aland  ALA  19.94429  60.22851

6 各国の重心の緯度経度を接合(left_join)

library(dplyr)
world_merged$ISO3<-world_merged$iso_a3
world_merged <- left_join(world_merged,wmap_data,by="ISO3")
head(world_merged)
## Simple feature collection with 6 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -70.06611 ymin: -18.01973 xmax: 74.89131 ymax: 60.40581
## Geodetic CRS:  +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
##          name iso_a3 country factory countryjp ison countrycode ISO3
## 1       Aruba    ABW    <NA>      NA      <NA>   NA        <NA>  ABW
## 2 Afghanistan    AFG    <NA>      NA      <NA>   NA        <NA>  AFG
## 3      Angola    AGO    <NA>      NA      <NA>   NA        <NA>  AGO
## 4    Anguilla    AIA    <NA>      NA      <NA>   NA        <NA>  AIA
## 5     Albania    ALB    <NA>      NA      <NA>   NA        <NA>  ALB
## 6       Aland    ALA    <NA>      NA      <NA>   NA        <NA>  ALA
##          NAME       LON       LAT                       geometry
## 1       Aruba -69.97345  12.51678 MULTIPOLYGON (((-69.89912 1...
## 2 Afghanistan  66.00845  33.83627 MULTIPOLYGON (((74.89131 37...
## 3      Angola  17.56405 -12.32934 MULTIPOLYGON (((14.19082 -5...
## 4    Anguilla -63.05667  18.22432 MULTIPOLYGON (((-63.00122 1...
## 5     Albania  20.05399  41.14258 MULTIPOLYGON (((20.06396 42...
## 6       Aland  19.94429  60.22851 MULTIPOLYGON (((20.61133 60...
#工場がある国に絞る
library(tidyr)
world_merged_label<-drop_na(world_merged,factory)

#工場がない国は工場の数をゼロにする
world_merged$factory[is.na(world_merged$factory)] <- 0

7 地図描画 国名ラベルあり

ggp1 <-ggplot(data = world_merged_label) +
    geom_sf(aes(fill = factory)) +
    scale_fill_distiller(name="縫製工場数", palette = "Reds", direction=1)+
   geom_sf(data = world_merged, fill = NA)+
  labs(title="",caption="") +
  geom_text(data = world_merged_label, label=paste0(world_merged_label$countryjp, " ", as.character(format(world_merged_label$factory, big.mark=","))), aes(x=LON, y=LAT), hjust=0, vjust=0, fontface='bold', size=2, check_overlap = TRUE) +
  coord_sf(xlim = c(-90, 150), ylim = c(60, -30), expand = TRUE)+
   theme(axis.text = element_blank(), axis.title = element_blank())

ggp1

ggsave("fig3_5_map_uniqlo.jpg", width = 8, height = 4, dpi = "print")

8 地図描画 国名ラベルなし

ggp1 <-ggplot(data = world_merged_label) +
    geom_sf(aes(fill = factory)) +
    scale_fill_distiller(name="縫製工場数", palette = "Reds", direction=1)+
   geom_sf(data = world_merged, fill = NA)+
  labs(title="",caption="") +
  coord_sf(xlim = c(-90, 150), ylim = c(60, -30), expand = TRUE)+
   theme(axis.text = element_blank(), axis.title = element_blank())


ggp1

ggsave("fig3_5_map_uniqlo2.jpg", width = 8, height = 4, dpi = "print")

9 地図描画 国名ラベルあり

ggp1 <-ggplot(data = world_merged_label) +
    geom_sf(aes(fill = factory)) +
    scale_fill_distiller(name="縫製工場数", palette = "Reds", direction=1)+
   geom_sf(data = world_merged, fill = NA)+
  labs(title="",caption="") +
  coord_sf(xlim = c(-90, 150), ylim = c(60, -30), expand = TRUE)+
   theme(axis.text = element_blank(), axis.title = element_blank())+
geom_sf_label(aes(label =paste0(world_merged_label$countryjp, " ", as.character(format(world_merged_label$factory, big.mark=",")))),size=2)


ggp1

ggsave("fig3_5_map_uniqlo3.jpg", width = 8, height = 4, dpi = "print")

10 地図描画 国名ラベルあり

参考

ggp1 <-ggplot(data = world_merged_label) +
    geom_sf(aes(fill = factory)) +
    scale_fill_distiller(name="縫製工場数", palette = "Reds", direction=1)+
   geom_sf(data = world_merged, fill = NA)+
  coord_sf(xlim = c(-90, 150), ylim = c(60, -30), expand = TRUE)+
   theme(axis.text = element_blank(), axis.title = element_blank())+
  ggrepel::geom_label_repel(
    aes(label = paste0(world_merged_label$countryjp, " ", as.character(format(world_merged_label$factory))), geometry = geometry),
    stat = "sf_coordinates",
    segment.size  = 0.2,
    #min.segment.length = 5,
    #label.size = 1, # with this we 
    color = "black"
    ) 


ggp1

ggsave("fig3_5_map_uniqlo4.jpg", width = 16, height = 10, dpi = "print")