【问题标题】:Choropleth mapping issue in RR中的Choropleth映射问题
【发布时间】:2011-02-07 11:45:06
【问题描述】:

编辑:我已经意识到我的问题的根源。我只有我有数据的县的计数信息,这比我要绘制的区域中的县数少。

按理说问题出在这几行代码:

mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]

对于如何从地图库中生成与 NY、NJ、CT 和 PA 中的县数相匹配的适当长度的向量,是否有人建议?我想合并我拥有的计数数据,并为我没有信息的县包括零。

我正在尝试按照此处描述的教程进行操作:http://www.thisisthegreenroom.com/2009/choropleths-in-r/

下面的代码执行,但它要么没有正确匹配我的数据集和 maps_counties 数据,要么没有按照我期望的顺序绘制它。例如,大纽约地区的结果区域没有显示密度,而宾夕法尼亚州的随机县显示密度最高。

我的数据表的一般格式是:

county state count
fairfield connecticut 17
hartford connecticut 6
litchfield connecticut 3
new haven connecticut 12
...
...
westchester new york 70
yates new york 1
luzerne pennsylvania 1

请注意,此数据按州和县排序,包括 CT、NJ、NY 和 PA 的数据。

首先,我读入我的数据集:

library(maps)
library(RColorBrewer)
d <- read.table("gissum.txt", sep="\t", header=TRUE)

#Concatenate state and county info to match maps library
d$stcon <- paste(d$state, d$county, sep=",")

#Color bins
colors = brewer.pal(5, "PuBu")
d$colorBuckets <- as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))))

这是我的匹配

mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]

绘图:

map("county"
  ,c("new york","new jersey", "connecticut", "pennsylvania")
  ,col = colors[d$colorBuckets[na.omit(match(mapnames ,d$stcon))]]
  ,fill = TRUE
  ,resolution = 0
  ,lty = 0
  ,lwd= 0.5
)
map("state"
  ,c("new york","new jersey", "connecticut", "pennsylvania")
  ,col = "black"
  ,fill=FALSE
  ,add=TRUE
  ,lty=1
  ,lwd=2
)

map("county"
   ,c("new york","new jersey", "connecticut", "pennsylvania")
   ,col = "black"
   ,fill=FALSE
   ,add=TRUE
  , lty=1
  , lwd=.5
)
title(main="Respondent Home ZIP Codes by County")

我确定我错过了一些基本的内容:地图函数绘制项目的顺序 - 但我似乎无法弄清楚。谢谢您的帮助。如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: r mapping plot


    【解决方案1】:

    通过将您的数据与来自选定州的地图的数据合并,可以解决您的问题。这是你要找的吗?

    library(maps);
    library(RColorBrewer);
    
    # Create Dummy Data Frame to Play With
    
    d = rbind(c('fairfield','connecticut',17),c('westchester','new york',70), c('luzerne','pennsylvania',1));
    d = data.frame(d);
    names(d) = c("county", "state", "count");
    d$count = as.numeric(as.character(d$count));
    d$stcon = paste(d$state, d$county, sep=",");
    
    # Extract mapnames for States
    
    mapnames2 = map("county",c("new york","new jersey", "connecticut", "pennsylvania"),plot=FALSE)[4]$names;
    mapnames2 = data.frame(mapnames2);
    names(mapnames2) = "stcon";
    
    # Merge with d
    
    d = merge(mapnames2, d, all = T);
    d$count[is.na(d$count)] = 0;
    
    
    # Color bins
    colors = brewer.pal(5, "PuBu");
    d$colorBuckets = as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))));
    
    map("county"
      ,c("new york","new jersey", "connecticut", "pennsylvania")
      ,col = colors[d$colorBuckets]
      ,fill = TRUE
      ,resolution = 0
      ,lty = 0
      ,lwd= 0.5
    )
    map("state"
      ,c("new york","new jersey", "connecticut", "pennsylvania")
      ,col = "black"
      ,fill=FALSE
      ,add=TRUE
      ,lty=1
      ,lwd=2
    )
    
    map("county"
       ,c("new york","new jersey", "connecticut", "pennsylvania")
       ,col = "black"
       ,fill=FALSE
       ,add=TRUE
      , lty=1
      , lwd=.5
    )
    title(main="Respondent Home ZIP Codes by County")
    

    【讨论】:

    • 这正是我所需要的。感谢您的周到回复!
    猜你喜欢
    • 2016-03-21
    • 1970-01-01
    • 2017-09-02
    • 2010-10-11
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多