R - 使用 rworldmap 在地图中打印特定国家名称

R - Print specific country names in a map using rworldmap

我正在使用 R 中的 rworldmap 包中的欧洲地图创建热图(因为我不知道如何使用 ggmap 或 ggplot2 执行此操作)。

我只需要绘制数据框中存在的国家/地区名称,而不是所有欧洲国家/地区。我怎样才能做到这一点? 我的代码:

library(RColorBrewer)
#getting colours
colourPalette <- brewer.pal(5,'RdPu')


library(rworldmap)

europe <- data.frame(
  "country" = c("Greece", 
                "France", 
                "Spain",
                "Italy",
                "UK",
                "Finland","Norway","Sweden",
                "Germany",
                "Romania"), 
  "x" = c(2.5, 3, 2.2, 1.8,2.32, 1.99, 2.01, 2.34, 1.88, 2.45))

matched <- joinCountryData2Map(europe, joinCode="NAME", nameJoinColumn="country")





mapParams <- mapCountryData(matched, 
                            nameColumnToPlot="x", 
                            mapTitle="my Titley", 
                            addLegend=FALSE,
                            mapRegion="Europe"
                            ,colourPalette=colourPalette,
                            oceanCol="#404040", missingCountryCol="#A0A0A0")



#adding legend
do.call(addMapLegend
        ,c(mapParams
           ,legendLabels="all"
           ,legendWidth=0.5
           ,legendIntervals="data"
           ,legendMar = 2))

labelCountries()

使用 labelCountries() 打印所有国家/地区名称,但不可读。

谢谢

之前回答的一点帮助下:

# get the coordinates for each country
country_coord<-data.frame(coordinates(matched),stringsAsFactors=F)

# label the countries
country = c("Greece", 
              "France", 
              "Spain",
              "Italy",
              "UK",
              "Finland","Norway","Sweden",
              "Germany",
              "Romania")

#filter your wanted countrys
country_coord = country_coord[country,]

#insert your labels in plot
text(x=country_coord$X1,y=country_coord$X2,labels=row.names(country_coord))

您可以使用 text 添加国家/地区标签,但您必须先从匹配的坐标中提取坐标。 输出:

您可以在 text 中玩玩 col = "color",因为有些国家几乎无法阅读。或者更改地图中的色标