首页 > 解决方案 > 如何在 r(ggmap,ggplot) 中修复地图上的名称和警告消息

问题描述

我创建了在世界地图上显示国家名称的世界地图。但是,我的问题是所有名称都相互重叠,因此很难在地图上看到,另一个问题是 R 显示警告消息说“缺失值”。下面是数据集和其他信息。

输入

> dput(d1)
structure(list(area = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 8L, 
9L, 11L, 12L, 13L, 16L, 17L, 18L, 19L, 21L, 22L, 23L, 24L, 25L, 
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
39L, 40L, 42L, 43L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 55L, 
56L, 57L, 58L, 59L, 60L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 
70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 
83L, 85L, 86L, 87L, 89L, 91L, 92L, 93L, 96L, 97L, 98L, 99L, 100L, 
101L, 102L, 103L, 104L, 105L, 107L, 108L, 109L, 110L, 111L, 112L, 
113L, 114L, 115L, 116L, 117L, 118L, 120L, 121L, 122L, 123L, 124L, 
125L, 126L, 127L, 129L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 
138L, 140L, 143L, 144L, 145L, 146L, 148L, 149L, 150L, 151L, 153L, 
155L, 156L, 157L, 158L, 159L, 161L, 163L, 164L, 165L, 166L, 167L, 
169L, 170L, 171L, 172L, 173L, 174L), .Label = c("Afghanistan", 
"Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina", 
"Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bangladesh", 
"Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", 
"Bolivia (Plurinational State of)", "Bosnia and Herzegovina", 
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", 
"Cabo Verde", "Cambodia", "Cameroon", "Canada", "Central African Republic", 
"Chad", "Chile", "China, Hong Kong SAR", "China, Macao SAR", 
"China, mainland", "China, Taiwan Province of", "Colombia", "Congo", 
"Costa Rica", "Côte d'Ivoire", "Croatia", "Cuba", "Cyprus", "Czechia", 
"Democratic People's Republic of Korea", "Denmark", "Djibouti", 
"Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", 
"Estonia", "Ethiopia", "Fiji", "Finland", "France", "French Polynesia", 
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada", 
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", 
"Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", 
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", 
"Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", 
"Latvia", "Lebanon", "Lesotho", "Liberia", "Lithuania", "Luxembourg", 
"Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", 
"Mauritania", "Mauritius", "Mexico", "Mongolia", "Montenegro", 
"Morocco", "Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", 
"New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", 
"Norway", "Oman", "Pakistan", "Panama", "Paraguay", "Peru", "Philippines", 
"Poland", "Portugal", "Republic of Korea", "Republic of Moldova", 
"Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", 
"Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "Sao Tome and Principe", 
"Saudi Arabia", "Senegal", "Serbia", "Sierra Leone", "Slovakia", 
"Slovenia", "Solomon Islands", "South Africa", "Spain", "Sri Lanka", 
"Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Tajikistan", 
"Thailand", "The former Yugoslav Republic of Macedonia", "Timor-Leste", 
"Togo", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", 
"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", 
"United Republic of Tanzania", "United States of America", "Uruguay", 
"Uzbekistan", "Vanuatu", "Venezuela (Bolivarian Republic of)", 
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"), class = "factor"), 
    latitude = c(33.94, 41.15, 28.03, -11.2, 17.06, -38.42, -25.27, 
    47.52, 25.03, 23.68, 13.19, 17.19, 9.31, 32.32, -16.29, -22.33, 
    -14.24, 4.54, 42.73, 12.24, 16, 12.57, 7.37, 56.13, 6.61, 
    15.45, -35.68, 22.4, 22.2, 35.86, 23.7, 4.57, -0.23, 9.75, 
    7.54, 21.52, 35.13, 40.34, 56.26, 11.83, 15.41, 18.74, -1.83, 
    26.82, 13.79, -16.58, 61.92, 46.23, -17.68, -0.8, 13.44, 
    51.17, 7.95, 39.07, 12.26, 15.78, 9.95, 11.8, 4.86, 18.97, 
    15.2, 47.16, 64.96, 20.59, -0.79, 32.43, 33.22, 53.41, 31.05, 
    41.87, 18.11, 36.2, 30.59, -0.02, -3.37, 29.31, 19.86, 33.85, 
    -29.61, 6.43, -18.77, -13.25, 4.21, 3.2, 17.57, 35.94, 21.01, 
    -20.35, 23.63, 46.86, 31.79, -18.67, 21.91, -22.96, 28.39, 
    52.13, -20.9, -40.9, 12.87, 17.61, 9.08, 60.47, 30.38, 8.54, 
    -23.44, -9.19, 12.88, 51.92, 39.4, 35.91, 45.94, -1.94, 17.36, 
    13.91, 12.98, -13.76, 0.19, 23.89, 14.5, 8.46, -9.65, -30.56, 
    40.46, 7.87, 3.92, -26.52, 60.13, 46.82, 15.87, -8.87, 8.62, 
    10.69, 33.89, 38.96, 1.37, 23.42, 55.38, -6.37, 37.09, -32.52, 
    -15.38, 6.42, 14.06, 15.55, -13.13, -19.02), longitude = c(67.71, 
    20.17, 1.66, 17.87, -61.8, -63.62, 133.78, 14.55, -77.4, 
    90.36, -59.54, -88.5, 2.32, -64.76, -63.59, 24.68, -51.93, 
    114.73, 25.49, -1.56, -24.01, 104.99, 12.35, -106.35, 20.94, 
    18.73, -71.54, 114.11, 113.54, 104.2, 120.96, -74.3, 15.83, 
    -83.75, -5.55, -77.78, 33.43, 127.51, 9.5, 42.59, -61.37, 
    -70.16, -78.18, 30.8, -88.9, 179.41, 25.75, 2.21, -149.41, 
    11.61, -15.31, 10.45, -1.02, 21.82, -61.6, -90.23, -9.7, 
    -15.18, -58.93, -72.29, -86.24, 19.5, -19.02, 78.96, 113.92, 
    53.69, 43.68, -8.24, 34.85, 12.57, -77.3, 138.25, 36.24, 
    37.91, -168.73, 47.48, 102.5, 35.86, 28.23, -9.43, 46.87, 
    34.3, 101.98, 73.22, -4, 14.38, -10.94, 57.55, -102.55, 103.85, 
    -7.09, 35.53, 95.96, 18.49, 84.12, 5.29, 165.62, 174.89, 
    -85.21, 8.08, 8.68, 8.47, 69.35, -80.78, -58.44, -75.02, 
    121.77, 19.15, -8.22, 127.77, 24.97, 29.87, -62.78, -60.98, 
    -61.29, -172.1, 6.61, 45.08, -14.45, -11.78, 160.16, 22.94, 
    -3.75, 80.77, -56.03, 31.47, 18.64, 8.23, 100.99, 125.73, 
    0.82, -61.22, 9.54, 35.24, 32.29, 53.85, -3.44, 34.89, -95.71, 
    -55.77, 166.96, -66.59, 108.28, 48.52, 27.85, 29.15)), .Names = c("area", 
"latitude", "longitude"), row.names = c(1L, 84L, 207L, 331L, 
440L, 557L, 813L, 941L, 1204L, 1320L, 1443L, 1831L, 1943L, 2060L, 
2163L, 2409L, 2534L, 2671L, 2791L, 2925L, 3040L, 3146L, 3262L, 
3387L, 3527L, 3636L, 3739L, 3867L, 4000L, 4121L, 4267L, 4408L, 
4540L, 4660L, 4789L, 5037L, 5174L, 5438L, 5535L, 5674L, 5781L, 
5892L, 6004L, 6138L, 6272L, 6653L, 6778L, 6911L, 7051L, 7172L, 
7292L, 7533L, 7680L, 7800L, 7943L, 8063L, 8193L, 8305L, 8396L, 
8515L, 8635L, 8767L, 8909L, 9034L, 9168L, 9294L, 9414L, 9534L, 
9670L, 9805L, 9953L, 10072L, 10215L, 10483L, 10610L, 10704L, 
10950L, 11188L, 11317L, 11392L, 11758L, 11883L, 12003L, 12137L, 
12241L, 12357L, 12487L, 12603L, 12722L, 12855L, 13087L, 13216L, 
13330L, 13442L, 13563L, 13684L, 13825L, 13954L, 14088L, 14217L, 
14335L, 14466L, 14730L, 14863L, 14993L, 15115L, 15247L, 15389L, 
15525L, 15659L, 15923L, 16199L, 16307L, 16406L, 16521L, 16630L, 
16738L, 16829L, 16959L, 17206L, 17579L, 17679L, 17819L, 17969L, 
18200L, 18316L, 18433L, 18575L, 18818L, 19079L, 19165L, 19280L, 
19397L, 19523L, 19741L, 20002L, 20123L, 20266L, 20395L, 20536L, 
20783L, 20879L, 21009L, 21118L, 21237L, 21357L), class = "data.frame")

代码:

sbbox <- make_bbox(lon = d1$longitude, lat=d1$latitude, f=.1)
s_map <- get_map(location = sbbox,maptype = 'terrain', source = 'google')

ggmap(s_map) +
  geom_point(data=d1, mapping = aes(x=longitude,y=latitude)) +
  geom_label_repel(aes(x=longitude, y=latitude,label=area), 
                   data=d1,box.padding = 2, point.padding = 0.0005, fontface = 'bold') +
  ggtitle('latitude vs longitude') +
  theme(plot.title = element_text(hjust=0.05)) +
  xlab('Hote Longitude') +
  ylab('Hotel Latitude') +
  theme(text = element_text(size=15),
        axis.text.x = element_text(angle=90,hjust=0)) +
  theme(text = element_text(size=15),
        axis.text.y = element_text(angle=90, hjust=1))

警告信息:

Warning messages:
1: Removed 15 rows containing missing values (geom_point). 
2: Removed 15 rows containing missing values (geom_label_repel). 

Map output:

在此处输入图像描述

提前感谢您的帮助!

标签: rgoogle-mapsggplot2latitude-longitudeggrepel

解决方案


推荐阅读