2

我正在尝试创建一个基本上将州分组为销售区域的等值线地图。例如:乔治亚州、佛罗里达州和阿拉巴马州将被视为一个称为东南部的领土。

看来我应该能够使用来自 map_data("state") 的现有状态 shapefile 数据来创建具有这些领土的新 shapefile。

但我不完全确定如何做到这一点。


谢谢你们的帮助。让我尝试添加一些代码来解释。

library(ggplot2)
library(maps)
library(plyr)
library(grid)

#load us state map data
us_state_map = map_data("state");

#map each state to a division
us_state_map$division[us_state_map$region %in% c("connecticut", "maine", "massachusetts", "new hampshire", "rhode island", "vermont")] <- "D1"
us_state_map$division[us_state_map$region %in% c("new jersey","new york","puerto rico","virgin islands")] <- "D2"
us_state_map$division[us_state_map$region %in% c("delaware","district of columbia","maryland","pennsylvania","virginia","west virginia")] <- "D3"
us_state_map$division[us_state_map$region %in% c("alabama","florida","georgia","kentucky","mississippi","north carolina","south carolina","tennessee")] <- "D4"
us_state_map$division[us_state_map$region %in% c("illinois","indiana","michigan","minnesota","ohio","wisconsin")] <- "D5"
us_state_map$division[us_state_map$region %in% c("arkansas","louisiana","new mexico","oklahoma","texas")] <- "D6"
us_state_map$division[us_state_map$region %in% c("iowa","kansas","missouri","nebraska")] <- "D7"
us_state_map$division[us_state_map$region %in% c("colorado","montana","north dakota","south dakota","utah","wyoming")] <- "D8"
us_state_map$division[us_state_map$region %in% c("arizona","california","hawaii","nevada")] <- "D9"
us_state_map$division[us_state_map$region %in% c("alaska","idaho","oregon","washington")] <- "D10"

#create a dummy variable that counts the number of states in each division
divisions.subtotal <- ddply(us_state_map, .(division), summarize, NumberOfStates=length(unique(region)))

#merge our dummy data back into the map data table
us_state_map.mod <- merge(x=us_state_map, y=divisions.subtotal, all.x=TRUE, by.x="division", by.y="division")
us_state_map.mod = arrange(us_state_map.mod, order);
us_state_map.mod$division = as.factor(us_state_map.mod$division)

#plot a map of each division
map <- ggplot() 
map = map + geom_polygon(data=us_state_map.mod, aes(x=long, y=lat, group=group, fill=division))
map

#using these division boundaries, how do i plot the NumberOfStates variable for each Division
4

1 回答 1

3

不确定您是否仍然需要答案,但我建议您使用地图库:

map("usa")

map("state",regions=c("connecticut", "maine", "massachusetts", 
    "new hampshire", "rhode island","vermont"),interior=F,boundary=T,add=T)

然后使用col= specifications为每个区域着色不同的级别。这有点乏味,但它确实创建了区域。

于 2013-10-11T16:18:10.883 回答