我想繪製一個世界地圖使用ggplot2(v.9),它結合了兩部分,如果信息。下面的例子說明:ggplot地圖與l
library(rgdal)
library(ggplot2)
library(maptools)
# Data from http://thematicmapping.org/downloads/world_borders.php.
# Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
# Unpack and put the files in a dir 'data'
gpclibPermit()
world.map <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
world.ggmap <- fortify(world.map, region = "NAME")
n <- length(unique(world.ggmap$id))
df <- data.frame(id = unique(world.ggmap$id),
growth = 4*runif(n),
category = factor(sample(1:5, n, replace=T)))
## noise
df[c(sample(1:100,40)),c("growth", "category")] <- NA
ggplot(df, aes(map_id = id)) +
geom_map(aes(fill = growth, color = category), map =world.ggmap) +
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
scale_fill_gradient(low = "red", high = "blue", guide = "colorbar")
然而,這種解決方案是不同時顯示growth
和category
的好方法。 Growth
是非常明顯的,但category
幾乎不可能看到,因爲它只是一個邊界。
我試圖增加邊框的大小,但沒有運氣(新的geom_map很難使用)。有沒有人知道如何在上面的例子中增加邊框尺寸,或甚至更好,一種顯示兩個因素的機制?
附加問題:國家名稱(如地圖包使用的國家名稱(具有USSR!特徵)是示例中使用的數據很脆弱。我更喜歡使用ISO 3166-1 alpha-3(1)。有誰知道有GGPLOT2設有ISO -...國名易於使用的數據(包含在鏈接數據)
結果:
result http://ompldr.org/vY3hsYQ
是什麼world.map?它沒有在你的代碼中定義。如果我嘗試強化(w,region =「NAME」),我會得到'無效多字節字符'錯誤。請提供可重複的代碼。 – 2012-03-04 20:03:47
對不起,更正。這是w。 – Rasmus 2012-03-04 20:10:24
在fortify行上出現以下錯誤:「nchar(ID)錯誤:無效的多字節字符串1」 – 2012-03-04 22:12:34