1

我一直在使用 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"))

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

4

1 回答 1

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)

在此处输入图像描述

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

于 2019-07-11T08:17:57.310 回答