2012-02-16 73 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

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