Map Examples

Author

hyun jhin lee

ggplot2, rnaturalearth 설치, 불러오기

Code
library(ggplot2)
# install.packages("rnaturalearth")
library(rnaturalearth)

세계지도 그리기 (rnaturalearth 팩키지 사용)

데이터 내용 보기

Code
str(countries110@data)
'data.frame':   177 obs. of  94 variables:
 $ featurecla: chr  "Admin-0 country" "Admin-0 country" "Admin-0 country" "Admin-0 country" ...
 $ scalerank : int  1 1 1 1 1 1 1 1 1 1 ...
 $ LABELRANK : int  6 3 7 2 2 3 3 2 2 2 ...
 $ SOVEREIGNT: chr  "Fiji" "United Republic of Tanzania" "Western Sahara" "Canada" ...
 $ SOV_A3    : chr  "FJI" "TZA" "SAH" "CAN" ...
 $ ADM0_DIF  : int  0 0 0 0 1 0 0 0 0 0 ...
 $ LEVEL     : int  2 2 2 2 2 2 2 2 2 2 ...
 $ TYPE      : chr  "Sovereign country" "Sovereign country" "Indeterminate" "Sovereign country" ...
 $ ADMIN     : chr  "Fiji" "United Republic of Tanzania" "Western Sahara" "Canada" ...
 $ ADM0_A3   : chr  "FJI" "TZA" "SAH" "CAN" ...
 $ GEOU_DIF  : int  0 0 0 0 0 0 0 0 0 0 ...
 $ GEOUNIT   : chr  "Fiji" "Tanzania" "Western Sahara" "Canada" ...
 $ GU_A3     : chr  "FJI" "TZA" "SAH" "CAN" ...
 $ SU_DIF    : int  0 0 0 0 0 0 0 1 0 0 ...
 $ SUBUNIT   : chr  "Fiji" "Tanzania" "Western Sahara" "Canada" ...
 $ SU_A3     : chr  "FJI" "TZA" "SAH" "CAN" ...
 $ BRK_DIFF  : int  0 0 1 0 0 0 0 0 0 0 ...
 $ NAME      : chr  "Fiji" "Tanzania" "W. Sahara" "Canada" ...
 $ NAME_LONG : chr  "Fiji" "Tanzania" "Western Sahara" "Canada" ...
 $ BRK_A3    : chr  "FJI" "TZA" "B28" "CAN" ...
 $ BRK_NAME  : chr  "Fiji" "Tanzania" "W. Sahara" "Canada" ...
 $ BRK_GROUP : chr  NA NA NA NA ...
 $ ABBREV    : chr  "Fiji" "Tanz." "W. Sah." "Can." ...
 $ POSTAL    : chr  "FJ" "TZ" "WS" "CA" ...
 $ FORMAL_EN : chr  "Republic of Fiji" "United Republic of Tanzania" "Sahrawi Arab Democratic Republic" "Canada" ...
 $ FORMAL_FR : chr  NA NA NA NA ...
 $ NAME_CIAWF: chr  "Fiji" "Tanzania" "Western Sahara" "Canada" ...
 $ NOTE_ADM0 : chr  NA NA "Self admin." NA ...
 $ NOTE_BRK  : chr  NA NA "Self admin.; Claimed by Morocco" NA ...
 $ NAME_SORT : chr  "Fiji" "Tanzania" "Western Sahara" "Canada" ...
 $ NAME_ALT  : chr  NA NA NA NA ...
 $ MAPCOLOR7 : int  5 3 4 6 4 6 2 4 6 3 ...
 $ MAPCOLOR8 : int  1 6 7 6 5 1 3 2 6 1 ...
 $ MAPCOLOR9 : int  2 2 4 2 1 6 5 3 6 3 ...
 $ MAPCOLOR13: int  2 2 4 2 1 1 4 1 11 13 ...
 $ POP_EST   : chr  "920938" "53950935" "603253" "35623680" ...
 $ POP_RANK  : int  11 16 11 15 17 14 15 13 17 15 ...
 $ GDP_MD_EST: num  8374 150600 906 1674000 18560000 ...
 $ POP_YEAR  : int  2017 2017 2017 2017 2017 2017 2017 2017 2017 2017 ...
 $ LASTCENSUS: int  2007 2002 NA 2011 2010 2009 1989 2000 2010 2010 ...
 $ GDP_YEAR  : int  2016 2016 2007 2016 2016 2016 2016 2016 2016 2016 ...
 $ ECONOMY   : chr  "6. Developing region" "7. Least developed region" "7. Least developed region" "1. Developed region: G7" ...
 $ INCOME_GRP: chr  "4. Lower middle income" "5. Low income" "5. Low income" "1. High income: OECD" ...
 $ WIKIPEDIA : int  NA NA NA NA 0 NA NA NA NA NA ...
 $ FIPS_10_  : chr  "FJ" "TZ" "WI" "CA" ...
 $ ISO_A2    : chr  "FJ" "TZ" "EH" "CA" ...
 $ ISO_A3    : chr  "FJI" "TZA" "ESH" "CAN" ...
 $ ISO_A3_EH : chr  "FJI" "TZA" "ESH" "CAN" ...
 $ ISO_N3    : chr  "242" "834" "732" "124" ...
 $ UN_A3     : chr  "242" "834" "732" "124" ...
 $ WB_A2     : chr  "FJ" "TZ" NA "CA" ...
 $ WB_A3     : chr  "FJI" "TZA" NA "CAN" ...
 $ WOE_ID    : int  23424813 23424973 23424990 23424775 23424977 -90 23424980 23424926 23424846 23424747 ...
 $ WOE_ID_EH : int  23424813 23424973 23424990 23424775 23424977 23424871 23424980 23424926 23424846 23424747 ...
 $ WOE_NOTE  : chr  "Exact WOE match as country" "Exact WOE match as country" "Exact WOE match as country" "Exact WOE match as country" ...
 $ ADM0_A3_IS: chr  "FJI" "TZA" "MAR" "CAN" ...
 $ ADM0_A3_US: chr  "FJI" "TZA" "SAH" "CAN" ...
 $ ADM0_A3_UN: int  NA NA NA NA NA NA NA NA NA NA ...
 $ ADM0_A3_WB: int  NA NA NA NA NA NA NA NA NA NA ...
 $ CONTINENT : chr  "Oceania" "Africa" "Africa" "North America" ...
 $ REGION_UN : chr  "Oceania" "Africa" "Africa" "Americas" ...
 $ SUBREGION : chr  "Melanesia" "Eastern Africa" "Northern Africa" "Northern America" ...
 $ REGION_WB : chr  "East Asia & Pacific" "Sub-Saharan Africa" "Middle East & North Africa" "North America" ...
 $ NAME_LEN  : int  4 8 9 6 24 10 10 16 9 9 ...
 $ LONG_LEN  : int  4 8 14 6 13 10 10 16 9 9 ...
 $ ABBREV_LEN: int  4 5 7 4 6 4 4 6 5 4 ...
 $ TINY      : int  NA NA NA NA NA NA 5 NA NA NA ...
 $ HOMEPART  : int  1 1 1 1 1 1 1 1 1 1 ...
 $ MIN_ZOOM  : num  0 0 4.7 0 0 0 0 0 0 0 ...
 $ MIN_LABEL : num  3 3 6 1.7 1.7 3 3 2.5 1.7 2 ...
 $ MAX_LABEL : num  8 8 11 5.7 5.7 7 8 7.5 6.7 7 ...
 $ NE_ID     : chr  "1159320625" "1159321337" "1159321223" "1159320467" ...
 $ WIKIDATAID: chr  "Q712" "Q924" "Q6250" "Q16" ...
 $ NAME_AR   : chr  NA NA NA NA ...
 $ NAME_BN   : chr  NA NA NA NA ...
 $ NAME_DE   : chr  "Fidschi" "Tansania" "Westsahara" "Kanada" ...
 $ NAME_EN   : chr  "Fiji" "Tanzania" "Western Sahara" "Canada" ...
 $ NAME_ES   : chr  "Fiyi" "Tanzania" "Sahara Occidental" "Canadá" ...
 $ NAME_FR   : chr  "Fidji" "Tanzanie" "Sahara occidental" "Canada" ...
 $ NAME_EL   : chr  NA NA NA NA ...
 $ NAME_HI   : chr  NA NA NA NA ...
 $ NAME_HU   : chr  "Fidzsi-szigetek" "Tanzánia" "Nyugat-Szahara" "Kanada" ...
 $ NAME_ID   : chr  "Fiji" "Tanzania" "Sahara Barat" "Kanada" ...
 $ NAME_IT   : chr  "Figi" "Tanzania" "Sahara Occidentale" "Canada" ...
 $ NAME_JA   : chr  NA NA NA NA ...
 $ NAME_KO   : chr  NA NA NA NA ...
 $ NAME_NL   : chr  "Fiji" "Tanzania" "Westelijke Sahara" "Canada" ...
 $ NAME_PL   : chr  "Fidzi" "Tanzania" "Sahara Zachodnia" "Kanada" ...
 $ NAME_PT   : chr  "Fiji" "Tanzânia" "Saara Ocidental" "Canadá" ...
 $ NAME_RU   : chr  NA NA NA NA ...
 $ NAME_SV   : chr  "Fiji" "Tanzania" "Västsahara" "Kanada" ...
 $ NAME_TR   : chr  "Fiji" "Tanzanya" "Bati Sahra" "Kanada" ...
 $ NAME_VI   : chr  "Fiji" "Tanzania" "Tây Sahara" "Canada" ...
 $ NAME_ZH   : chr  NA NA NA NA ...
Code
head(countries110@data, 2)
Code
countries110@data$CONTINENT #국가별 대륙명 보기
  [1] "Oceania"                 "Africa"                 
  [3] "Africa"                  "North America"          
  [5] "North America"           "Asia"                   
  [7] "Asia"                    "Oceania"                
  [9] "Asia"                    "South America"          
 [11] "South America"           "Africa"                 
 [13] "Africa"                  "Africa"                 
 [15] "Africa"                  "Africa"                 
 [17] "North America"           "North America"          
 [19] "Europe"                  "North America"          
 [21] "South America"           "Europe"                 
 [23] "North America"           "Seven seas (open ocean)"
 [25] "Asia"                    "Africa"                 
 [27] "Africa"                  "North America"          
 [29] "South America"           "South America"          
 [31] "South America"           "South America"          
 [33] "South America"           "North America"          
 [35] "North America"           "North America"          
 [37] "North America"           "North America"          
 [39] "North America"           "North America"          
 [41] "South America"           "South America"          
 [43] "South America"           "Europe"                 
 [45] "South America"           "North America"          
 [47] "North America"           "North America"          
 [49] "Africa"                  "Africa"                 
 [51] "Africa"                  "Africa"                 
 [53] "Africa"                  "Africa"                 
 [55] "Africa"                  "Africa"                 
 [57] "Africa"                  "Africa"                 
 [59] "Africa"                  "Africa"                 
 [61] "Africa"                  "Africa"                 
 [63] "Africa"                  "Africa"                 
 [65] "Africa"                  "Africa"                 
 [67] "Africa"                  "Africa"                 
 [69] "Africa"                  "Africa"                 
 [71] "Africa"                  "Africa"                 
 [73] "Africa"                  "Africa"                 
 [75] "Africa"                  "Africa"                 
 [77] "Asia"                    "Asia"                   
 [79] "Africa"                  "Asia"                   
 [81] "Africa"                  "Africa"                 
 [83] "Africa"                  "Asia"                   
 [85] "Asia"                    "Asia"                   
 [87] "Asia"                    "Asia"                   
 [89] "Asia"                    "Oceania"                
 [91] "Asia"                    "Asia"                   
 [93] "Asia"                    "Asia"                   
 [95] "Asia"                    "Asia"                   
 [97] "Asia"                    "Asia"                   
 [99] "Asia"                    "Asia"                   
[101] "Asia"                    "Asia"                   
[103] "Asia"                    "Asia"                   
[105] "Asia"                    "Asia"                   
[107] "Asia"                    "Asia"                   
[109] "Asia"                    "Asia"                   
[111] "Europe"                  "Europe"                 
[113] "Europe"                  "Europe"                 
[115] "Europe"                  "Europe"                 
[117] "Europe"                  "Europe"                 
[119] "Europe"                  "Europe"                 
[121] "Europe"                  "Europe"                 
[123] "Europe"                  "Europe"                 
[125] "Asia"                    "Europe"                 
[127] "Europe"                  "Europe"                 
[129] "Europe"                  "Europe"                 
[131] "Europe"                  "Europe"                 
[133] "Europe"                  "Europe"                 
[135] "Oceania"                 "Oceania"                
[137] "Oceania"                 "Oceania"                
[139] "Asia"                    "Asia"                   
[141] "Asia"                    "Europe"                 
[143] "Europe"                  "Europe"                 
[145] "Europe"                  "Asia"                   
[147] "Asia"                    "Asia"                   
[149] "Asia"                    "Asia"                   
[151] "Europe"                  "Europe"                 
[153] "Europe"                  "Europe"                 
[155] "Africa"                  "Asia"                   
[157] "South America"           "Asia"                   
[159] "Asia"                    "Antarctica"             
[161] "Asia"                    "Asia"                   
[163] "Africa"                  "Africa"                 
[165] "Africa"                  "Africa"                 
[167] "Africa"                  "Africa"                 
[169] "Africa"                  "Africa"                 
[171] "Europe"                  "Europe"                 
[173] "Europe"                  "Europe"                 
[175] "Europe"                  "North America"          
[177] "Africa"                 
Code
countries110@data$NAME #국가명
  [1] "Fiji"                     "Tanzania"                
  [3] "W. Sahara"                "Canada"                  
  [5] "United States of America" "Kazakhstan"              
  [7] "Uzbekistan"               "Papua New Guinea"        
  [9] "Indonesia"                "Argentina"               
 [11] "Chile"                    "Dem. Rep. Congo"         
 [13] "Somalia"                  "Kenya"                   
 [15] "Sudan"                    "Chad"                    
 [17] "Haiti"                    "Dominican Rep."          
 [19] "Russia"                   "Bahamas"                 
 [21] "Falkland Is."             "Norway"                  
 [23] "Greenland"                "Fr. S. Antarctic Lands"  
 [25] "Timor-Leste"              "South Africa"            
 [27] "Lesotho"                  "Mexico"                  
 [29] "Uruguay"                  "Brazil"                  
 [31] "Bolivia"                  "Peru"                    
 [33] "Colombia"                 "Panama"                  
 [35] "Costa Rica"               "Nicaragua"               
 [37] "Honduras"                 "El Salvador"             
 [39] "Guatemala"                "Belize"                  
 [41] "Venezuela"                "Guyana"                  
 [43] "Suriname"                 "France"                  
 [45] "Ecuador"                  "Puerto Rico"             
 [47] "Jamaica"                  "Cuba"                    
 [49] "Zimbabwe"                 "Botswana"                
 [51] "Namibia"                  "Senegal"                 
 [53] "Mali"                     "Mauritania"              
 [55] "Benin"                    "Niger"                   
 [57] "Nigeria"                  "Cameroon"                
 [59] "Togo"                     "Ghana"                   
 [61] "Côte d'Ivoire"            "Guinea"                  
 [63] "Guinea-Bissau"            "Liberia"                 
 [65] "Sierra Leone"             "Burkina Faso"            
 [67] "Central African Rep."     "Congo"                   
 [69] "Gabon"                    "Eq. Guinea"              
 [71] "Zambia"                   "Malawi"                  
 [73] "Mozambique"               "eSwatini"                
 [75] "Angola"                   "Burundi"                 
 [77] "Israel"                   "Lebanon"                 
 [79] "Madagascar"               "Palestine"               
 [81] "Gambia"                   "Tunisia"                 
 [83] "Algeria"                  "Jordan"                  
 [85] "United Arab Emirates"     "Qatar"                   
 [87] "Kuwait"                   "Iraq"                    
 [89] "Oman"                     "Vanuatu"                 
 [91] "Cambodia"                 "Thailand"                
 [93] "Laos"                     "Myanmar"                 
 [95] "Vietnam"                  "North Korea"             
 [97] "South Korea"              "Mongolia"                
 [99] "India"                    "Bangladesh"              
[101] "Bhutan"                   "Nepal"                   
[103] "Pakistan"                 "Afghanistan"             
[105] "Tajikistan"               "Kyrgyzstan"              
[107] "Turkmenistan"             "Iran"                    
[109] "Syria"                    "Armenia"                 
[111] "Sweden"                   "Belarus"                 
[113] "Ukraine"                  "Poland"                  
[115] "Austria"                  "Hungary"                 
[117] "Moldova"                  "Romania"                 
[119] "Lithuania"                "Latvia"                  
[121] "Estonia"                  "Germany"                 
[123] "Bulgaria"                 "Greece"                  
[125] "Turkey"                   "Albania"                 
[127] "Croatia"                  "Switzerland"             
[129] "Luxembourg"               "Belgium"                 
[131] "Netherlands"              "Portugal"                
[133] "Spain"                    "Ireland"                 
[135] "New Caledonia"            "Solomon Is."             
[137] "New Zealand"              "Australia"               
[139] "Sri Lanka"                "China"                   
[141] "Taiwan"                   "Italy"                   
[143] "Denmark"                  "United Kingdom"          
[145] "Iceland"                  "Azerbaijan"              
[147] "Georgia"                  "Philippines"             
[149] "Malaysia"                 "Brunei"                  
[151] "Slovenia"                 "Finland"                 
[153] "Slovakia"                 "Czechia"                 
[155] "Eritrea"                  "Japan"                   
[157] "Paraguay"                 "Yemen"                   
[159] "Saudi Arabia"             "Antarctica"              
[161] "N. Cyprus"                "Cyprus"                  
[163] "Morocco"                  "Egypt"                   
[165] "Libya"                    "Ethiopia"                
[167] "Djibouti"                 "Somaliland"              
[169] "Uganda"                   "Rwanda"                  
[171] "Bosnia and Herz."         "Macedonia"               
[173] "Serbia"                   "Montenegro"              
[175] "Kosovo"                   "Trinidad and Tobago"     
[177] "S. Sudan"                

세계지도 그리기

Code
ne_map = ne_countries(scale = "small")
df_for = fortify(ne_map) ##shape file---> data frame으로 변경 
Regions defined for each Polygons
Code
head(df_for, 2)
Code
ggplot(data = df_for) + 
  geom_polygon(aes(x = long, y = lat,group = group), fill = "#FFFFFF", color = "blue")

대륙별로 그리기

Code
ne_map_asia = ne_countries(scale = "small", continent = "Asia")
df_for_a = fortify(ne_map_asia)
Regions defined for each Polygons
Code
ggplot(data = df_for_a) + 
  geom_polygon(aes(x = long, y = lat,group = group), fill = "#FFFFFF", color = "blue")

국가별로 그리기(South Korea)

Code
ne_map_k = ne_countries(scale = "small", country = "South Korea") #scale: small. medium, large 
df_for_k = fortify(ne_map_k)
Regions defined for each Polygons
Code
ggplot(data = df_for_k) + 
  geom_polygon(aes(x = long, y = lat,group = group), fill = "#FFFFFF", color = "blue")+coord_fixed(ratio = 1)

구글 지도 사용하기

  1. https://mapsplatform.google.com 에서 geocoding api신청 https://datadoctorblog.com/2021/01/29/etc-google-cloud-platform-geocoding/
  2. R Console에 register_google(key=‘자기 인증키’) 입력 https://kuduz.tistory.com/1042 (참고)
Code
# install.packages("ggmap")
library(ggmap)
ℹ Google's Terms of Service: ]8;;https://mapsplatform.google.com<https://mapsplatform.google.com>]8;;
ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
Code
#register_google(key='')
ggmap(get_map(location='south korea', zoom=7)) # zoom은 3부터 21까지 지정. 숫자가 작을수록 넓은 지역
ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=south%20korea&zoom=7&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=south%20korea&zoom=7&size=640x640&scale=2&maptype=terrain&language=en-EN&key=xxx]8;;>
ℹ <]8;;https://maps.googleapis.com/maps/api/geocode/json?address=south+korea&key=xxxhttps://maps.googleapis.com/maps/api/geocode/json?address=south+korea&key=xxx]8;;>

maptype=‘terrain’,‘satellite’,‘roadmap’, ‘hybrid’

Code
map <- get_map(location='south korea', zoom=7, maptype='satellite', color='bw')
ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=south%20korea&zoom=7&size=640x640&scale=2&maptype=satellite&language=en-EN&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=south%20korea&zoom=7&size=640x640&scale=2&maptype=satellite&language=en-EN&key=xxx]8;;>
ℹ <]8;;https://maps.googleapis.com/maps/api/geocode/json?address=south+korea&key=xxxhttps://maps.googleapis.com/maps/api/geocode/json?address=south+korea&key=xxx]8;;>
Code
ggmap(map)

ggmap으로 미국지도 그리기

Code
map_a <- get_map(location='usa', zoom=4, maptype='roadmap', color='bw')
ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=usa&zoom=4&size=640x640&scale=2&maptype=roadmap&language=en-EN&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=usa&zoom=4&size=640x640&scale=2&maptype=roadmap&language=en-EN&key=xxx]8;;>
ℹ <]8;;https://maps.googleapis.com/maps/api/geocode/json?address=usa&key=xxxhttps://maps.googleapis.com/maps/api/geocode/json?address=usa&key=xxx]8;;>
Code
ggmap(map_a)

구글맵위에 데이터 붙이기

Code
library(readr)
wifi <- read_csv("/Users/hyunjhinlee/Desktop/R/23_spring/wifi.csv")
Rows: 12290 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): company
dbl (2): lat, lon

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Code
head(wifi)
Code
map <- get_map(location='south korea', zoom=7, maptype='roadmap', color='bw')
ℹ <]8;;https://maps.googleapis.com/maps/api/staticmap?center=south%20korea&zoom=7&size=640x640&scale=2&maptype=roadmap&language=en-EN&key=xxxhttps://maps.googleapis.com/maps/api/staticmap?center=south%20korea&zoom=7&size=640x640&scale=2&maptype=roadmap&language=en-EN&key=xxx]8;;>
ℹ <]8;;https://maps.googleapis.com/maps/api/geocode/json?address=south+korea&key=xxxhttps://maps.googleapis.com/maps/api/geocode/json?address=south+korea&key=xxx]8;;>
Code
ggmap(map) + geom_point(data=wifi, aes(x=lon, y=lat, color=company), alpha=0.1)

Code
wifi_map <-ggmap(map) + geom_point(data=wifi, aes(x=lon, y=lat), alpha=0.01)
wifi_map

우리나라 행정구역 경계지도 ( GADM 사용 )

Code
# install.packages("raster")
library(raster) #Loading required package: sp
Loading required package: sp
Code
korea <- getData('GADM', country='kor', level=2)
Warning in getData("GADM", country = "kor", level = 2): getData will be removed in a future version of raster
. Please use the geodata package instead
Code
ggplot() + geom_polygon(data=korea, aes(x=long, y=lat, group=group), fill='white', color='black')+coord_fixed(ratio = 1)
Regions defined for each Polygons

서울 행정구역 경계지도

Code
korea_gadm <- getData('GADM', country='kor', level=2)
Warning in getData("GADM", country = "kor", level = 2): getData will be removed in a future version of raster
. Please use the geodata package instead
Code
seoul_gadm <- korea_gadm[korea_gadm$NAME_1 %in% 'Seoul', ]
 ggplot()+geom_polygon(data=seoul_gadm, aes(x=long, y=lat, group=group), fill='white', color='black') 
Regions defined for each Polygons

수도권 경계지도

Code
sudogwon_gadm <-korea_gadm[korea_gadm$NAME_1  %in% c('Seoul', 'Incheon', 'Gyeonggi-do'), ]
ggplot() + geom_polygon(data=sudogwon_gadm, aes(x=long, y=lat, group=group), fill='white', color='black') + xlim(c(125.5, 128)) + ylim(c(36.75, 38.25))
Regions defined for each Polygons

대한민국 행정구역 shape file 사용

http://www.gisdeveloper.co.kr/?p=2332

Code
library(raster)
library(sp)
library(rgdal)
Please note that rgdal will be retired during 2023,
plan transition to sf/stars/terra functions using GDAL and PROJ
at your earliest convenience.
See https://r-spatial.org/r/2022/04/12/evolution.html and https://github.com/r-spatial/evolution
rgdal: version: 1.6-6, (SVN revision 1201)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 3.4.2, released 2022/03/08
Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.2/Resources/library/rgdal/gdal
GDAL binary built with GEOS: FALSE 
Loaded PROJ runtime: Rel. 8.2.1, January 1st, 2022, [PJ_VERSION: 821]
Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.2/Resources/library/rgdal/proj
PROJ CDN enabled: FALSE
Linking to sp version:1.6-0
To mute warnings of possible GDAL/OSR exportToProj4() degradation,
use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
Code
korea_s <- shapefile("/Users/hyunjhinlee/Desktop/R/sig.shp", encoding = 'euc-kr' ) #encoding
korea_sm <- ggplot() + geom_polygon(data=korea_s, aes(x=long, y=lat, group=group), fill='white', color='black') 
Regions defined for each Polygons
Code
korea_sm ##오래걸림 

Code
#install.packages("rgeos")
#install.packages("maptools")
#install.packages("rgdal") #3개를 순서대로 설치해야함.
library(rgeos)
rgeos version: 0.6-2, (SVN revision 693)
 GEOS runtime version: 3.10.2-CAPI-1.16.0 
 Please note that rgeos will be retired during 2023,
plan transition to sf functions using GEOS at your earliest convenience.
 GEOS using OverlayNG
 Linking to sp version: 1.6-0 
 Polygon checking: TRUE 
Code
library(maptools)
Checking rgeos availability: TRUE
Please note that 'maptools' will be retired during 2023,
plan transition at your earliest convenience;
some functionality will be moved to 'sp'.
Code
library(rgdal)
head(korea_s)
Code
head(korea_s@data) ## 같음 
Code
seoul_s <- korea_s[korea_s$SIG_CD <=11740, ]  #코드11740 이하 서울 
seoul_df <- fortify(seoul_s, region = 'SIG_KOR_NM')
seoul_map <- ggplot()+geom_polygon(data=seoul_df, aes(x=long, y=lat, group=group, color=id), fill='white')
seoul_map

좌표계 변환하여 지도에 다른 데이터 붙이기

https://lovetoken.github.io/r/data_visualization/2018/04/15/sp_proj4string_spTransform.html(참조)

Code
library(sp)
library(rgdal)
slotNames(seoul_s)
[1] "data"        "polygons"    "plotOrder"   "bbox"        "proj4string"
Code
seoul_s@polygons[[1]]@Polygons[[1]]@coords[1:4, ]
         x       y
1 956615.5 1953567
2 956621.6 1953565
3 956626.2 1953564
4 956638.8 1953562
Code
seoul_s@proj4string
Coordinate Reference System:
Deprecated Proj.4 representation:
 +proj=tmerc +lat_0=38 +lon_0=127.5 +k=0.9996 +x_0=1000000 +y_0=2000000
+ellps=GRS80 +units=m +no_defs 
WKT2 2019 representation:
PROJCRS["unknown",
    BASEGEOGCRS["unknown",
        DATUM["Unknown based on GRS80 ellipsoid",
            ELLIPSOID["GRS 1980",6378137,298.257222101,
                LENGTHUNIT["metre",1],
                ID["EPSG",7019]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8901]]],
    CONVERSION["unknown",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",38,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",127.5,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",0.9996,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",1000000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",2000000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]]] 
Code
from_crs = CRS("+proj=tmerc +lat_0=38 +lon_0=127.5 +k=0.9996 +x_0=1000000 +y_0=2000000 +ellps=GRS80 +units=m +no_defs")
to_crs = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
proj4string(seoul_s) <- from_crs
seoul_s2 <- spTransform(seoul_s, to_crs) ##좌표계 전환 
Warning: PROJ support is provided by the sf and terra packages among others
Code
seoul_s2@polygons[[1]]@Polygons[[1]]@coords[1:4, ]
         [,1]     [,2]
[1,] 127.0086 37.58047
[2,] 127.0087 37.58045
[3,] 127.0088 37.58044
[4,] 127.0089 37.58042
Code
seoul_s_df <- fortify(seoul_s2)
Regions defined for each Polygons
Code
head(seoul_s_df)
Code
seoul_map2 <- ggplot()+geom_polygon(data=seoul_s_df, aes(x=long, y=lat, group=group), fill='white', color='grey')
seoul_map2

Code
seoul_wifi <-seoul_map2 + geom_point(data=wifi, aes(x=lon, y=lat, color=company), alpha=0.5)+ xlim(c(126.7, 127.3)) + ylim(c(37.3, 38.0)) + coord_fixed(ratio=1)
seoul_wifi
Warning: Removed 10118 rows containing missing values (`geom_point()`).