绘制(填充颜色)地图与美国陈述R中的数据

时间:2014-06-25 02:40:02

标签: r map plot

我有以下数据,我想在美国的国家地图上绘制:

x = data.frame(state.x77[,"Income"])

如何在地图上将这些颜色填充为状态?

我可以绘制地图:

map("state", boundary=TRUE, col=colorRampPalette(c("blue","green","yellow","red"))(50), fill=T)

我可以匹配状态:

match(map("state",plot=F)$names, tolower(rownames(x)))

我尝试修改?map_data:

中的示例
if (require("maps")) {
states <- map_data("state")
arrests <- USArrests
names(arrests) <- tolower(names(arrests))
arrests$region <- tolower(rownames(USArrests))

choro <- merge(states, arrests, sort = FALSE, by = "region")
choro <- choro[order(choro$order), ]
qplot(long, lat, data = choro, group = group, fill = assault,
  geom = "polygon")
qplot(long, lat, data = choro, group = group, fill = assault / murder,
  geom = "polygon")
}

但我无法调整它。谢谢你的帮助。

1 个答案:

答案 0 :(得分:5)

您无需将逮捕数据与地图数据合并。您可以将地图数据单独传递到geom_map图层。尝试

x = data.frame(region=tolower(rownames(state.x77)), 
    income=state.x77[,"Income"], 
    stringsAsFactors=F)

states_map <- map_data("state")
ggplot(x, aes(map_id = region)) + 
    geom_map(aes(fill = income), map = states_map) +
    scale_fill_gradientn(colours=c("blue","green","yellow","red")) + 
    expand_limits(x = states_map$long, y = states_map$lat)