2012-03-04 32 views
21

我想繪製一個世界地圖使用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") 

然而,這種解決方案是不同時顯示growthcategory的好方法。 Growth是非常明顯的,但category幾乎不可能看到,因爲它只是一個邊界。

我試圖增加邊框的大小,但沒有運氣(新的geom_map很難使用)。有沒有人知道如何在上面的例子中增加邊框尺寸,甚至更​​好,一種顯示兩個因素的機制?

附加問題:國家名稱(如地圖包使用的國家名稱(具有USSR!特徵)是示例中使用的數據很脆弱。我更喜歡使用ISO 3166-1 alpha-3(1)。有誰知道有GGPLOT2設有ISO -...國名易於使用的數據(包含在鏈接數據)

結果:

result http://ompldr.org/vY3hsYQ

+0

是什麼world.map?它沒有在你的代碼中定義。如果我嘗試強化(w,region =「NAME」),我會得到'無效多字節字符'錯誤。請提供可重複的代碼。 – 2012-03-04 20:03:47

+0

對不起,更正。這是w。 – Rasmus 2012-03-04 20:10:24

+0

在fortify行上出現以下錯誤:「nchar(ID)錯誤:無效的多字節字符串1」 – 2012-03-04 22:12:34

回答

12

我會用不同的色調範圍的填充和線條顏色:

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(high = "red", low = "white", guide = "colorbar") + 
    scale_colour_hue(h = c(120, 240)) 

enter image description here

或者,使用填充類別和透明度爲增長水平。

ggplot(df, aes(map_id = id)) + 
    geom_map(aes(alpha = growth, fill = category), map =world.ggmap) + 
    expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + 
    scale_alpha(range = c(0.2, 1), na.value = 1) 

enter image description here

這取決於你想顯示什麼。

以防萬一,這裏是改變LINESIZE方式:

ggplot(df, aes(map_id = id)) + 
geom_map(aes(fill = growth, color = category, size = factor(1)), map =world.ggmap) + 
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + 
scale_fill_gradient(high = "red", low = "white", guide = "colorbar") + 
scale_colour_hue(h = c(120, 240)) + 
scale_size_manual(values = 2, guide = FALSE) 

enter image description here

這裏是HSV版本:

df$hue <- ifelse(is.na(df$category), 0, as.numeric(df$category)/max(as.numeric(df$category), na.rm=T)) 
df$sat <- ifelse(is.na(df$growth), 0, df$growth/max(df$growth, na.rm=T)) 
df$fill <- ifelse(is.na(df$category), "grey50", hsv(df$hue, df$sat)) 

ggplot(df, aes(map_id = id)) + 
geom_map(aes(fill = fill), map =world.ggmap) + 
expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + 
scale_fill_identity(guide = "none") 

enter image description here

+1

謝謝!問題是讓「足夠厚」的線變得明顯,即使圖形打印得很小。 – Rasmus 2012-03-04 22:28:51

+1

我覺得你問的太多了。我不認爲有可能用一張小紙來展示這麼多信息。 – kohske 2012-03-04 22:33:19

+0

你可以找到改變尺寸的方法,但實際上我不認爲這是有用的... – kohske 2012-03-04 22:38:18

7

一種選擇是,映射增長到一些點的大小繪製在質心多邊形。

centroids <- as.data.frame(coordinates(world.map)) 
df <- data.frame(df,centroids) 

choropleth <-ggplot() + 
    geom_map(aes(fill = category, map_id = id),data = df, map =world.ggmap) + 
    expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + 
    scale_fill_hue(na.value=NA) 
choropleth 



choropleth + geom_point(aes(x=V1,y=V2,size=growth),data=df) + 
    scale_area(range=c(0,3)) 

enter image description here

或者,如果你真的想雙碼顏色,你可以將顏色改爲點。請注意,您還可以使用新的OpenStreetMap軟件包(無恥插件)添加衛星圖像柵格地圖。

library(OpenStreetMap) 
library(raster) 
rastermap <- openmap(c(70,-179), 
     c(-70,179),zoom=2,type='bing') 
rastermap <- openproj(rastermap) 
autoplot(rastermap,expand=FALSE) + 
    geom_map(aes(x=70,y=70,fill = category, map_id = id),data = df, 
     map =world.ggmap) + 
    expand_limits(x = world.ggmap$long, y = world.ggmap$lat) + 
    scale_fill_hue(na.value=NA) + 
    geom_point(aes(x=V1,y=V2,colour=growth),data=df) + 
    scale_colour_gradient(low = "red", high = "blue", 
     guide = "colorbar",na.value=NA) 

enter image description here

+0

Ian ,你的包裝看起來不錯![1]它增加了一個很好的接觸。添加一個alpha(ed)填充可能看起來非常酷。順便說一句:你知道ggmap包嗎?根據摘要他們聽起來很相似。這個觀點是一個好主意。通過使用明星或其他東西,看起來更少的鏈接墨水臭,這可能會變成一件好事。謝謝! – Rasmus 2012-03-05 00:00:55

+0

我知道這個軟件包。 OpenStreetMap基本上是RGoogleMaps和ggmap的替代品。它確實很酷,比如瓷磚緩存和地圖投影轉換。 – 2012-03-05 01:17:25

+0

@IanFellows如果有興趣,我只是在這裏問一個相關的問題(使用相同的例子):http://stackoverflow.com/questions/13219387/world-map-map-halves-of-countries-to-different-colors – 2012-11-05 01:58:10