根据R中的值给美国各州上色
在R中,我想根据值为美国各州着色。 我的数据如下表所示:根据R中的值给美国各州上色,r,map,colors,maps,state,R,Map,Colors,Maps,State,在R中,我想根据值为美国各州着色。 我的数据如下表所示: AK 0.1511529 AL 0.1720370 AR 0.2078534 AZ 0.2057174 等等 我如何用一种简单的方式来做这件事?我试过使用 maps("state"), 但这给了我与州缩写不对应的州名: "alabama" "arizona" 诸如此类 "washington:orcas island" 我觉得应该有
AK 0.1511529
AL 0.1720370
AR 0.2078534
AZ 0.2057174
等等
我如何用一种简单的方式来做这件事?我试过使用
maps("state"),
但这给了我与州缩写不对应的州名:
"alabama"
"arizona"
诸如此类
"washington:orcas island"
我觉得应该有一个更简单的解决方案,而不是使用
state.abb
state.name
谢谢匹配是一个不错的解决方案。我希望这个代码示例有帮助
# 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])
#您的值(此处为命名向量)
值考虑发布您尝试过的代码,以便其他人可以运行和修改它。