有没有办法在 Rworldmap 上添加标签或城市名称?

Is there a way to add labels or city names on Rworldmap?

我一直在使用 Rworldmap 创建一个地图,其中突出显示了特定的国家和代表这些城市的点。我想在城市点旁边添加一些文字,说明城市名称。

我尝试加载 ggplot2 并加载几何文本的代码:

geom_text(data = plotcities, aes(lat,long), stat = "identity",
          position = "identity", parse = FALSE, check_overlap = TRUE, na.rm = FALSE,
          show.legend = FALSE, inherit.aes = TRUE)

我返回的都是空值或错误。

通用代码如下:

library(rworldmap)
library("ggmap")
library(maptools)
library(maps)
library(RColorBrewer)

data("world.cities")
plotcities <- subset(world.cities, 
                     name %in% c("Cologne", "Chennai", "Denver", "Madrid", "Manila", "San Diego", "Seattle", "Shanghai")
                     & country.etc %in% c("Germany", "USA", "Spain", "China", "Philippines", "India"))



theCountries <- c("USA", 
                  "CAN", "DEU", "FRA", "IND", 
                  "GBR", "NLD", "ITA", 
                  "CHN", "KOR", "JPN", 
                  "ESP", "PRT", "RUS", 
                  "NOR", "SGP", "AUS", 
                  "CHL", "MEX", "PHL", "RWA", 
                  "JOR", "HND", "PAN", "THA", "DOM", 
                  "ZAF", "TUR", "CHE", "FIN",
                  "SEN", "BOL", "OMN", "PAK", "CMR", "MUS", "BEL", "MYS", 
                  "UAE", "BRA", "MLI", "MOZ", "NAM", "EGY", "ARG", "UKR", "ZMB", "KEN",
                  "VNM", "NGA", "DNK", "IRN", "AFG")
# These are the ISO3 names of the countries you'd like to plot


CEAMap <- data.frame( country = c("USA", 
                                 "CAN", "DEU", "FRA", "IND", 
                                 "GBR", "NLD", "ITA", 
                                 "CHN", "KOR", "JPN", 
                                 "ESP", "PRT", "RUS", 
                                 "NOR", "SGP", "AUS", 
                                 "CHL", "MEX", "PHL", "RWA", 
                                 "JOR", "HND", "PAN", "THA", "DOM", 
                                 "ZAF", "TUR", "CHE", "FIN",
                                 "SEN", "BOL", "OMN", "PAK", "CMR", "MUS", "BEL", "MYS", "UAE", "BRA", "MLI", "MOZ", "NAM", "EGY", "ARG", "UKR", "ZMB", "KEN",
                                 "VNM", "NGA", "DNK", "IRN", "AFG"),
                      involvement = c(1, 
                                    2, 2, 2, 2, 
                                    3, 3, 3, 
                                    4, 4, 4, 
                                    5, 5, 5, 
                                    6, 6, 6, 
                                    7, 7, 7, 7,
                                    8, 8, 8, 8, 8,
                                    9, 9, 9, 9,
                                    10, 10, 10, 10, 10, 10, 10, 10, 
                                    11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
                                    12, 12, 12, 12, 12))

# CEAMap is a data.frame with the ISO3 country names plus a variable to
# merge to the map data
# To get rid of that stupid-ass continent Antarctica, the command is != "Antarctica", necessary to create a subset
CEAcountries <- joinCountryData2Map(CEAMap, joinCode = "ISO3",
                              nameJoinColumn = "country")
new_world <- subset(CEAcountries, continent != "Antarctica")
colourPalette <- RColorBrewer::brewer.pal(12,'PRGn')

# This will join your CEAcountries data.frame to the country map data


mapCountryData(new_world, nameColumnToPlot="country", 
               catMethod = "categorical", colourPalette = colourPalette, 
               mapTitle='CEA Locations',
               missingCountryCol = gray(.8),  addLegend = FALSE)

#PLOTTING THE CITIES ON THE MAP

points(plotcities$long, plotcities$lat, pch =  25, col = "gold1", bg= "gold3")

#ATTEMPT TO ADD NAMES TO THE CITIES
geom_label_repel(data = plotcities,
                 aes(long, lat, label = "Chart Data.Region", group="Chart"),
                 fill = "green",
                 label.size = NA,
                 color = 'black',
                 size  = 4,
                 box.padding = unit(.1, "lines"), point.padding = unit(0.0, "lines"))

我想在地图上显示地图上突出显示的城市名称:"Cologne"、"Chennai"、"Denver"、"Madrid"、"Manila", "San Diego", "Seattle", "Shanghai" 但是当我尝试将它链接在一起时我得到 NULL

ggplot2 在这种情况下没有任何用处。改为使用基础图中的 text()

mapCountryData(new_world, nameColumnToPlot="country", 
               catMethod = "categorical", colourPalette = colourPalette, 
               mapTitle='CEA Locations',
               missingCountryCol = gray(.8),  addLegend = FALSE)

points(plotcities$long, plotcities$lat, pch =  25, col = "gold1", bg= "gold3")

text(plotcities$long, plotcities$lat, plotcities$name, pos = 3)

您可以根据需要调整大小、颜色和位置(在您所在城市的左侧、右侧等)。