2012-06-27 48 views
15

我想用R來生成一個非常基本的世界地圖,其中一組國家用紅色填充以表示它們是瘧疾流行國家。如何在R中創建世界地圖並填寫特定國家?

我有一個數據框中的這些國家的名單,但我正努力將它們覆蓋在世界地圖上。

我試過在rworldmap包中使用wrld_simpl對象和joinCountryData2Map方法。

我會評論這個答案,以防止增加一個可能多餘的問題,但我目前沒有足夠的聲望,爲此道歉。

https://stackoverflow.com/a/9102797/1470099

我難以理解給予plot()命令的參數 - 我想知道是否有隻是一個簡單的方法來讓R能夠繪製所有國家名稱的我的列表中wrld_simpl地圖上,而不是使用grepl()等等,等等

plot(wrld_simpl, 
    col = c(gray(.80), "red")[grepl("^U", [email protected]$NAME) + 1]) 
+0

@ttmaccer,爲什麼不添加作爲一個答案? – A5C1D2H2I1M1N2O1R2T1

回答

17

使用rworldmap包,你可以使用以下命令:

library(rworldmap) 

theCountries <- c("DEU", "COD", "BFA") 
# These are the ISO3 names of the countries you'd like to plot in red 

malDF <- data.frame(country = c("DEU", "COD", "BFA"), 
    malaria = c(1, 1, 1)) 
# malDF is a data.frame with the ISO3 country names plus a variable to 
# merge to the map data 

malMap <- joinCountryData2Map(malDF, joinCode = "ISO3", 
    nameJoinColumn = "country") 
# This will join your malDF data.frame to the country map data 

mapCountryData(malMap, nameColumnToPlot="malaria", catMethod = "categorical", 
    missingCountryCol = gray(.8)) 
# And this will plot it, with the trick that the color palette's first 
# color is red 
+0

非常感謝,這真的幫助了我......還有一些非洲小國沒有出現在地圖上,但這將是另一個日子的任務! – phlancelot

12

嘗試使用googleVis包,然後用gvisGeoMap功能

G1 <- gvisGeoMap(Exports,locationvar='Country',numvar='Profit',options=list(dataMode='regions')) 

plot(G1) 
+1

哇,我從來沒有意識到googleVis有多強大!非常簡單的語法,不需要擔心spatialPointsDataFrame的問題,並且輸出很漂亮! –

3
library(maptools) 
    data(wrld_simpl) 
    myCountries = [email protected]$NAME %in% c("Australia", "United Kingdom", "Germany", "United States", "Sweden", "Netherlands", "New Zealand") 
    plot(wrld_simpl, col = c(gray(.80), "red")[myCountries+1]) 

enter image description here

相關問題