【问题标题】:Plot (fill color) map with USA states data in R用 R 中的美国州数据绘制(填充颜色)地图
【发布时间】:2014-06-25 02:40:02
【问题描述】:

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

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

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

【问题讨论】:

    标签: r map plot


    【解决方案1】:

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

    【讨论】:

    • 逮捕数据仅来自?map_data中的示例。我只是想修改它。我想绘制 x (x
    • @rnso 很抱歉。我忘了向上滚动。我已更新示例以使用 state.x77 收入数据。
    • 这正是我所需要的。只是我想使用这个调色板: col=colorRampPalette(c("blue","green","yellow","re​​d"))(some_number)
    • @rnso 我不确定它看起来有多棒,但是你来了。请阅读ggplot2 文档了解更多自定义选项。
    • map_dataggplot2 中吗?我之前正在寻找它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2016-03-21
    • 1970-01-01
    • 2013-12-24
    相关资源
    最近更新 更多