【问题标题】:R - Print specific country names in a map using rworldmapR - 使用 rworldmap 在地图中打印特定国家名称
【发布时间】:2019-06-16 23:48:52
【问题描述】:

我正在使用 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() 会打印所有国家/地区名称,但不可读。

谢谢

【问题讨论】:

    标签: r ggplot2 plot rworldmap


    【解决方案1】:

    this 之前回答的一点帮助下:

    # 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",因为有些国家/地区几乎无法阅读。或者可以更改地图中的色标

    【讨论】:

    • 有趣的解决方法谢谢。我想目前没有内置方法。我尝试使用 labelCountries,但在理解它的工作原理时遗漏了一些东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多