用R中的美国状态数据绘制(填充颜色)图 [英] Plot (fill color) map with USA states data in R
本文介绍了用R中的美国状态数据绘制(填充颜色)图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要在美国的州地图上绘制以下数据:
I have following data which I want to plot on States map of the USA:
x = data.frame(state.x77[,"Income"])
如何绘制这些颜色作为地图上的颜色填充状态?
How can I plot these as color fill in states on a map?
我可以绘制地图:
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修改示例:
I tried to modify example from ?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")
}
但是我无法调整它.感谢您的帮助.
But I am not able to adjust it. Thanks for your help.
推荐答案
您无需将逮捕数据与地图数据合并.您可以将地图数据分别传递到geom_map
图层.试试
You don't need to merge your arrests data with your map data. You can pass the map data separately to a geom_map
layer. Try
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)
这篇关于用R中的美国状态数据绘制(填充颜色)图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文