2012-03-04 105 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