代码之家  ›  专栏  ›  技术社区  ›  Chase

r中的choropleth映射问题

  •  2
  • Chase  · 技术社区  · 16 年前

    编辑:我已经意识到问题的根源。我只有我有数据的县的统计信息,这少于我正在谋划的地区的县的数量。

    毫无疑问,问题代码行在这里:

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

    有人对如何从地图库中生成一个长度与纽约、新泽西、CT和PA的县匹配的矢量有什么建议吗?我想合并我拥有的计数数据,包括我没有信息的县的0。

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

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

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

    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))))
    

    这是我的配对

    map names<-map(“county”,plot=false)[4]$名称
    colorsMatched<-d$colorbuskets[na.ommit(匹配(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")
    

    我确信我遗漏了一些基本的re:maps函数绘制项目的顺序-但是我似乎无法理解。谢谢你的帮助。如果你需要更多的信息,请告诉我。

    1 回复  |  直到 16 年前
        1
  •  0
  •   Ramnath    16 年前

    这里有一个可能的解决方案,通过将数据与来自选定状态映射的数据合并来解决您的问题。这就是你要找的吗?

    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")
    
    推荐文章