Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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_Plot - Fatal编程技术网

用R中的美国各州数据绘制(填充颜色)地图

用R中的美国各州数据绘制(填充颜色)地图,r,map,plot,R,Map,Plot,我想在美国的州地图上绘制以下数据: 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))) 我试图从

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

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_数据修改示例:

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")
}
if(需要(“映射”)){

州您不需要将逮捕数据与地图数据合并。您可以将地图数据单独传递到
geom\u地图层。请重试

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)
x=data.frame(region=tolower(rownames(state.x77)),
收入=州.x77[,“收入”],
(系数=F)

州地图逮捕数据仅来自?地图地图数据中的示例。我只是试图修改它。我想绘制x(x@rnso很抱歉。我忘了向上滚动。我已经更新了示例以处理state.x77收入数据。这正是我所需要的。只是我想使用这个调色板:col=colorrmppalete(c(“蓝色”、“绿色”),“黄色”、“红色”)(一些数字)@rn所以我不确定它看起来有多好,但你在这里。请阅读
ggplot2
文档以了解进一步的自定义选项。
ggplot2
中是否有
map\u data
?我刚才在找它。