2015-02-12 25 views
2

早上好,我花了很多時間來弄清楚如何直接添加國家名稱的情節不像傳說的一部分,但像地圖的一部分。 即時通訊使用包rworldmap,試圖使用identifyCountries() - 但它是用於交互(當用戶點擊地圖時),比我發現這樣的解決方案 Administrative regions map of a country with ggmap and ggplot2但它是爲ggplot2,並且太複雜。我試圖做到這一點與mapCountryData()。希望你的幫助,謝謝。地圖上的國家的名稱與rworldmap

data <- data.frame(Country=c('Russia','Cyprus', 'Belize', 'Austria' ,'Virgin Islands', 
     'Italy','United States' ,'United Kingdom', 'Germany', 'France' ,'Poland' ,'Switzerland'), 
Value=c(-0.310,-0.206,-0.300,-0.179,-0.196,-0.174,-0.105,-0.142,-0.082,-0.097,-0.027,0.052)) 

    library('rworldmap') 

    pdf1 <- joinCountryData2Map(data, joinCode="NAME", nameJoinColumn="Country") 

    mapCountryData(pdf1, nameColumnToPlot="Value", catMethod="pretty", 
    colourPalette='white2Black',addLegend='TRUE',mapTitle=NULL, mapRegion="Europe") 
+0

'錯誤joinCountryData2Map(數據,joinCode = 「NAME」 ,nameJoinColumn =「國家」): 您的選擇名稱JoinColumn:'國家'似乎不存在您的數據,列=名稱值' – 2015-02-12 05:02:12

+0

@Pascal我的錯誤,我有變動編輯國家在數據 – 2015-02-12 05:06:17

回答

5

你可以試試:

# get the coordinates for each country 
country_coord<-data.frame(coordinates(pdf1),stringsAsFactors=F) 
# label the countries 
text(x=country_coord$X1,y=country_coord$X2,labels=row.names(country_coord)) 

通過你的代碼後,這些行,你會得到:

enter image description here

+0

謝謝,我花了一段時間弄清楚如何處理country_coord中的row.names,以我需要的國家子集,但最後我已經做到了,感謝您的座標幫助,和文本的方式! – 2015-02-12 16:46:10

+0

@DimaSukhorukov,不客氣!感謝您的反饋,我很高興知道我的答案幫助了您:-) – Cath 2015-02-13 07:41:02

+0

良好的工作。如果您願意,您可以用較少的代碼獲得相同的結果。 'text(pdf1,labels =「NAME」)' – Andy 2015-02-16 12:49:40

相關問題