我正在尝试创建一个基本上将州分为销售地区的等值区域地图。例如:佐治亚州,佛罗里达州和阿拉巴马州将被视为一个名为东南部的领土。
似乎我应该能够使用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
答案 0 :(得分:2)
不确定您是否还需要答案,但我建议您使用地图库:
map("usa")
map("state",regions=c("connecticut", "maine", "massachusetts",
"new hampshire", "rhode island","vermont"),interior=F,boundary=T,add=T)
然后使用col= specifications
为每个区域设置不同的级别。这有点单调乏味,但确实创造了区域。