【R】コーヒー生産量

コーヒーは、ちょっとリラックスしたいときの最高の飲み物だと思っています。子供たちに、世界的にどこで生産されているのか。と質問を受けましたが、十分に答えられなかったので、調べてみることにしました。

世界のコーヒー生産量をWikipediaの”List of countries by coffee production”で調べました。せっかくなので、Rでスクレイピングして、地図に表示してみます。

まず、rvestでhtmlからtableを取得します。それをデータフレームにします。

library(tidyverse)
library(rvest)

coffee_html <- read_html("https://en.wikipedia.org/wiki/List_of_countries_by_coffee_production")
coffee_html

results <- coffee_html %>% 
  html_table(fill=TRUE)
coffee_df <- results[1] %>% 
  as.data.frame() 
> coffee_html
{html_document}
<html class="client-nojs" lang="en" dir="ltr">
[1] <head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n<meta ...
[2] <body class="mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject mw-editab ...
>

扱いやすいように、データフレームをちょっと操作します。

coffee_dat <- coffee_df %>% 
  rename(sovereignt=Country) %>% 
  mutate(Metric.Tons = str_remove_all(Metric.Tons, ",")) %>% 
  select(Rank, sovereignt, Metric.Tons)
> head(coffee_dat)
  Rank sovereignt Metric.Tons
1    1     Brazil     2652000
2    2    Vietnam     1650000
3    3   Colombia      810000
4    4  Indonesia      660000
5    5   Ethiopia      384000
6    6   Honduras      348000

rnaturalearthで世界地図を取得して、コーヒーのデータとマージします。

world <- ne_countries(scale = "medium", returnclass = "sf")
world_joined <- left_join(world, coffee_dat, key = sovereignt)
> head(world_joined)
Simple feature collection with 6 features and 65 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -73.41544 ymin: -55.25 xmax: 75.15803 ymax: 42.68825
CRS:            +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
  scalerank      featurecla labelrank           sovereignt sov_a3 adm0_dif level
1         1 Admin-0 country         3          Afghanistan    AFG        0     2
2         1 Admin-0 country         3               Angola    AGO        0     2
3         1 Admin-0 country         6              Albania    ALB        0     2
4         1 Admin-0 country         4 United Arab Emirates    ARE        0     2
5         1 Admin-0 country         2            Argentina    ARG        0     2
6         1 Admin-0 country         6              Armenia    ARM        0     2
               type                admin adm0_a3 geou_dif              geounit gu_a3
1 Sovereign country          Afghanistan     AFG        0          Afghanistan   AFG
2 Sovereign country               Angola     AGO        0               Angola   AGO
3 Sovereign country              Albania     ALB        0              Albania   ALB
4 Sovereign country United Arab Emirates     ARE        0 United Arab Emirates   ARE
5 Sovereign country            Argentina     ARG        0            Argentina   ARG
6 Sovereign country              Armenia     ARM        0              Armenia   ARM
  su_dif              subunit su_a3 brk_diff                 name            name_long
1      0          Afghanistan   AFG        0          Afghanistan          Afghanistan
2      0               Angola   AGO        0               Angola               Angola
3      0              Albania   ALB        0              Albania              Albania
4      0 United Arab Emirates   ARE        0 United Arab Emirates United Arab Emirates
5      0            Argentina   ARG        0            Argentina            Argentina
6      0              Armenia   ARM        0              Armenia              Armenia
  brk_a3             brk_name brk_group abbrev postal                    formal_en
1    AFG          Afghanistan      <NA>   Afg.     AF Islamic State of Afghanistan
2    AGO               Angola      <NA>   Ang.     AO  People's Republic of Angola
3    ALB              Albania      <NA>   Alb.     AL          Republic of Albania
4    ARE United Arab Emirates      <NA> U.A.E.     AE         United Arab Emirates
5    ARG            Argentina      <NA>   Arg.     AR           Argentine Republic
6    ARM              Armenia      <NA>   Arm.    ARM          Republic of Armenia
  formal_fr note_adm0 note_brk            name_sort name_alt mapcolor7 mapcolor8
1      <NA>      <NA>     <NA>          Afghanistan     <NA>         5         6
2      <NA>      <NA>     <NA>               Angola     <NA>         3         2
3      <NA>      <NA>     <NA>              Albania     <NA>         1         4
4      <NA>      <NA>     <NA> United Arab Emirates     <NA>         2         1
5      <NA>      <NA>     <NA>            Argentina     <NA>         3         1
6      <NA>      <NA>     <NA>              Armenia     <NA>         3         1
  mapcolor9 mapcolor13  pop_est gdp_md_est pop_year lastcensus gdp_year
1         8          7 28400000      22270       NA       1979       NA
2         6          1 12799293     110300       NA       1970       NA
3         1          6  3639453      21810       NA       2001       NA
4         3          3  4798491     184300       NA       2010       NA
5         3         13 40913584     573900       NA       2010       NA
6         2         10  2967004      18770       NA       2001       NA
                    economy              income_grp wikipedia fips_10 iso_a2 iso_a3
1 7. Least developed region           5. Low income        NA    <NA>     AF    AFG
2 7. Least developed region  3. Upper middle income        NA    <NA>     AO    AGO
3      6. Developing region  4. Lower middle income        NA    <NA>     AL    ALB
4      6. Developing region 2. High income: nonOECD        NA    <NA>     AE    ARE
5   5. Emerging region: G20  3. Upper middle income        NA    <NA>     AR    ARG
6      6. Developing region  4. Lower middle income        NA    <NA>     AM    ARM
  iso_n3 un_a3 wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un adm0_a3_wb
1    004   004    AF   AFG     NA        AFG        AFG         NA         NA
2    024   024    AO   AGO     NA        AGO        AGO         NA         NA
3    008   008    AL   ALB     NA        ALB        ALB         NA         NA
4    784   784    AE   ARE     NA        ARE        ARE         NA         NA
5    032   032    AR   ARG     NA        ARG        ARG         NA         NA
6    051   051    AM   ARM     NA        ARM        ARM         NA         NA
      continent region_un       subregion                  region_wb name_len long_len
1          Asia      Asia   Southern Asia                 South Asia       11       11
2        Africa    Africa   Middle Africa         Sub-Saharan Africa        6        6
3        Europe    Europe Southern Europe      Europe & Central Asia        7        7
4          Asia      Asia    Western Asia Middle East & North Africa       20       20
5 South America  Americas   South America  Latin America & Caribbean        9        9
6          Asia      Asia    Western Asia      Europe & Central Asia        7        7
  abbrev_len tiny homepart Rank Metric.Tons                       geometry
1          4   NA        1   NA        <NA> MULTIPOLYGON (((61.21082 35...
2          4   NA        1   44        2100 MULTIPOLYGON (((16.32653 -5...
3          4   NA        1   NA        <NA> MULTIPOLYGON (((20.59025 41...
4          6   NA        1   NA        <NA> MULTIPOLYGON (((51.57952 24...
5          4   NA        1   NA        <NA> MULTIPOLYGON (((-65.5 -55.2...
6          4   NA        1   NA        <NA> MULTIPOLYGON (((43.58275 41...

最後に地図を描きます。

ggplot(data =  world_joined)+
  geom_sf(aes(fill = as.numeric(world_joined$Metric.Tons)), 
          color = "black")+ 
  labs(fill='Coffee production')  +
  scale_fill_viridis_c(option = "viridis")

データは表であらわします。

library(kableExtra)
library(clipr)
coffee_df %>% 
  kable(align = "c", row.names=FALSE) %>%
  kable_styling(full_width = F) %>%
  column_spec(1, bold = T) %>%
  collapse_rows(columns = 1, valign = "middle") %>% 
  write_clip
Rank Country X60.kilogram.bags Metric.Tons Pounds
1 Brazil 44,200,000 2,652,000 5,714,381,000
2 Vietnam 27,500,000 1,650,000 3,637,627,000
3 Colombia 13,500,000 810,000 1,785,744,000
4 Indonesia 11,000,000 660,000 1,455,050,000
5 Ethiopia 6,400,000 384,000 846,575,000
6 Honduras 5,800,000 348,000 767,208,000
7 India 5,800,000 348,000 767,208,000
8 Uganda 4,800,000 288,000 634,931,000
9 Mexico 3,900,000 234,000 515,881,000
10 Guatemala 3,400,000 204,000 449,743,000
11 Peru 3,200,000 192,000 423,287,000
12 Nicaragua 2,200,000 132,000 291,010,000
13 China(2013/14 est.)[7] 1,947,000 116,820 257,544,000
14 Ivory Coast 1,800,000 108,000 238,099,000
15 Costa Rica 1,492,000 89,520 197,357,000
16 Kenya 833,000 49,980 110,187,000
17 Papua New Guinea 800,000 48,000 105,821,000
18 Tanzania 800,000 48,000 105,821,000
19 El Salvador 762,000 45,720 100,795,000
20 Ecuador 700,000 42,000 92,594,000
21 Cameroon 570,000 34,200 75,398,000
22 Laos 520,000 31,200 68,784,000
23 Madagascar 520,000 31,200 68,784,000
24 Gabon 500,000 30,000 66,138,000
25 Thailand 500,000 30,000 66,138,000
26 Venezuela 500,000 30,000 66,138,000
27 Dominican Republic 400,000 24,000 52,910,000
28 Haiti 350,000 21,000 46,297,000
29 Democratic Republic of the Congo 335,000 20,100 44,312,000
30 Rwanda 250,000 15,000 33,069,000
31 Burundi 200,000 12,000 26,455,000
32 Philippines 200,000 12,000 26,455,000
33 Togo 200,000 12,000 26,455,000
34 Guinea 160,000 9,600 21,164,000
35 Yemen 120,000 7,200 15,873,000
36 Cuba 100,000 6,000 13,227,000
37 Panama 100,000 6,000 13,227,000
38 Bolivia 90,000 5,400 11,904,000
39 Timor Leste 80,000 4,800 10,582,000
40 Central African Republic 65,000 3,900 8,598,000
41 Nigeria 40,000 2,400 5,291,000
42 Ghana 37,000 2,220 4,894,000
43 Sierra Leone 36,000 2,160 4,761,000
44 Angola 35,000 2,100 4,629,000
45 Jamaica 21,000 1,260 2,777,000
46 Paraguay 20,000 1,200 2,645,000
47 Malawi 16,000 960 2,116,000
48 Trinidad and Tobago 12,000 720 1,587,000
49 Zimbabwe 10,000 600 1,322,000
50 Liberia 6,000 360 793,000
51 Zambia 2,000 120 264,000

Add a Comment

メールアドレスが公開されることはありません。