2012-02-16 157 views
31

我最近一直在使用ggplot2来创建一堆choropleths。我想知道是否有可能使用GGPLOT2创建与此类似(从WorldMapper)地图:Cartogram + choropleth map in R

enter image description here

这是在shape文件多边形扭曲代表相对人口数一等值线。我相信这被称为cartogram。他们用一堆其他变量来做到这一点。本着Choropleth R Challenge的精神,是否有人知道如何使用R来做到这一点?

+2

您可以尝试[ScapeToad](http://scapetoad.choros.ch/)获取R环境以外的制图。 – radek 2012-02-17 11:51:48

+1

感谢您的支持; ScapeToad工作得很好,满足我的需求。但是,如果R内有解决方案,我会留下问题。 – 2012-02-18 18:46:52

+4

我开始致力于将d3-cartogram与rCharts进行集成。你的数据的结构是什么? – timelyportfolio 2014-09-11 14:39:59

回答

4

cartogrampackage,可在CRAN上找到,它具有您想要的橡胶片扭曲式纸制图。

1

这可能工作:

您将需要预安装FFTW。 Rcartogram and getcartr you will need devtools

不知道如何做到这一点ggplot2,但这是另一种选择。

这里我使用的是一个Thematic World Map的shape文件,下载并解压后,会得到一个名为TM_WORLD_BORDERS-0.3的文件夹。

对于等值线/示意地图,你会重塑先用大小,明暗有一个特点:

library(rgdal)#needed for readOGR 
library(sp) #needed for spplot 
library(Rcartogram) 
library(getcartr) 
setwd("<your_directory_with_shapefile>") #to the file that has your shapefile and your information file (in this case, a csv named datR) 
#read shapefile 
#here i have a folder with a shapefile and a csv with columns as ISO (IS02 for convenience) country and value 
worldR <- readOGR(dsn = getwd(), layer= "TM_WORLD_BORDERS-0.3") # If reading a shapefile, the data source name (dsn= argument) is the folder (directory) where the shapefile is, and the layer is the name of the shapefile (without the .shp extension) 
#names(worldR) #note how here there are columns for ISO2 (which matches a column named 'iso' in datR and LAT\LON 
#[1] "FIPS"  "ISO2"  "ISO3"  "UN"  "NAME"  "AREA"  "POP2005" "REGION" "SUBREGION" "LON"  "LAT" 
proj4string(worldR) 
datR <- read.csv("datR.csv") #this is a file that has one column called 'score' and one column called size': 

    head(datR) 
    # iso size  score 
    #1 AE 323 0.9819077 
    #2 AR 262 0.9591067 
    #3 AT 7481 0.9987313 
    #4 AU 5425 0.9837414 
    #5 BA 31 0.9871938 
    #6 BB 99 0.9715991 

    ##Merge SpatialPolygonsDataFrame with other info 
    map_dat <- merge(worldR, datR, by.x="ISO2",by.y="iso") 
    #remove coordinate reference system arguments 
    proj4string(map_dat) <- CRS(as.character(NA)) # from here https://github.com/chrisbrunsdon/getcartr/issues/1 
    world.carto <- quick.carto(map_dat, map_dat$size, blur = 0) 
    #plot(world.carto) #cartogram without anything 
    #spplot size, color 
    my.palette = c("#ff0000", "#ff8000", "#ffff00", "#bfff00","#00ff00") #red, orange, yellow, light green, dark green 
    spplot(world.carto, 'score', col.regions = my.palette, cuts = length(my.palette)-1,main="Choropleth of score and cartogram of size") 

这应该给你类似于这样一个情节:

enter image description here

我在匆忙中做了这件事,让我知道它是否有效