【问题标题】:Color US states according to value in R根据 R 中的值为美国各州着色
【发布时间】:2013-12-24 10:05:57
【问题描述】:

在 R 中,我想按值对美国各州进行着色。 我的数据在这样的表中:

    AK  0.1511529
    AL  0.1720370
    AR  0.2078534
    AZ  0.2057174

等等

如何以简单的方式做到这一点?我试过使用

    maps("state"), 

但这给了我与州缩写不对应的州名:

"alabama"
"arizona"                         

诸如此类的东西

"washington:orcas island"

我觉得应该有一个比尝试匹配它们更简单的解决方案

 state.abb
 state.name

谢谢

【问题讨论】:

  • 考虑发布您尝试过的代码,以便其他人可以运行并修改它。

标签: r map colors maps state


【解决方案1】:

匹配不是一个糟糕的解决方案。我希望这个代码示例有所帮助。

# your values (here as named vector)
values <- runif(length(state.abb),0,1)
names(values) <- state.abb

# getting the names used by map
tmp <- map('state',plot=FALSE,namesonly=TRUE)
# matching (after adjusting using gsub and tolower)
tmp <- match(gsub('(:.*)','',tmp),tolower(state.name))
# convert your numbers to grey-scale and selct using the match
map('state',fill=TRUE,col=grey(values)[tmp])

匹配是必要的,也是因为每个状态可能有多个多边形要绘制。

【讨论】:

    猜你喜欢
    • 2016-03-21
    • 2021-08-22
    • 2020-07-31
    • 2011-08-09
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多