Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据R中的值给美国各州上色_R_Map_Colors_Maps_State - Fatal编程技术网

根据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" 我觉得应该有

在R中,我想根据值为美国各州着色。 我的数据如下表所示:

    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])
#您的值(此处为命名向量)

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