본문 바로가기
  • "You can't manage what you can't measure" Peter Drucker
데이터 분석 (with Rstudio)

[Rstudio] 세계지도 그리기와 색칠하기 (전체, 국가별 지도)

by Vitaminymc 2024. 4. 15.
반응형

[ 세계 지도 그리기 🌏 ]

[ maps 패키지 ]

maps 패키지가 제공하는 지도를 map_data 함수를 통해서 load하고 ggplot() + geom_polygon()을 활용하여 지도를 작성

install.packages('maps')
library(maps)

library(tidyverse)

world <- map_data(map='world')

str(world)
> str(world)
'data.frame':	99338 obs. of  6 variables:
 $ long     : num  -69.9 -69.9 -69.9 -70 -70.1 ...
 $ lat      : num  12.5 12.4 12.4 12.5 12.5 ...
 $ group    : num  1 1 1 1 1 1 1 1 1 1 ...
 $ order    : int  1 2 3 4 5 6 7 8 9 10 ...
 $ region   : chr  "Aruba" "Aruba" "Aruba" "Aruba" ...
 $ subregion: chr  NA NA NA NA ...
#세계지도 1
ggplot(world, aes(x=long, y=lat, group=group))+
  geom_polygon()

#세계지도 2
ggplot(world, aes(x=long, y=lat, group=group))+
  geom_polygon(aes(fill=region), color='white', show.legend=FALSE)+
  coord_quickmap()

세계지도1
세계지도 2

[ World 데이터 포함 국가  ]

세계 252국가 (4 X 63)

더보기
Afghanistan Dominican Republic Lithuania Saint Lucia
Albania Ecuador Luxembourg Saint Martin
Algeria Egypt Madagascar Saint Pierre and Miquelon
American Samoa El Salvador Madeira Islands Saint Vincent
Andorra Equatorial Guinea Malawi Samoa
Angola Eritrea Malaysia San Marino
Anguilla Estonia Maldives Sao Tome and Principe
Antarctica Ethiopia Mali Saudi Arabia
Antigua Falkland Islands Malta Senegal
Argentina Faroe Islands Marshall Islands Serbia
Armenia Fiji Martinique Seychelles
Aruba Finland Mauritania Siachen Glacier
Ascension Island France Mauritius Sierra Leone
Australia French Guiana Mayotte Singapore
Austria French Polynesia Mexico Sint Eustatius
Azerbaijan French Southern and
Antarctic Lands
Micronesia Sint Maarten
Azores Gabon Moldova Slovakia
Bahamas Gambia Monaco Slovenia
Bahrain Georgia Mongolia Solomon Islands
Bangladesh Germany Montenegro Somalia
Barbados Ghana Montserrat South Africa
Barbuda Greece Morocco South Georgia
Belarus Greenland Mozambique South Korea
Belgium Grenada Myanmar South Sandwich Islands
Belize Grenadines Namibia South Sudan
Benin Guadeloupe Nauru Spain
Bermuda Guam Nepal Sri Lanka
Bhutan Guatemala Netherlands Sudan
Bolivia Guernsey Nevis Suriname
Bonaire Guinea New Caledonia Swaziland
Bosnia and Herzegovina Guinea-Bissau New Zealand Sweden
Botswana Guyana Nicaragua Switzerland
Brazil Haiti Niger Syria
Brunei Heard Island Nigeria Taiwan
Bulgaria Honduras Niue Tajikistan
Burkina Faso Hungary Norfolk Island Tanzania
Burundi Iceland North Korea Thailand
Cambodia India North Macedonia Timor-Leste
Cameroon Indonesia Northern Mariana Islands Tobago
Canada Iran Norway Togo
Canary Islands Iraq Oman Tonga
Cape Verde Ireland Pakistan Trinidad
Cayman Islands Isle of Man Palau Tunisia
Central African Republic Israel Palestine Turkey
Chad Italy Panama Turkmenistan
Chagos Archipelago Ivory Coast Papua New Guinea Turks and Caicos Islands
Chile Jamaica Paraguay Uganda
China Japan Peru UK
Christmas Island Jersey Philippines Ukraine
Cocos Islands Jordan Pitcairn Islands United Arab Emirates
Colombia Kazakhstan Poland Uruguay
Comoros Kenya Portugal USA
Cook Islands Kiribati Puerto Rico Uzbekistan
Costa Rica Kosovo Qatar Vanuatu
Croatia Kuwait Republic of Congo Vatican
Cuba Kyrgyzstan Reunion Venezuela
Curacao Laos Romania Vietnam
Cyprus Latvia Russia Virgin Islands
Czech Republic Lebanon Rwanda Wallis and Futuna
Democratic Republic of the Congo Lesotho Saba Western Sahara
Denmark Liberia Saint Barthelemy Yemen
Djibouti Libya Saint Helena Zambia
Dominica Liechtenstein Saint Kitts Zimbabwe

[ 대한민국, 중국, 일본 지도 그리기 ]

대한민국 (남한, 북한 구분), 중국, 일본을 지정해서 Load하여 작성

kcj <- map_data('world', region= c('south korea', 'north korea','china' , 'japan'))

ggplot(kcj, aes(x=long, y=lat, group=group))+
  geom_polygon(aes(fill=region), color='white', show.legend=FALSE)+
  coord_quickmap()

[ 대한민국 도시  지도 표시 ]

maps 패키지에 포함된 도시 기준

cities <- get('world.cities')

str(cities)
> str(cities)
'data.frame':	43645 obs. of  6 variables:
 $ name       : chr  "'Abasan al-Jadidah" "'Abasan al-Kabirah" "'Abdul Hakim" "'Abdullah-as-Salam" ...
 $ country.etc: chr  "Palestine" "Palestine" "Pakistan" "Kuwait" ...
 $ pop        : int  5629 18999 47788 21817 2456 3434 9198 5492 22706 41731 ...
 $ lat        : num  31.3 31.3 30.6 29.4 32 ...
 $ long       : num  34.3 34.4 72.1 48 35.1 ...
 $ capital    : int  0 0 0 0 0 0 0 0 0 0 ...
kor_cities <- cities[cities$country.etc == 'Korea South', ]

head(kor_cities)
nrow(kor_cities)
> head(kor_cities)
       name country.etc    pop   lat   long capital
429   Aewol Korea South  24095 33.47 126.32       0
1436 Andong Korea South 129708 36.56 128.72       0
1467 Angang Korea South  35042 35.98 129.24       0
1563  Ansan Korea South 668533 37.35 126.86       0
1577 Ansong Korea South  72978 37.01 127.28       0
1632 Anyang Korea South 643315 37.39 126.92       0

> nrow(kor_cities)
[1] 132
#대한민국(남한) 지도 load
korea <- map_data('world', region= c('south korea'))

#대한민국(남한) 지도 작성
p <- ggplot(korea, aes(x=long, y=lat, group=group))+
  geom_polygon(color='black', fill='white',show.legend=FALSE)+
  coord_quickmap()

#대한민국(남한) 도시 표시 (geom_point)
p + geom_point(data = kor_cities, aes(x = long, y = lat, size = pop, group=country.etc),
               color = 'blue')

[ Reference site ]

https://blog.naver.com/goldenezkang/220061647790

https://blog.naver.com/regenesis90/222223319082

https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_map.html


728x90