【问题标题】:Is there a way to add labels or city names on Rworldmap?有没有办法在 Rworldmap 上添加标签或城市名称?
【发布时间】:2019-11-20 21:25:33
【问题描述】:

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

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

【问题讨论】:

    标签: r ggplot2 rworldmap


    【解决方案1】:

    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)
    
    

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

    【讨论】:

      猜你喜欢
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多