r - 使用 R 中的美国各州数据绘制(填充颜色) map

标签 r map plot

我有以下数据要绘制在美国的州 map 上:

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

如何将这些绘制为 map 上各州的颜色填充?

我可以绘制 map :
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")
}

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

最佳答案

您不需要将逮捕数据与 map 数据合并。您可以将 map 数据单独传递给 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)

关于r - 使用 R 中的美国各州数据绘制(填充颜色) map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24399367/

相关文章:

r - R 中的 MCMC 变点模型

r - 填充ggplot2散点图中叠加的椭圆

c++ - 调用 std::map::clear 的段错误

matlab - MATLAB 中数据周围的椭圆

python - 如何绘制网络图的补集?

r - 在 map 图 block 上绘制空间多边形时如何调整透明度?

r - 无法安装包车

c++ - pair(const std::pair<_T1, _T2>&) 被隐式删除,因为默认定义格式不正确错误:分配 unique_ptr 映射时

java - Hibernate 中的 SortedMap 键持久化

python - 与Python中的子图共享X轴并同时更改大小