簡體   English   中英

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" ,因為有些國家幾乎無法閱讀。 或者可能更改地圖中的顏色比例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM