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

时间:2019-07-11 08:07:48

标签: r ggplot2 rworldmap

我一直在使用Rworldmap创建一个地图,该地图突出显示了特定的国家/地区并代表了那些城市。我想在城市点旁边添加一些文字,上面写着城市名称。

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

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"))

我想在地图上显示地图上突出显示的城市的名称:“科隆”,“钦奈”,“丹佛”,“马德里”,“马尼拉”,“圣地亚哥”,“西雅图” ,“ Shanghai”,但是当我尝试将其链接在一起时会得到NULL

1 个答案:

答案 0 :(得分:0)

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)

enter image description here

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